简体   繁体   中英

converting epoch column values to time in pandas dataframe

I have a column of epoch values in a dataframe and trying to convert them to date time. I've tried some solutions but can't seem to get them to work for me.

See example below. time.ctime() gives me the correct date and time for each value. When I try to apply it to the column I get cannot convert the series to <class 'int'>. Ensuring type int doesn't make a difference.

df = pd.DataFrame(data= {'epoch':[1599783600, 1600016400]})

import time
time.ctime(1599783600)
Out: 'Thu Sep 10 20:20:00 2020'
 
time.ctime(1600016400)
Out: 'Sun Sep 13 13:00:00 2020'

df['time3'] = time.ctime(df['epoch'])

TypeError: cannot convert the series to <class 'int'>

df['epoch'] = df['epoch'].astype(int)
df.dtypes
Out:
  epoch    int64
  dtype: object

I also tried the following that seems to work except the date time is not correct (dates are start times of NFL games).

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

from datetime import datetime, timedelta
start = datetime(1970, 1, 1)

df['time2'] = df.epoch.apply(lambda x: start + timedelta(seconds=x))

df

Out: 
        epoch                time               time2
0  1599783600 2020-09-11 00:20:00 2020-09-11 00:20:00
1  1600016400 2020-09-13 17:00:00 2020-09-13 17:00:00

Any guidance would be appreciated.

You need to account for timezone. It looks like these games started EST (you will need to source this somewhere):

df['time'] = pd.to_datetime(
    df['epoch'], unit='s', utc=True
).dt.tz_convert(tz="US/Eastern")

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