gpt4 book ai didi

python - 两个 pd.Timestamp 对象之间每天上午 9 点的 pd.date_range

转载 作者:行者123 更新时间:2023-12-04 07:24:57 25 4
gpt4 key购买 nike

我拥有的:

pd.Timestamp('2021-07-05 08:10:11')
pd.Timestamp('2021-07-07 12:13:14')
我在找什么:
[pd.Timestamp('2021-07-05 09:00:00'), 
pd.Timestamp('2021-07-06 09:00:00'),
pd.Timestamp('2021-07-07 09:00:00')]
感觉我应该可以通过 pd.date_range 的一些组合来做到这一点。和 pd.offsets ,但我找不到正确的组合。
鉴于上午 9 点是标准的气象时间点,我希望有人已经有一个很好的解决方案!

最佳答案

方法一
我们可以使用 datetime.replacepd.date_range和频率D :

start = pd.Timestamp('2021-07-05 08:10:11').replace(hour=9, minute=0, second=0)
end = pd.Timestamp('2021-07-07 12:13:14').replace(hour=9, minute=0, second=0)

pd.date_range(start, end, freq='D')
DatetimeIndex(['2021-07-05 09:00:00', '2021-07-06 09:00:00',
'2021-07-07 09:00:00'],
dtype='datetime64[ns]', freq='D')

方法二:使用 pd.offsets.BusinessHour
我们可以将偏移量传递给 pd.date_range在这种情况下,我们可以使用 BusinessHour因为它默认使用 09:00 .
start = pd.Timestamp('2021-07-05 08:10:11')
end = pd.Timestamp('2021-07-07 12:13:14')

dates = pd.date_range(start, end, freq=pd.offsets.BusinessHour()).to_series()
dates.groupby(dates.dt.date).min()
2021-07-05   2021-07-05 09:00:00
2021-07-06 2021-07-06 09:00:00
2021-07-07 2021-07-07 09:00:00
dtype: datetime64[ns]
要设置另一个开始时间,我们可以设置 start参数在 pd.offsets.BusinessHour

关于python - 两个 pd.Timestamp 对象之间每天上午 9 点的 pd.date_range,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68267555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com