简体   繁体   English

Pandas date_range只有小时,分钟和秒

[英]Pandas date_range with only hours, minutes and seconds

I want a list of timestamps ranging from 00:00:00 to 23:45:00 using pandas date_range . 我想时间戳从列表00:00:0023:45:00使用pandas date_range

I tried like this 我试过这样的

pd.date_range(start=pd.Timestamp('00:00:00'), end=pd.Timestamp('23:45:00'), freq='15T')

Even though I haven't provided the Year, Month and Date, the output I got is like this 即使我没有提供年,月和日,我得到的输出是这样的

DatetimeIndex(['2018-09-14 00:00:00', '2018-09-14 00:15:00',
               '2018-09-14 00:30:00', '2018-09-14 00:45:00',
               '2018-09-14 01:00:00', '2018-09-14 01:15:00',
               '2018-09-14 01:30:00', '2018-09-14 01:45:00',
               '2018-09-14 02:00:00', '2018-09-14 02:15:00',
               '2018-09-14 02:30:00', '2018-09-14 02:45:00',
               '2018-09-14 03:00:00', '2018-09-14 03:15:00',
               '2018-09-14 03:30:00', '2018-09-14 03:45:00',
               '2018-09-14 04:00:00', '2018-09-14 04:15:00',
               '2018-09-14 04:30:00', '2018-09-14 04:45:00',
               '2018-09-14 05:00:00', '2018-09-14 05:15:00',
               '2018-09-14 05:30:00', '2018-09-14 05:45:00',
               '2018-09-14 06:00:00', '2018-09-14 06:15:00',
               '2018-09-14 06:30:00', '2018-09-14 06:45:00',
               '2018-09-14 07:00:00', '2018-09-14 07:15:00',
               '2018-09-14 07:30:00', '2018-09-14 07:45:00',
               '2018-09-14 08:00:00', '2018-09-14 08:15:00',
               '2018-09-14 08:30:00', '2018-09-14 08:45:00',
               '2018-09-14 09:00:00', '2018-09-14 09:15:00',
               '2018-09-14 09:30:00', '2018-09-14 09:45:00',
               '2018-09-14 10:00:00', '2018-09-14 10:15:00',
               '2018-09-14 10:30:00', '2018-09-14 10:45:00',
               '2018-09-14 11:00:00', '2018-09-14 11:15:00',
               '2018-09-14 11:30:00', '2018-09-14 11:45:00',
               '2018-09-14 12:00:00', '2018-09-14 12:15:00',
               '2018-09-14 12:30:00', '2018-09-14 12:45:00',
               '2018-09-14 13:00:00', '2018-09-14 13:15:00',
               '2018-09-14 13:30:00', '2018-09-14 13:45:00',
               '2018-09-14 14:00:00', '2018-09-14 14:15:00',
               '2018-09-14 14:30:00', '2018-09-14 14:45:00',
               '2018-09-14 15:00:00', '2018-09-14 15:15:00',
               '2018-09-14 15:30:00', '2018-09-14 15:45:00',
               '2018-09-14 16:00:00', '2018-09-14 16:15:00',
               '2018-09-14 16:30:00', '2018-09-14 16:45:00',
               '2018-09-14 17:00:00', '2018-09-14 17:15:00',
               '2018-09-14 17:30:00', '2018-09-14 17:45:00',
               '2018-09-14 18:00:00', '2018-09-14 18:15:00',
               '2018-09-14 18:30:00', '2018-09-14 18:45:00',
               '2018-09-14 19:00:00', '2018-09-14 19:15:00',
               '2018-09-14 19:30:00', '2018-09-14 19:45:00',
               '2018-09-14 20:00:00', '2018-09-14 20:15:00',
               '2018-09-14 20:30:00', '2018-09-14 20:45:00',
               '2018-09-14 21:00:00', '2018-09-14 21:15:00',
               '2018-09-14 21:30:00', '2018-09-14 21:45:00',
               '2018-09-14 22:00:00', '2018-09-14 22:15:00',
               '2018-09-14 22:30:00', '2018-09-14 22:45:00',
               '2018-09-14 23:00:00', '2018-09-14 23:15:00',
               '2018-09-14 23:30:00', '2018-09-14 23:45:00'],
              dtype='datetime64[ns]', freq='15T')

I know I can strip the needed Hour, Minute and Second value from this. 我知道我可以从中删除所需的小时,分​​钟和秒值。 But I am wondering is there are direct way for this. 但我想知道是否有直接的方法。

Can this be done in pandas.? 这可以在熊猫中完成吗?

You can extract your required form of time from timestamp with 'strftime' Function 您可以使用'strftime'功能从时间戳中提取所需的时间形式

pd.date_range("11:00", "21:30", freq="30min").strftime('%H:%M:%S')

Out: 日期:

array(['11:00:00', '11:30:00', '12:00:00', '12:30:00', '13:00:00',
       '13:30:00', '14:00:00', '14:30:00', '15:00:00', '15:30:00',
       '16:00:00', '16:30:00', '17:00:00', '17:30:00', '18:00:00',
       '18:30:00', '19:00:00', '19:30:00', '20:00:00', '20:30:00',
       '21:00:00', '21:30:00'], dtype='<U8')

Since pandas.date_range gives you, well a range of dates, and there is no pandas.time_range , I think you are left with not many options. 由于pandas.date_range为您提供了一系列日期,而且没有pandas.time_range ,我认为您没有多少选择。

The easiest way is to just take the time components from the dates: 最简单的方法是从日期中获取时间组件:

>>> r = pd.date_range(start=pd.Timestamp('00:00:00'), end=pd.Timestamp('23:45:00'), freq='15T')
>>> r.time
array([datetime.time(0, 0), datetime.time(0, 15), datetime.time(0, 30),
       ...
       datetime.time(23, 15), datetime.time(23, 30), datetime.time(23, 45)], dtype=object)

This returns a numpy array of datetime.time objects. 这将返回一个numpy数组的datetime.time对象。 You can then do with that whatever you want. 然后你可以随心所欲地做到这一点。 If you just want their string representations, the easiest way is probably to use the built-in map : 如果您只想要他们的字符串表示,最简单的方法可能是使用内置map

>>> list(map(str, pd.date_range(start=pd.Timestamp('00:00:00'), end=pd.Timestamp('23:45:00'), freq='15T').time))
['00:00:00',
 '00:15:00',
 ...
 '23:45:00']

List comprehension? 列表理解? I don't know if this is direct as you want 我不知道这是否是你想要的直接

[date.strftime('%H:%M:%S') for date in pd.date_range(... )]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM