Reputation: 11937
I want a list of timestamps ranging from 00:00:00
to 23:45:00
using 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.?
Upvotes: 9
Views: 19211
Reputation: 7206
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.
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. 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
:
>>> 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']
Upvotes: 6
Reputation: 765
List comprehension? I don't know if this is direct as you want
[date.strftime('%H:%M:%S') for date in pd.date_range(... )]
Upvotes: 1
Reputation: 4607
You can extract your required form of time from timestamp with 'strftime' Function
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')
Upvotes: 18