edesz
edesz

Reputation: 12406

Python/Pandas convert string to time only

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

Upvotes: 47

Views: 115813

Answers (6)

yeiichi
yeiichi

Reputation: 178

(Tested with Python 3.10.9/pandas 1.5.3)
You can apply pd.to_datetime() and datetime.time to the Series with the apply() function and the dt accessor: dfc['Time_of_Sail'].apply(pd.to_datetime).dt.time.

Recap

import pandas as pd

# Original DataFrame
>>> dfc = pd.DataFrame(
>>>     {
>>>         'Temp_Reading': [1, 2, 3, 4, 5],
>>>         'Time_of_Sail': ['11:33:11', '16:29:05', '09:37:56', '21:43:31', '17:42:06']
>>>     }
>>> )

# Convert to datetime.time object
>>> dfc['Time_of_Sail'] = dfc['Time_of_Sail'].apply(pd.to_datetime).dt.time
>>> dfc['Time_of_Sail']
    0    11:33:11
    1    16:29:05
    2    09:37:56
    3    21:43:31
    4    17:42:06
    Name: Time_of_Sail, dtype: object

Data type
Indeed dfc.dtypes returns Time_of_Sail object, but you'll see they are datetime.time objects at a closer look:

>>> from pprint import pprint
>>> pprint([i for i in dfc['Time_of_Sail']])
[datetime.time(11, 33, 11),
 datetime.time(16, 29, 5),
 datetime.time(9, 37, 56),
 datetime.time(21, 43, 31),
 datetime.time(17, 42, 6)]

Upvotes: 0

Achintha Isuru
Achintha Isuru

Reputation: 3347

If anyone is searching for a more generalized answer try

dfc['Time_of_Sail']= pd.to_datetime(dfc['Time_of_Sail'])

Upvotes: 1

ferengi
ferengi

Reputation: 477

This seems to work:

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

Upvotes: 2

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')

Upvotes: 14

Merlin
Merlin

Reputation: 25699

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

Upvotes: 77

Moe Chughtai
Moe Chughtai

Reputation: 384

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))

Upvotes: 0

Related Questions