i'mgnome
i'mgnome

Reputation: 545

I keep getting a date format error when trying to convert an int into a date

So I currently have a dataset that has a date column as an int, for example 102809 as 10/28/2009. So I'm trying to convert this column into an actual date column that would have the correct date format.

I tried this code from another question on stack overflow:

curry['Datetime'] = curry['Date'].apply(lambda x: pd.to_datetime(str(x), format='%m/%d/%Y'))

But this line returns this error:time data '102809' does not match format '%m/%d/%Y' (match)

Upvotes: 0

Views: 102

Answers (2)

Steven Rumbalski
Steven Rumbalski

Reputation: 45552

It looks like your date format is two-digits each for month, day and year, so your format should be '%m%d%y' with no slashes. (A two-digit year is %y not %Y.) Also, there is no need to use df.apply() which is slower.

curry['Datetime'] = pd.to_datetime(curry['Date'], format='%m%d%y')

Upvotes: 1

gtomer
gtomer

Reputation: 6574

Change it to :

curry['Datetime'] = curry['Date'].apply(lambda x: pd.to_datetime(str(x), format='%m%d%y'))

I have removed the slashes and change the %Y to lower case -> %y, because it is only two digits year

Upvotes: 1

Related Questions