[英]Pandas datetime index ceil to specific hour in day
I have a datetime index which I would like to round up(ceil) to a specific hour in the day.我有一个日期时间索引,我想将其四舍五入(ceil)到一天中的特定时间。 I am already aware of pandas' offset aliases and how they work, but specifically I would like to tell it to round the datetime to a specific hour in the day(or a specific day in the month).我已经知道 pandas 的偏移别名及其工作方式,但具体来说,我想告诉它将日期时间四舍五入到一天中的特定时间(或一个月中的特定日期)。 For example I would like to have this kind of transformation:例如,我想进行这种转换:
print(results.index)
DatetimeIndex(['2018-12-14 05:00:00+01:00', '2018-12-14 06:00:00+01:00',
'2018-12-14 07:00:00+01:00', '2018-12-14 08:00:00+01:00',
'2018-12-14 09:00:00+01:00', '2018-12-14 10:00:00+01:00',
'2018-12-14 11:00:00+01:00', '2018-12-14 12:00:00+01:00',
'2018-12-14 13:00:00+01:00', '2018-12-14 14:00:00+01:00',
Turns into变成
DatetimeIndex(['2018-12-14 08:00:00+01:00', '2018-12-14 08:00:00+01:00',
'2018-12-14 08:00:00+01:00', '2018-12-14 08:00:00+01:00',
'2018-12-15 08:00:00+01:00', '2018-12-15 08:00:00+01:00',
'2018-12-15 08:00:00+01:00', '2018-12-15 08:00:00+01:00',
'2018-12-15 08:00:00+01:00', '2018-12-15 08:00:00+01:00',
As far as I'm aware, there does not exist such a parameter that we can pass to ceil() that would allow this, since we can only round to nearest hour, day, month(freq='H', 'D', 'M')... Is there an elegant solution to this or would I have to code my own for loop?据我所知,不存在我们可以传递给 ceil() 的参数,因为我们只能四舍五入到最近的小时、天、月(freq='H', 'D' , 'M')... 对此是否有优雅的解决方案,或者我是否必须编写自己的 for 循环代码?
One idea is use numpy.where
and offsets.DateOffset
- here hour
without s
means set values to 8
, day
with s
means add one day to original days:一个想法是使用numpy.where
和offsets.DateOffset
- 这里没有s
hour
意味着将值设置为8
,带s
day
意味着在原始日期基础上增加一天:
d = pd.DatetimeIndex(['2018-12-14 05:00:00+01:00', '2018-12-14 06:00:00+01:00',
'2018-12-14 07:00:00+01:00', '2018-12-14 08:00:00+01:00',
'2018-12-14 09:00:00+01:00', '2018-12-14 10:00:00+01:00',
'2018-12-14 11:00:00+01:00', '2018-12-14 12:00:00+01:00',
'2018-12-14 13:00:00+01:00', '2018-12-14 14:00:00+01:00'])
results = pd.DataFrame(index=d)
out = np.where(results.index.hour <= 8,
results.index + pd.offsets.DateOffset(hour=8),
results.index + pd.offsets.DateOffset(days=1, hour=8))
print (pd.DatetimeIndex(out))
DatetimeIndex(['2018-12-14 08:00:00+01:00', '2018-12-14 08:00:00+01:00',
'2018-12-14 08:00:00+01:00', '2018-12-14 08:00:00+01:00',
'2018-12-15 08:00:00+01:00', '2018-12-15 08:00:00+01:00',
'2018-12-15 08:00:00+01:00', '2018-12-15 08:00:00+01:00',
'2018-12-15 08:00:00+01:00', '2018-12-15 08:00:00+01:00'],
dtype='datetime64[ns, pytz.FixedOffset(60)]', freq=None)
Another idea is use Timedelta
s and add day only if condition is True
:另一个想法是使用Timedelta
s 并仅在条件为True
时添加日期:
m = results.index.hour > 8
out = results.index + pd.offsets.DateOffset(hour=8) + pd.Timedelta(days=1) * m
print (out)
DatetimeIndex(['2018-12-14 08:00:00+01:00', '2018-12-14 08:00:00+01:00',
'2018-12-14 08:00:00+01:00', '2018-12-14 08:00:00+01:00',
'2018-12-15 08:00:00+01:00', '2018-12-15 08:00:00+01:00',
'2018-12-15 08:00:00+01:00', '2018-12-15 08:00:00+01:00',
'2018-12-15 08:00:00+01:00', '2018-12-15 08:00:00+01:00'],
dtype='datetime64[ns, pytz.FixedOffset(60)]', freq=None)
m = results.index.hour > 8
out = results.index.floor('d') + pd.Timedelta(hours=8) + pd.Timedelta(days=1) * m
print (out)
DatetimeIndex(['2018-12-14 08:00:00+01:00', '2018-12-14 08:00:00+01:00',
'2018-12-14 08:00:00+01:00', '2018-12-14 08:00:00+01:00',
'2018-12-15 08:00:00+01:00', '2018-12-15 08:00:00+01:00',
'2018-12-15 08:00:00+01:00', '2018-12-15 08:00:00+01:00',
'2018-12-15 08:00:00+01:00', '2018-12-15 08:00:00+01:00'],
dtype='datetime64[ns, pytz.FixedOffset(60)]', freq=None)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.