Python/Pandas convert string to time only

Question:

I have the following Pandas dataframe in Python 2.7.

import pandas as pd
trial_num = [1,2,3,4,5]
sail_rem_time = ['11:33:11','16:29:05','09:37:56','21:43:31','17:42:06']
dfc = pd.DataFrame(zip(*[trial_num,sail_rem_time]),columns=['Temp_Reading','Time_of_Sail'])
print dfc

The dataframe looks like this:

  Temp_Reading Time_of_Sail
             1     11:33:11
             2     16:29:05
             3     09:37:56
             4     21:43:31
             5     17:42:06

This dataframe comes from a *.csv file. I use Pandas to read in the *.csv file as a Pandas dataframe. When I use print dfc.dtypes, it shows me that the column Time_of_Sail has a datatype object. I would like to convert this column to datetime datatype BUT I only want the time part – I don’t want the year, month, date.

I can try this:

dfc['Time_of_Sail'] = pd.to_datetime(dfc['Time_of_Sail'])
dfc['Time_of_Sail'] = [time.time() for time in dfc['Time_of_Sail']]

but the problem is that the when I run print dfc.dtypes it still shows that the column Time_of_Sail is object.

Is there a way to convert this column into a datetime format that only has the time?

Additional Information:

To create the above dataframe and output, this also works:

import pandas as pd
trial_num = [1,2,3,4,5]
sail_rem_time = ['11:33:11','16:29:05','09:37:56','21:43:31','17:42:06']
data = [
    [trial_num[0],sail_rem_time[0]],
    [trial_num[1],sail_rem_time[1]],[trial_num[2],sail_rem_time[2]],
    [trial_num[3],sail_rem_time[3]]
    ]
dfc = pd.DataFrame(data,columns=['Temp_Reading','Time_of_Sail'])
dfc['Time_of_Sail'] = pd.to_datetime(dfc['Time_of_Sail'])
dfc['Time_of_Sail'] = [time.time() for time in dfc['Time_of_Sail']]
print dfc
print dfc.dtypes
Asked By: edesz

||

Answers:

If you just want a simple conversion you can do the below:

import datetime as dt

dfc.Time_of_Sail = dfc.Time_of_Sail.astype(dt.datetime)

or you could add a holder string to your time column as below, and then convert afterwards using an apply function:

dfc.Time_of_Sail = dfc.Time_of_Sail.apply(lambda x: '2016-01-01 ' + str(x))
dfc.Time_of_Sail = pd.to_datetime(dfc.Time_of_Sail).apply(lambda x: dt.datetime.time(x))
Answered By: Moe Chughtai

These two lines:

dfc['Time_of_Sail'] = pd.to_datetime(dfc['Time_of_Sail'])
dfc['Time_of_Sail'] = [time.time() for time in dfc['Time_of_Sail']]

Can be written as:

dfc['Time_of_Sail'] = pd.to_datetime(dfc['Time_of_Sail'],format= '%H:%M:%S' ).dt.time
Answered By: Merlin

Using to_timedelta,we can convert string to time format(timedelta64[ns]) by specifying units as second,min etc.,

dfc['Time_of_Sail'] = pd.to_timedelta(dfc['Time_of_Sail'], unit='s')

This seems to work:

dfc['Time_of_Sail'] = pd.to_datetime(dfc['Time_of_Sail'], format='%H:%M:%S' ).apply(pd.Timestamp)

Answered By: ferengi

If anyone is searching for a more generalized answer try

dfc['Time_of_Sail']= pd.to_datetime(dfc['Time_of_Sail'])
Answered By: Achintha Isuru
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.