Monday, January 31, 2022

[SOLVED] Converting data to actual timestamp - how to add the correct unit?

Issue

I have a pandas dataframe with such a timestamp:

1615860000

This is: Tue Mar 16 2021 02:00:00 GMT+0000

However, when I convert it to datetime using this code:

df['time'] = pd.to_datetime(df['time'],utc=True).dt.tz_localize(None)
df.set_index('time', inplace=True)

I get a timestamp of somewhen on the 1st of January 1970. I think, this is due to an incorrect unit. How can I amend my code to get the actual date?


Solution

Try this:

df['time'] = pd.to_datetime(df['time'], unit='s')


Answered By - absoup
Answer Checked By - Cary Denson (WPSolving Admin)