Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas date_range with only hours, minutes and seconds

Tags:

python

pandas

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.?

like image 846
Sreeram TP Avatar asked Sep 14 '18 09:09

Sreeram TP


People also ask

How do pandas deal with datetime?

Pandas has a built-in function called to_datetime()that converts date and time in string format to a DateTime object. As you can see, the 'date' column in the DataFrame is currently of a string-type object. Thus, to_datetime() converts the column to a series of the appropriate datetime64 dtype.

How do I compare Panda timestamps?

Comparison between pandas timestamp objects is carried out using simple comparison operators: >, <,==,< = , >=. The difference can be calculated using a simple '–' operator. Given time can be converted to pandas timestamp using pandas. Timestamp() method.

Does pandas support time series?

pandas contains extensive capabilities and features for working with time series data for all domains. Using the NumPy datetime64 and timedelta64 dtypes, pandas has consolidated a large number of features from other Python libraries like scikits.


1 Answers

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')
like image 138
Naga kiran Avatar answered Nov 15 '22 04:11

Naga kiran