How to extract only time from pandas DatetimeIndex while maintaining the 'datetime64[ns]' dtype?

Viewed 60

I have generated a DatetimeIndex which looks like:

DatetimeIndex(['1970-01-01 09:30:00.015105074',
               '1970-01-01 09:30:00.059901970',
               '1970-01-01 09:30:00.113246707',
               '1970-01-01 09:30:00.113246707',
               '1970-01-01 09:30:00.113246707',
               '1970-01-01 09:30:00.113246707',
               '1970-01-01 09:30:00.113246707',
               '1970-01-01 09:30:00.154178213',
               '1970-01-01 09:30:00.173594287',
               '1970-01-01 09:30:00.202322801',
               ...
               '1970-01-01 15:59:59.544086847',
               '1970-01-01 15:59:59.544121155',
               '1970-01-01 15:59:59.544124809',
               '1970-01-01 15:59:59.544125669',
               '1970-01-01 15:59:59.544126313',
               '1970-01-01 15:59:59.544129843',
               '1970-01-01 15:59:59.544131783',
               '1970-01-01 15:59:59.544132627',
               '1970-01-01 15:59:59.544133264',
               '1970-01-01 15:59:59.871751084'],
              dtype='datetime64[ns]', name=0, length=112673, freq=None)

This has been generated using the code:

GOOG_msg_df = pd.read_csv('GOOG_msg_5.csv', header = None, index_col = 0)
pd.to_datetime(GOOG_msg_df.index, unit = 's')

I wish to extract only the time component (leave the date out). I tried the following:

pd.Series(pd.to_datetime(GOOG_msg_df.index, unit = 's').time)

and I get:

0         09:30:00.015105
1         09:30:00.059901
2         09:30:00.113246
3         09:30:00.113246
4         09:30:00.113246
               ...       
112668    15:59:59.544129
112669    15:59:59.544131
112670    15:59:59.544132
112671    15:59:59.544133
112672    15:59:59.871751
Length: 112673, dtype: object

The issue with this method is that the dtype is object instead of datetime64[ns].

Is there a way to extract only the time component while maintaining the datetime64[ns] dtype? This will allow me to perform operations that rely on this dtype. For e.g. :

pd.to_datetime(GOOG_msg_df.index, unit = 's') > pd.Timestamp('1970-01-01 10:00:00')
>>> array([False, False, False, ...,  True,  True,  True])
1 Answers

Create timedeltas instead time column if need later processing it:

s = pd.Series(pd.to_timedelta((pd.to_datetime(GOOG_msg_df.index, unit = 's').time).astype(str)))
Related