获取pandas datetime index的先前值

时间:2013-03-01 16:44:48

标签: python pandas

我有一个带有日期时间索引的pandas数据框

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]

3 个答案:

答案 0 :(得分:16)

这是一种方法,首先通过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


我不太确定你是如何设置你的DataFrame的,但这对我来说看起来不像是一个日期时间索引。这是我如何得到DataFrame(带有Timestamp索引):

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

答案 1 :(得分:1)

您能做df.shift().loc[date]吗?

答案 2 :(得分:0)

使用 shift 获取前一行值

data=[('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)]
df=pd.DataFrame(data=data,columns=['date','value'])
df['date']=pd.to_datetime(df['date'])

df['p_value']=df.value.shift(1)
df['pp_value']=df.value.shift(2)
df['ppp_value']=df.value.shift(3)
print(df)

输出

         date                    value   p_value  pp_value  ppp_value
 0 2013-02-22 00:00:00+00:00  0.280001       NaN       NaN        NaN
 1 2013-02-25 00:00:00+00:00  0.109999  0.280001       NaN        NaN
 2 2013-02-26 00:00:00+00:00 -0.150000  0.109999  0.280001        NaN
 3 2013-02-27 00:00:00+00:00  0.130001 -0.150000  0.109999   0.280001
 4 2013-02-28 00:00:00+00:00  0.139999  0.130001 -0.150000   0.109999