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

Time:08-09

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)

CodePudding user response:

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

CodePudding user response:

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')
  • Related