3 回答
TA贡献2011条经验 获得超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]
TA贡献1780条经验 获得超1个赞
不确定这是否是您的目标: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。(通过剪贴板读取数据时发生)
TA贡献1802条经验 获得超5个赞
你可以使用这个:
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
添加回答
举报