简体   繁体   中英

convert unix timestamp string to readable date using pandas libraries

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

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM