-2

I've got a column with some date-time data in a pandas data frame.

The format of the date-time data is '2016-10-04 07:59:42'.

I want to convert the date-time data to datetime data type.

I've tried using the below code, but I get a ValueError.

Thanks for any help!

# Library 
import pandas as pd 

# Data 
data = [['tom', '2016-10-04 07:59:42'], ['nick', '2016-10-04 07:59:42'], ['juli', '2016-10-04 07:59:42']] 
df = pd.DataFrame(data, columns = ['Name', 'Finished']) 

# Format column 
df['Finished'] = pd.to_datetime([f'{y}-{m}-{d}{hr}{mi}{se}' for y, m, d, hr, mi, se in df.Finished])
Robbie
  • 275
  • 4
  • 20
  • 3
    change to, ``pd.to_datetime(df.Finished, format="%Y-%m-%d %H:%M:%S")`` – sushanth Aug 02 '20 at 19:18
  • 2
    If the data comes from a csv then pandas will parse it for you https://pandas.pydata.org/docs/user_guide/io.html#datetime-handling – RichieV Aug 02 '20 at 19:22

1 Answers1

1

You can try the following:

df.Finished = pd.to_datetime(df.Finished, format="%Y-%m-%d %H:%M:%S")
ldren
  • 159
  • 5