简体   繁体   English

如何在熊猫数据框中获取带有日期时间索引的最后n个行?

[英]How to get last n-number of rows with datetime index in a pandas dataframe?

I am trying to get the last 32 data points from a pandas dataframe indexed by date. 我试图从按日期索引的pandas数据框中获取最后32个数据点。 I have multiple re-sampled dataframes numbered data1, data2, data3, ect... that have been re-sampled from 1 hour, 4 hour, 12 hour, 1 day. 我有多个重新采样的数据帧,编号为data1,data2,data3等,这些帧已从1小时,4小时,12小时,1天开始重新采样。

I already tried to use get_loc with the datetime index that I want to end on for each dataframe but the problem is that my datetime index is sampled differently so the datetime index is off by a few hours. 我已经尝试将get_loc与每个数据帧要结束的datetime索引一起使用,但是问题是我的datetime索引采样方式不同,因此datetime索引关闭了几个小时。 I also tried to just subtract the equivalent hours from datetime but this does not guarantee 32 data points 我还尝试从日期时间中减去等效时间,但这不能保证32个数据点

from datetime import timedelta
import pandas as pd

data1 = data.resample('4H').last().ffill()
data2 = data.resample('6H').last().ffill()
data3 = data.resample('12H').last().ffill()
data4 = data.resample('1D').last().ffill()

# datetime I want to end my row with and get last 32 values
end_index = pd.Timestamp("2019-02-27 00:00:00+00:00")

# this method does not always guarantee 32 data points
b = data1.loc[end_index - timedelta(hours=192): end_index].bfill().ffill()
c = data2.loc[end_index - timedelta(hours=380): end_index].bfill().ffill()
d = data3.loc[end_index - timedelta(hours=768): end_index].bfill().ffill()
e = data4.loc[end_index - timedelta(hours=768): end_index].bfill().ffill()

# this method throws an error because end_index is off by a few hours sometimes
pos = data1.index.get_loc(end_index)
b = data1.loc[pos - 32: pos].bfill().ffill()

pos = data2.index.get_loc(end_index)
c = data2.loc[pos - 32: pos].bfill().ffill()

pos = data3.index.get_loc(end_index)
d = data3.loc[pos - 32: pos].bfill().ffill()

pos = data2.index.get_loc(end_index)
e = data4.loc[pos - 32: pos].bfill().ffill()

KeyError: 1498208400000000000 During handling of the above exception, another exception occurred: KeyError:1498208400000000000在处理上述异常期间,发生了另一个异常:

I think you need iloc for select by positions: 我认为您需要iloc来按职位选择:

pos = data2.index.get_loc(end_index)
c = data2.iloc[pos - 32: pos].bfill().ffill()

pos = data3.index.get_loc(end_index)
d = data3.iloc[pos - 32: pos].bfill().ffill()

pos = data2.index.get_loc(end_index)
e = data4.iloc[pos - 32: pos].bfill().ffill()

As Code Different suggested, using .tail(32) with loc indexing works! 正如Code Different建议的那样,将.tail(32)与loc索引一起使用!

b = data1.loc[: test_index].bfill().ffill().tail(32)
c = data2.loc[: test_index].bfill().ffill().tail(32)
d = data3.loc[: test_index].bfill().ffill().tail(32)
e = data4.loc[: test_index].bfill().ffill().tail(32)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM