简体   繁体   中英

How to change an incorrect datetime format from a raw csv file to a proper datetime format in Python

I have a Python Dataframe like this

Index Name Dateofbirth

0      A.     12JAN1980:00:00:00.000000
1      B.   17JUN1954:00:00:00.000000
...
1250000  X.  09DEC1957:00:00:00.0000

The problem is that in the raw data csv file, my dates are stored in this format %d%m%Y:00:00:00.000000

So, the issue arises when I read this csv file into Python and convert Date of birth column into datetime with the following code

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

I get the following error :

raise ValueError("Unknown string format:", timestr) ValueError: ('Unknown string format:', '12JAN1980:00:00:00.000000

How can I change this format into the acceptable datetime format of %Y%m%d %H%M%S ? Changing the raw csv file is out of the question as there are over 1000000 rows.

Please help! I apologize for any lack of text formatting.

You can try this, it will return Dateofbirth with type object:

df['Dateofbirth'] = pd.to_datetime(df.Dateofbirth)
df['Dateofbirth'] = df['Dateofbirth'].dt.strftime('%Y%m%d %H%M%S')

or using this if you want Dateofbirth as type datetime:

df['Dateofbirth'] = pd.to_datetime(df['Dateofbirth'])
df['Dateofbirth'] = pd.to_datetime(df['Dateofbirth'].dt.strftime('%Y%m%d %H%M%S'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM