通过熊猫的时间序列

时间:2013-01-23 06:59:12

标签: python pandas

数据:

date,                                             price1

2001/11/01 00:00:01am,                              10

2001/11/02 00:00:02am,                              20

2001/11/03 00:00:03am,                              15

2001/11/04 00:00:04am,                              30

使用pandas数据框(df),我们可以用这种方式获取第一列的值:

df.price1

如果我们想要你能做些什么?

df.date

无效。

1 个答案:

答案 0 :(得分:2)

In [36]: rng = date_range('1/1/2011', periods=5, freq='H')

In [37]: df = DataFrame({'price':[1,2,3,4,5]},index = rng)

In [38]: df
Out[38]: 
                     price
2011-01-01 00:00:00      1
2011-01-01 01:00:00      2
2011-01-01 02:00:00      3
2011-01-01 03:00:00      4
2011-01-01 04:00:00      5

In [39]: df.index
Out[39]: 
<class 'pandas.tseries.index.DatetimeIndex'>
[2011-01-01 00:00:00, ..., 2011-01-01 04:00:00]
Length: 5, Freq: H, Timezone: None

In [40]: df.index.values
Out[40]: 
array([1970-01-15 104:00:00, 1970-01-15 105:00:00, 1970-01-15 106:00:00,
       1970-01-15 107:00:00, 1970-01-15 108:00:00], dtype=datetime64[ns])