Search code examples
pythonpandasdatedatetimeint64

Pandas dataframe change date from int64 to date in datetime


I have a dataframe where the date column type is int64. I want to convert that column and its values to datetime.

df['datdadat'] = pd.to_datetime(df['datadate'].astype('str'), errors='coerce')

conversion with my code in the last column

As you can see, my code fails when encountering the second row without leading zeroes for the months/days. I tried adding a format already, but when I add it in, even the first one with zeroes fails.

My question would be how to add in the leading zeroes before conversion, or if there is a good way to add a format in while converting it? Thx in advance


Solution

  • Try this:

    df['datdadat'] = pd.to_datetime(df['datdadat'], format='%d%m%y')
    print(df)
    
           datdadat
    0 1984-01-30
    1 1983-12-30