如何使用偏移量周期性地选择数据帧的行?

2024-05-19 10:54:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望每N小时选择一行数据帧。我可以使用df[::int(period)],但我不知道timeserie何时开始。另外,我需要对period应用一个offset,因此假设我的数据帧在午夜开始,它给出:

period = 6 

offset = 0
hours = [0, 6, 12, 18]

offset = 3
hours = [3, 9, 15, 21]

我怎么能用最少的行数做到这一点

这就是我的数据帧的外观:

2020-05-06 00:00:00+00:00  0
2020-05-06 01:00:00+00:00  1
2020-05-06 02:00:00+00:00  2
2020-05-06 03:00:00+00:00  3
2020-05-06 04:00:00+00:00  4
2020-05-06 05:00:00+00:00  5
2020-05-06 06:00:00+00:00  6
2020-05-06 07:00:00+00:00  7
2020-05-06 08:00:00+00:00  8
2020-05-06 09:00:00+00:00  9
2020-05-06 10:00:00+00:00  10
2020-05-06 11:00:00+00:00  11
2020-05-06 12:00:00+00:00  12
2020-05-06 13:00:00+00:00  13
2020-05-06 14:00:00+00:00  14
2020-05-06 15:00:00+00:00  15
2020-05-06 16:00:00+00:00  16
2020-05-06 17:00:00+00:00  17
2020-05-06 18:00:00+00:00  18
2020-05-06 19:00:00+00:00  19
2020-05-06 20:00:00+00:00  20
2020-05-06 21:00:00+00:00  21
2020-05-06 22:00:00+00:00  22
2020-05-06 23:00:00+00:00  23

例如,对于周期6和偏移量0,我希望:

2020-05-06 00:00:00+00:00  0
2020-05-06 06:00:00+00:00  6
2020-05-06 12:00:00+00:00  12
2020-05-06 18:00:00+00:00  18

对于周期4和偏移量2,我希望:

2020-05-06 02:00:00+00:00  2
2020-05-06 06:00:00+00:00  6
2020-05-06 10:00:00+00:00  10
2020-05-06 14:00:00+00:00  14
2020-05-06 18:00:00+00:00  18
2020-05-06 22:00:00+00:00  22

这就是我如何做到这一点,但这种方法是不灵活的,它不支持23后的偏移量

df.loc[[ind for ind in df.index if
                       (ind.hour == 0 + offset) |
                       (ind.hour == 6 + offset) |
                       (ind.hour == 12 + offset) |
                       (ind.hour == 18 + offset)
                       ]]

Tags: 数据方法dfforlocoffset偏移量period
3条回答

您可以使用以下选项:

print (s[(s.index.hour - offset)%period==0])
2020-05-06 00:00:00     0
2020-05-06 06:00:00     6
2020-05-06 12:00:00    12
2020-05-06 18:00:00    18
Freq: 6H, dtype: int64

和周期4和偏移量2

period = 4
offset = 2
print (s[(s.index.hour - offset)%period==0])
2020-05-06 02:00:00     2
2020-05-06 06:00:00     6
2020-05-06 10:00:00    10
2020-05-06 14:00:00    14
2020-05-06 18:00:00    18
2020-05-06 22:00:00    22
dtype: int64

不确定这是否是您所追求的:pandas date_range

如果小时数为6且无偏移:

#set the date as index
df = df.set_index(1)
#get the very first and last dates from the index :
start = df.index[0]
end = df.index[-1]

df.loc[pd.date_range(start=start,freq='6H',end=end)]

                            2
2020-05-06 00:00:00+00:00   0
2020-05-06 06:00:00+00:00   6
2020-05-06 12:00:00+00:00   12
2020-05-06 18:00:00+00:00   18

如果偏移量为2且小时数==4:

df.loc[pd.date_range(start=start + pd.offsets.Hour(2),freq='4H',end=end)]

                            2
2020-05-06 02:00:00+00:00   2
2020-05-06 06:00:00+00:00   6
2020-05-06 10:00:00+00:00   10
2020-05-06 14:00:00+00:00   14
2020-05-06 18:00:00+00:00   18
2020-05-06 22:00:00+00:00   22

我的列标签为1,2。(通过剪贴板读取数据时发生)

from datetime import date
# dummy data
df = pd.DataFrame({'num':np.arange(0,23+1),
                  'date':pd.date_range(date(2020,5,6),periods=23+1,freq='H')})
df = df.set_index('date')

df.loc[df.shift(offset).dropna()[::period].index]

相关问题 更多 >

    热门问题