Reputation: 1
I'm trying to convert a column in a dataframe to DateTime using data from a csv
DATE
9/7/2012
10/18/2019
10/2/2015
10/4/2015
4/25/2013
with this code
df["DATE"] = pd.to_datetime(df["DATE"], errors='coerce',format="%d/%m/%Y")
Only about half of the rows are successfully being converted to datetime, and the rest to NaT
It looks like this
DATE
2012-07-09
NaT
2015-02-10
2015-04-10
NaT
How do I fix this? Or could this be a problem with my with my data source?
Upvotes: 0
Views: 182
Reputation: 4618
Maybe this will work:
df["DATE"] = pd.to_datetime(df["DATE"], errors='coerce',format="%m/%d/%Y")
If the month is bigger than 12 pandas will return NaT, that's what happens when you run your code.
Upvotes: 1