得到大熊猫日期时间指数

问题描述:

的前值我有一个数据帧的大熊猫与日期时间指数得到大熊猫日期时间指数

Date 
2013-02-22 00:00:00+00:00 0.280001 
2013-02-25 00:00:00+00:00 0.109999 
2013-02-26 00:00:00+00:00 -0.150000 
2013-02-27 00:00:00+00:00 0.130001 
2013-02-28 00:00:00+00:00 0.139999 
Name: MOM12 

,并要评估定日期时间指数的前三个值。

date = "2013-02-27 00:00:00+00:00" 
df.ix[date] 

我搜索了这一点,但因为我的指标是约会,我不能做

df.ix[int-1] 
+0

你有一个索引的每一天,还是有一些跳过天? – 2013-03-01 16:50:34

+0

例如,有一些跳过的日子,周末和假期。 – trbck 2013-03-01 17:51:53

+0

这个问题可以帮助你:[切割熊猫时间序列日期+/- 2个工作日](http://*.com/questions/14092339/slice-pandas-timeseries-on-date-2-business-days) – 2013-03-01 20:05:47

这里有一种方法通过get_loc做到这一点,首先抢索引键的整数位置:

In [15]: t = pd.Timestamp("2013-02-27 00:00:00+00:00") 

In [16]: df1.index.get_loc(t) 
Out[16]: 3 

然后你就可以使用iloc(获取整数位置,或切片由整数位置):

In [17]: loc = df1.index.get_loc(t) 

In [18]: df.iloc[loc - 1] 
Out[18]: 
Date 2013-02-26 00:00:00 
         -0.15 
Name: 2, Dtype: object 

In [19]: df1.iloc[slice(max(0, loc-3), min(loc, len(df)))] 
     # the min and max feel slightly hacky (!) but needed incase it's within top or bottom 3 
Out[19]:       
Date      
2013-02-22 0.280001 
2013-02-25 0.109999 
2013-02-26 -0.150000 

indexing section of the docs


我不太清楚你如何设置你的数据帧,但看起来并不像一个日期时间指数给我。这是我如何得到数据帧(带时间戳指数):

In [11]: df = pd.read_clipboard(sep='\s\s+', header=None, parse_dates=[0], names=['Date', None]) 

In [12]: df 
Out[12]: 
       Date   
0 2013-02-22 00:00:00 0.280001 
1 2013-02-25 00:00:00 0.109999 
2 2013-02-26 00:00:00 -0.150000 
3 2013-02-27 00:00:00 0.130001 
4 2013-02-28 00:00:00 0.139999 

In [13]: df1 = df.set_index('Date') 

In [14]: df1 
Out[14]:     
Date     
2013-02-22 0.280001 
2013-02-25 0.109999 
2013-02-26 -0.150000 
2013-02-27 0.130001 
2013-02-28 0.139999 
+0

对irow的支持已被弃用:http://pandas.pydata.org/pandas-docs/version/0.19.2/generated/pandas.DataFrame.irow.html,应该使用iloc – user2413548 2017-12-17 22:27:12

+0

@ user2413548谢谢,我需要写一个脚本来通过所有这些答案! – 2017-12-18 00:26:12