【问题标题】:How to convert a list of UNIX timestamps to a list of datetime objects in a pandas dataframe?如何将 UNIX 时间戳列表转换为 pandas 数据框中的日期时间对象列表?
【发布时间】:2016-11-15 08:43:39
【问题描述】:

我有一个 pandas 数据框 df,其中有一列包含格式为 1,475,761,269,562 的 UNIX 时间戳。

当我尝试将其转换为 df["Timestamp"] = pd.to_datetime(df["Timestamp"], unit = "s"),

我得到 OverflowError: Long too big to convert.

我做错了什么?

【问题讨论】:

    标签: python datetime pandas unix-timestamp


    【解决方案1】:

    你需要unit='ms',也许replace , 来清空字符串:

    df = pd.DataFrame({'Timestamp':['1,475,761,269,562', '1,475,761,269,562']})
    print (df)
               Timestamp
    0  1,475,761,269,562
    1  1,475,761,269,562
    
    df["Timestamp"] = pd.to_datetime(df["Timestamp"].str.replace(',',''), unit = "ms")
    print (df)
                    Timestamp
    0 2016-10-06 13:41:09.562
    1 2016-10-06 13:41:09.562
    

    【讨论】:

      猜你喜欢
      • 2022-12-15
      • 1970-01-01
      • 2019-08-20
      • 2016-10-05
      • 2022-11-30
      • 1970-01-01
      • 1970-01-01
      • 2018-04-12
      • 1970-01-01
      相关资源
      最近更新 更多