【问题标题】:How to convert millisecond time stamp to normal date in Python?如何在 Python 中将毫秒时间戳转换为正常日期?
【发布时间】:2017-05-27 04:57:55
【问题描述】:

我有一个数据框,其中有一列的值以毫秒时间戳为单位。(df['Millisecond'])

我想要做的是将该列的所有值转换为正常日期。前任。 2017-04-27 04:55:00

【问题讨论】:

    标签: python timestamp milliseconds


    【解决方案1】:

    你需要 python 的 datetime 包来做到这一点:

    import datetime
    
    date = datetime.datetime.fromtimestamp(milliseconds/1000.0)
    
    date = date.strftime('%Y-%m-%d %H:%M:%S')
    

    【讨论】:

    • 我使用了另一个答案,因为它更容易,但这个也可以。谢谢:)
    • @R.Trading,但是这个答案比其他答案更有效,因为在那个答案中使用了熊猫,而不仅仅是转换所必需的。谢谢。
    【解决方案2】:

    您可以使用 to_datetime 函数https://pandas.pydata.org/docs/reference/api/pandas.to_datetime.html 来做到这一点。

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

    【讨论】:

      猜你喜欢
      • 2012-12-02
      • 2017-05-28
      • 1970-01-01
      • 2016-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-11
      • 2016-03-04
      相关资源
      最近更新 更多