【问题标题】:Python: convert timestamp to datetimePython:将时间戳转换为日期时间
【发布时间】:2022-01-27 17:02:19
【问题描述】:

我有以下数据框:

df=pd.DataFrame(index=[0])
df['timestamp'] = 1642104862000
df:
    timestamp
0   1642104862000

我正在应用以下代码:

df['timestamp'] = pd.to_datetime(df['timestamp'], infer_datetime_format=True)

但我得到的日期是1970-01-01 00:27:22.104862,这是错误的。正确的日期是2022-01-13 20:14:22

我怎样才能找到正确的日期? 谢谢

【问题讨论】:

  • 毫秒所以unit='ms'
  • 感谢您的快速回答。成功了

标签: python pandas datetime timestamp unix-timestamp


【解决方案1】:

确保您使用的是正确的单位。试试这个代码:

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

【讨论】:

  • 如果指定unit,则不需要infer_datetime_format=True
猜你喜欢
  • 2019-02-03
  • 2020-06-15
  • 1970-01-01
  • 1970-01-01
  • 2017-04-25
  • 2022-01-08
相关资源
最近更新 更多