pandas: series

来源:互联网 发布:深圳市斯基凯网络 编辑:程序博客网 时间:2024/05/22 08:16

Series是一种类似于一维数组的对象,它由一组数据(各种Numpy数据类型)以及一组与之相关的数据标签(即索引)组成。仅由一组数据即可产生简单的Series。

In [2]:
s = Series(np.random.randn(5),index =['a','b','c','d','e'])
s 
Out[2]:
a   -2.895114b   -1.231825c    0.471328d   -1.287756e    1.475353dtype: float64
In [3]:
'b' in s
Out[3]:
True
In [4]:
s['b']
Out[4]:
-1.2318253286905612
In [5]:
s.index
Out[5]:
Index(['a', 'b', 'c', 'd', 'e'], dtype='object')

to_dict查看词典

In [6]:
mapping = s.to_dict()
mapping 
Out[6]:
{'a': -2.8951137320706906, 'b': -1.2318253286905612, 'c': 0.47132812495267679, 'd': -1.2877558861047, 'e': 1.4753528130026292}

通过词典的方式也可以构建Series

In [7]:
Series(mapping)
Out[7]:
a   -2.895114b   -1.231825c    0.471328d   -1.287756e    1.475353dtype: float64

默认取数据的长度是一年

In [8]:
ts = get_price('600208.XSHG')['ClosingPx'][-10:]
ts
Out[8]:
2013-12-20    3.05282013-12-23    3.06242013-12-24    3.05282013-12-25    3.04312013-12-26    2.99502013-12-27    3.04312013-12-30    3.05282013-12-31    3.08162014-01-02    3.07202014-01-03    3.0142Name: ClosingPx, dtype: float64

声明了strat_date & end_date 以后才可以进行更个性化的定制,默认是A股,美股别忘记加上国家'us'

In [11]:
tsspecial = get_price('000001.XSHE', start_date='2008-04-01', end_date='2015-04-12')['ClosingPx'][-90:]
tsspecial
Out[11]:
2014-11-26     7.60322014-11-27     7.67112014-11-28     8.43752014-12-01     8.26792014-12-02     8.89872014-12-03     8.88512014-12-04     9.46842014-12-05     9.85502014-12-08    10.32982014-12-09     9.29892014-12-10     9.62442014-12-11     9.44132014-12-12     9.45492014-12-15     9.21072014-12-16     9.7601               ...   2015-03-20    10.39092015-03-23    10.46552015-03-24    10.41122015-03-25    10.11282015-03-26    10.24842015-03-27    10.22812015-03-30    10.62152015-03-31    10.68252015-04-01    10.82492015-04-02    10.71642015-04-03    10.75032015-04-07    11.40152015-04-08    12.15432015-04-09    12.20862015-04-10    13.4294Name: ClosingPx, dtype: float64

从头去前五项

In [12]:
ts[:5]
Out[12]:
2013-12-20    3.05282013-12-23    3.06242013-12-24    3.05282013-12-25    3.04312013-12-26    2.9950Name: ClosingPx, dtype: float64
In [13]:
ts.index
Out[13]:
DatetimeIndex(['2013-12-20', '2013-12-23', '2013-12-24', '2013-12-25',               '2013-12-26', '2013-12-27', '2013-12-30', '2013-12-31',               '2014-01-02', '2014-01-03'],              dtype='datetime64[ns]', freq=None)
In [14]:
date = ts.index[6]
date
Out[14]:
Timestamp('2013-12-30 00:00:00')
In [15]:
ts[date]
Out[15]:
3.0528
In [16]:
ts[6]
Out[16]:
3.0528

米筐技能:同时读取多只股票,更多米筐技请参考basic_demo.

In [18]:
dfcn = get_price(['000024.XSHE', '000001.XSHE', '000002.XSHE'], start_date='2005-04-01', end_date='2015-04-12')['ClosingPx'][-10:]
0 0
原创粉丝点击