Convert time object to datetime format in python pandas

14,976

You can use combine in list comprehension with zip:

df = pd.DataFrame({'DateTime': ['2011-01-01 12:48:20', '2014-01-01 12:30:45']})
df['DateTime'] = pd.to_datetime(df['DateTime'])

df['date'] = df['DateTime'].dt.date
df['time'] = df['DateTime'].dt.time

import datetime
df['new'] = [datetime.datetime.combine(a, b) for a, b in zip(df['date'], df['time'])]
print (df)

             DateTime        date      time                 new
0 2011-01-01 12:48:20  2011-01-01  12:48:20 2011-01-01 12:48:20
1 2014-01-01 12:30:45  2014-01-01  12:30:45 2014-01-01 12:30:45

Or convert to strings, join together and convert again:

df['new'] = pd.to_datetime(df['date'].astype(str) + ' ' +df['time'].astype(str))
print (df)
             DateTime        date      time                 new
0 2011-01-01 12:48:20  2011-01-01  12:48:20 2011-01-01 12:48:20
1 2014-01-01 12:30:45  2014-01-01  12:30:45 2014-01-01 12:30:45

But if use floor for remove times with converting times to timedeltas then use + only:

df['date'] = df['DateTime'].dt.floor('d')
df['time'] = pd.to_timedelta(df['DateTime'].dt.strftime('%H:%M:%S'))

df['new'] = df['date'] + df['time']
print (df)

             DateTime       date     time                 new
0 2011-01-01 12:48:20 2011-01-01 12:48:20 2011-01-01 12:48:20
1 2014-01-01 12:30:45 2014-01-01 12:30:45 2014-01-01 12:30:45
Share:
14,976
Nadeem Haque
Author by

Nadeem Haque

Updated on June 20, 2022

Comments

  • Nadeem Haque
    Nadeem Haque almost 2 years

    I have a dataset of column name DateTime having dtype object.

    df['DateTime'] = pd.to_datetime(df['DateTime'])
    

    I have used the above code to convert to datetime format then did a split in the column to have Date and Time separately

    df['date'] = df['DateTime'].dt.date
    df['time'] = df['DateTime'].dt.time
    

    but after the split the format changes to object type and while converting it to datetime it showing error for the time column name as: TypeError: is not convertible to datetime

    How to convert it to datetime format the time column