了解所有 Businesshours
和 BusinessDays
从给定的列表中选择。我关注了几个关于熊猫偏移的文档,但是没有弄明白。我也按照stackoverflow的要求去做了,这里有类似的内容。 但没有成功。
>>> d = {'hours': ['2020-02-11 13:44:53', '2020-02-12 13:44:53', '2020-02-11 8:44:53', '2020-02-02 13:44:53']}
>>> df = pd.DataFrame(d)
>>> df
hours
0 2020-02-11 13:44:53
1 2020-02-12 13:44:53
2 2020-02-11 8:44:53
3 2020-02-02 13:44:53
>>> y = df['hours']
>>> from pandas.tseries.offsets import *
>>> y.apply(pd.Timestamp).asfreq(BDay())
1970-01-01 NaT
Freq: B, Name: hours, dtype: datetime64[ns]
>>> y.apply(pd.Timestamp).asfreq(BusinessHour())
Series([], Freq: BH, Name: hours, dtype: datetime64[ns])
我想,你正在寻找类似的东西。
bh = pd.offsets.BusinessHour() # avoid not necessary imports
y.apply(pd.Timestamp).apply(bh.rollforward)
结果是:
0 2020-02-11 13:44:53
1 2020-02-12 13:44:53
2 2020-02-11 09:00:00
3 2020-02-03 09:00:00
Name: hours, dtype: datetime64[ns]
所以..:
或者,如果你想 才查 粒子日期时间是否在工作时间内,运行。
y.apply(pd.Timestamp).apply(bh.onOffset)
结果是:
0 True
1 True
2 False
3 False
Name: hours, dtype: bool
意思是最后两个日期时间是 外面 工作时间。