首页 > 解决方案 > Python datetime 到 Unix 纪元时间戳

问题描述

我正在尝试将 pandas 日期时间对象转换为 unix/epoch 时间戳。我收到数据集中某个特定日期的错误。

我正在尝试的时间戳是这些:

1) 2019-08-31 21:38:00

2) 2019-01-09 16:26:00

我正在使用 Python 的 datetime 模块:

1) datetime.strptime('2019-08-31 21:38:00', '%Y-%m-%d %H:%M:%S').timestamp()
1567280280.0

2) datetime.strptime('2019-01-09 16:26:00', '%Y-%m-%d %H:%M:%S').timestamp()
1547047560.0

尽管对象 (2) 大于 (1),但 python 中的纪元时间戳显示不同。

当我在 excel 中将相同的日期时间对象转换为数值时,我得到:

1) '2019-08-31 21:38:00' -> 43708.901389

2) '2019-01-09 16:26:00' -> 43709.684722

哪个是对的。对象 (2) > 对象 (1)。

显然日期 (1) 小于日期 (2)。

为什么它使用python显示错误的顺序?

标签: pythonexcelpandasdatetime

解决方案


推荐阅读