mHelpMe
mHelpMe

Reputation: 6668

convert dataframe column which contains integers to dates

I have a dataframe which has a column of integers. These integers represent a date, for example todays date would be shown as 20200122.

I need to convert all the numbers in this column to a date, yyyy-mm-dd.

I tried the following,

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

However it doesn't return the date I want.

For example the number 20191114.0 (2019-11-14) is represented as 1970-01-01 00:00:00.020191114 when I use the line of code above.

Not sure what to try next?

Upvotes: 2

Views: 38

Answers (1)

jezrael
jezrael

Reputation: 863256

I think .0 after number should be no problem here if specify format %Y%m%d:

df = pd.DataFrame({'mycol':[20191114.0, 20191115.0]})
df['mycol'] = pd.to_datetime(df['mycol'], format='%Y%m%d')
print (df)
       mycol
0 2019-11-14
1 2019-11-15

Upvotes: 3

Related Questions