简体   繁体   中英

pandas df column to date

I have a pandas df column that has a 10 digit timestamp? it is a type('O') and I am trying to get it into datetime format of mm-dd-yyyy

date
1534636800
1534723200
1534809600
1534896000

i tried

all_data['datetime'] = pd.to_datetime(all_data['date'], format='%Y-%m-%d %H:%M:%S')

which returned

datetime
1970-01-01 00:00:01.534636800
1970-01-01 00:00:01.534723200
1970-01-01 00:00:01.534809600
1970-01-01 00:00:01.534896000

This is obviously incorrect.

I also tried all_data['datetime'] = datetime.fromtimestamp(all_data['date'].astype(int)) and all_data['datetime'] = pd.datetime(all_data['date'].astype(int))

Both return errors "TypeError: cannot convert the series to <class 'int'>"

Input

df = pd.DataFrame({
    'date':[
1534636800,
1534723200,
1534809600,
1534896000,]
})
df

Code

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

Output

    date
0   2018-08-19
1   2018-08-20
2   2018-08-21
3   2018-08-22

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