获取pandas日期时间索引的前一个值

30 投票
4 回答
29687 浏览
提问于 2025-04-17 17:36

我有一个使用日期时间作为索引的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]

4 个回答

1

我也遇到过同样的问题,感谢Andy Hayden的解决方案,我成功地实现了对带有时间索引的DataFrame行的遍历。所以我把它放进了一个小函数里。这个函数可以用来获取之前或之后的值,只要索引没有超出范围。

def get_row(df, row, n = 0, value = None):
    loc = df.index.get_loc(row[0])
    if value == None:
        return df.iloc[loc + n]
    else:
        return df.iloc[loc + n][value]

所以在遍历行的时候,你可以调用这个函数。

for row in df.itertuples():
    # Get past value of a whole row
    get_row(df, row, -1)
    # Get past value of a certain column of a row
    get_row(df, row, -1, "column_name")
    # Get future value of a certain column of a row
    get_row(df, row, 1, "column_name")

    # Can be used to get the current row but this is slower than the following function
    # Slower
    get_row(df, row, 0, "column_name")
    # Faster
    row[data.columns.get_loc("column_name") + 1]
3

你可以试试用 df.shift().loc[date] 这个方法吗?

28

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

可以查看 文档中的索引部分


我不太确定你是怎么设置你的 DataFrame 的,但在我看来这不像是一个时间索引。 这是我获取 DataFrame(带时间戳索引)的方法:

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

撰写回答