Convert column of epoch timestamps to datetime with timezone

146
April 01, 2019, at 12:40 PM

I have a dataframe in my Jupyter notebook with a column of epoch timestamps, it looks like this:

 Name    stop      ts            remark
 A       01        1546470653    -
 B       032       1546470969    Not listed
 C       022       1546471069    Not listed
 D       045       1546471238    Not listed

What I'm trying to do is to convert the epoch timestamp to human readable date & time with time zone (utc+8), the result should look like this:

 Name    stop      ts            remark       timestamp
 A       01        1546470653    -            2019-01-03T07:10:53+08:00
 B       032       1546470969    Not listed   2019-01-03T07:16:09+08:00
 C       022       1546471069    Not listed   2019-01-03T07:17:49+08:00
 D       045       1546471238    Not listed   2019-01-03T07:20:38+08:00

Can someone help me achieve this? Thanks a lot!

Answer 1

Use pd.to_datetime with unit='s'. You can then set the timezone using the tz_* methods.

df['timestamp'] = (pd.to_datetime(df['ts'], unit='s')
                     .dt.tz_localize('utc')
                     .dt.tz_convert('Asia/Hong_Kong'))    
df
  Name  stop          ts      remark                 timestamp
0    A     1  1546470653           - 2019-01-03 07:10:53+08:00
1    B    32  1546470969  Not listed 2019-01-03 07:16:09+08:00
2    C    22  1546471069  Not listed 2019-01-03 07:17:49+08:00
3    D    45  1546471238  Not listed 2019-01-03 07:20:38+08:00
READ ALSO
How to fix Value error reported when using optimize.minimize() but the function referenced has no error?

How to fix Value error reported when using optimize.minimize() but the function referenced has no error?

To get the optimal value of Theta in logistic regression I was using optimizeminimize() function, and my function costFunction(X,y,theta) returns the cost and gradient given the value of X,y and theta

138
How to join two seperate strings

How to join two seperate strings

I need to join two strings

153
Not showing image after put on server

Not showing image after put on server

basically i am creating a matching game, everything works fine when i test on my local machine, but when i put it onto server it is not displaying images

144
Unknown option -D Gjs

Unknown option -D Gjs

Gjs documentation says gjs -D -S examplejs will start a debugger but when I do the same follow error occurs

121