Neil
Neil

Reputation: 8247

class datetime.time is not convertible to datetime in pandas

I am new to Python coming from R background,I have following time column in pandas

   time
   09:12:23
   09:34:33
   10:23:22
   11:23:33

I want to convert this into time object of pandas,I am doing following in python

     df['time'] = pd.to_datetime(df['time']).dt.time

Why its showing me following error.

   class datetime.time is not convertible to datetime

Upvotes: 8

Views: 11680

Answers (1)

jezrael
jezrael

Reputation: 863481

If possible same non datetime/time values add parameter errors='coerce':

print (df)
       time
0     aaaaa
1  09:34:33
2  10:23:22
3  11:23:33

df['time'] = pd.to_datetime(df['time'], errors='coerce').dt.time
print (df)
       time
0       NaT
1  09:34:33
2  10:23:22
3  11:23:33

EDIT: For check this values:

print (df[pd.to_datetime(df['time'], errors='coerce').isnull()])
    time
0  sdass

EDIT1:

Here is possible convert to timedeltas by to_timedelta - advantage is using pandas timedelta functions, which not working with times:

df['time'] = pd.to_timedelta(df['time'], errors='coerce')
print (df)
      time
0      NaT
1 09:34:33
2 10:23:22
3 11:23:33

Upvotes: 5

Related Questions