I have a pandas dataframe with UNIX timestamps (these are integers and not time objects). I'd like to convert the UNIX timestamps into local time (according to China timezone). So, based on this, I tried to do the following:
import pandas as pd
data = {'timestamp': [1540651297, 1540651300, 1540651303, 1540651306, 1540651309, 1540651312]}
df = pd.DataFrame (data, columns = ['timestamp'])
df
df['timestamp1'] = pd.to_datetime(df.timestamp, unit='s')
df['timestamp2']=df['timestamp'].apply(lambda d: datetime.datetime.fromtimestamp(int(d)).strftime('%Y-%m-%d %H:%M:%S'))
df['timestamp3'] = df['timestamp1'].dt.tz_localize('Asia/Shanghai').dt.tz_convert('UTC')
| timestamp | timestamp1 | timestamp2 | timestamp3 |
|---|---|---|---|
| 1540651297 | 2018-10-27 14:41:37 | 2018-10-27 22:41:37 | 2018-10-27 06:41:37+00:00 |
| 1540651300 | 2018-10-27 14:41:40 | 2018-10-27 22:41:40 | 2018-10-27 06:41:40+00:00 |
| 1540651303 | 2018-10-27 14:41:43 | 2018-10-27 22:41:43 | 2018-10-27 06:41:43+00:00 |
| 1540651306 | 2018-10-27 14:41:46 | 2018-10-27 22:41:46 | 2018-10-27 06:41:46+00:00 |
| 1540651309 | 2018-10-27 14:41:49 | 2018-10-27 22:41:49 | 2018-10-27 06:41:49+00:00 |