convert unix timestamp string to readable date using pandas libraries

Viewed 78
unix_timestamp=1284105682
print(pd.to_datetime(unix_timestamp,unit='s',origin='unix'))

Expected Output : 2010-09-10 13:31:22 My Output: 2010-09-10 08:01:22

I am unable to figure out how time is getting wrong

2 Answers

It seems to be a UTC-related issue. You can create the datetime with UTC and then change it to your time-zone like this:

import pandas as pd
unix_timestamp=1284105682
df = pd.to_datetime(unix_timestamp, unit='s', origin='unix', utc=True)
df.tz_convert('America/Sao_Paulo')
unix_timestamp=1284105682
pd.to_datetime(unix_timestamp,unit='s', utc=True).tz_convert('Asia/Kolkata')

Output

Timestamp('2010-09-10 13:31:22+0530', tz='Asia/Kolkata')
Related