Pandas 获取属于工作时间或工作日的日期时间戳。

问题描述 投票:0回答:1

了解所有 BusinesshoursBusinessDays 从给定的列表中选择。我关注了几个关于熊猫偏移的文档,但是没有弄明白。我也按照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])
python-3.x pandas
1个回答
1
投票

我想,你正在寻找类似的东西。

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]

所以..:

  • 两个第一 小时 没有改变(他们在营业时间内)。
  • 第三(2020-02-11 8:44:53)已推进到 9:00 (营业日开始)。
  • 第四(2020-02-02 13:44:53 日)已提前至次日(周一)在。9:00.

或者,如果你想 才查 粒子日期时间是否在工作时间内,运行。

y.apply(pd.Timestamp).apply(bh.onOffset)

结果是:

0     True
1     True
2    False
3    False
Name: hours, dtype: bool

意思是最后两个日期时间是 外面 工作时间。

© www.soinside.com 2019 - 2024. All rights reserved.