【问题标题】:convert epoch to readable data time in python在python中将纪元转换为可读数据时间
【发布时间】:2019-01-24 20:20:57
【问题描述】:

我有一个数据框,其中一列处于纪元时间:

df1['timestamp'] 

是这样的:

1533009600000
1533013200000  
1533016800000 



timestamp    object

我需要转换为人类可读的时间戳。我试过这个:

import time
df1['timestamp']=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(df1['timestamp']))

我收到此错误:

TypeError: cannot convert the series to <type 'float'>

我在这里做错了什么?

【问题讨论】:

  • @Prune,我正在关注日期时间转换,但我遇到了一些奇怪的错误。这真的不是同一类型的问题。
  • 我很抱歉;感谢您的额外关注。问题出在 Pandas 处理中。原始列的类型为float,您正在即时更改它。您需要正确的 astype() 转换来执行此操作。
  • 时间戳的单位是什么?

标签: python pandas


【解决方案1】:

您可以根据纪元时间的单位进行转换。这个数字对于s来说太大了,假设是ms

df['timestamp'] = pd.to_datetime(df['timestamp'], unit = 'ms')


    timestamp
0   2018-07-31 04:00:00
1   2018-07-31 05:00:00
2   2018-07-31 06:00:00

【讨论】:

    猜你喜欢
    • 2021-03-30
    • 2012-01-30
    • 2012-08-16
    • 1970-01-01
    • 2014-12-04
    • 2013-01-13
    • 2014-05-03
    • 2010-09-12
    • 1970-01-01
    相关资源
    最近更新 更多