Tobitor
Tobitor

Reputation: 1508

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

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?

Upvotes: 0

Views: 28

Answers (1)

absoup
absoup

Reputation: 446

Try this:

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

Upvotes: 1

Related Questions