3 回答

TA貢獻2011條經(jīng)驗 獲得超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條經(jīng)驗 獲得超1個贊
不確定這是否是您的目標:pandas date_range
如果小時數(shù)是 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 且小時數(shù) == 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。(通過剪貼板讀取數(shù)據(jù)時發(fā)生)

TA貢獻1802條經(jīng)驗 獲得超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
添加回答
舉報