[英]Python pandas date_range with sampling assigned to each day separately
I want to create date range between specific dates with a certain sampling within each day, but each day should start at midnight:我想在特定日期之间创建日期范围,每天进行一定的抽样,但每一天都应该从午夜开始:
['2017-01-01 00:00:00', '2017-01-01 05:00:00','2017-01-01 10:00:00', '2017-01-01 15:00:00', '2017-01-01 20:00:00', '2017-01-02 00:00:00', '2017-01-02 05:00:00' ...]
Of course, in default pd.date_range(date1,date2,freq='5H') gives the output:当然,默认情况下 pd.date_range(date1,date2,freq='5H') 给出 output:
['2017-01-01 00:00:00', '2017-01-01 05:00:00', '2017-01-01 10:00:00', '2017-01-01 15:00:00', '2017-01-01 20:00:00', '2017-01-02 01:00:00', '2017-01-02 06:00:00', ..]
Any idea of how to handle this issue?知道如何处理这个问题吗? Thank you!
谢谢!
Create hours range and then filter by modulo 5 by hours in indexing:创建小时范围,然后在索引中按小时对模 5 进行过滤:
date1= '2017-01-01'
date2= '2017-01-04'
r = pd.date_range(date1,date2,freq='H')
r = r[r.hour % 5 == 0]
print (r)
DatetimeIndex(['2017-01-01 00:00:00', '2017-01-01 05:00:00',
'2017-01-01 10:00:00', '2017-01-01 15:00:00',
'2017-01-01 20:00:00', '2017-01-02 00:00:00',
'2017-01-02 05:00:00', '2017-01-02 10:00:00',
'2017-01-02 15:00:00', '2017-01-02 20:00:00',
'2017-01-03 00:00:00', '2017-01-03 05:00:00',
'2017-01-03 10:00:00', '2017-01-03 15:00:00',
'2017-01-03 20:00:00', '2017-01-04 00:00:00'],
dtype='datetime64[ns]', freq=None)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.