Search code examples
pythonpandasdatedatetimestrftime

.dt.strftime gives back the wrong dates Pandas


I have a pandas dataframe with a column CreatedDate in it. Currently the values of the column look like this:

id      CreatedDate
123     1586362930000
124     1586555550000

Desired output is:

id      CreatedDate
123     2020-04-08T15:50:00Z
124     2020-04-08T15:45:00Z

I have tried the following:

# Change the column type from int to datetime64[ns]
df['CreatedDate'] = pd.to_datetime(df['CreatedDate'])

new_df = df['CreatedDate'].dt.strftime("%Y-%m-%d"+"T"+"%H:%M:%S"+"Z")

The output is this:

id      CreatedDate
123     1970-01-01 00:26:26.362930
124     1970-01-01 00:26:26.365487

Which is not what I have expected, I know for a fact that those days should be April 8th. I have tested dt.strftime("%Y-%m-%d"+"T"+"%H:%M:%S"+"Z") with just a string and it returns the desired output, however, when I apply it to the dataframe it doesn't work properly


Solution

  • This is unix time

    pd.to_datetime(df.CreatedDate,unit='ms').dt.strftime("%Y-%m-%d""T""%H:%M:%S""Z")
    
    0    2020-04-08T16:22:10Z
    1    2020-04-10T21:52:30Z
    Name: CreatedDate, dtype: object