[英]Inconsistent pandas datetime conversion
A reproducible example: 一个可重现的示例:
import pandas as pd
now = pd.Timestamp('2018-04-09 09:10')
start_of_today = pd.datetime(now.year, now.month, now.day, 0)
print(pd.to_datetime(start_of_today.timestamp(), unit='s'))
The last line will return 2018-04-08 22:00:00 instead of 2018-04-09 00:00, why does this happen? 最后一行将返回2018-04-08 22:00:00而不是2018-04-09 00:00,为什么会发生这种情况?
As per datetime.timestamp
documentation : 根据datetime.timestamp
文档 :
For aware datetime instances, the return value is computed as: 对于已知的日期时间实例,返回值的计算方式为:
(dt - datetime(1970, 1, 1, tzinfo=timezone.utc)).total_seconds()
Therefore, you need to align your pd.datetime
object with UTC timezone: 因此,您需要将pd.datetime
对象与UTC时区对齐:
from datetime import timezone
import pandas as pd
now = pd.Timestamp('2018-04-09 09:10')
start_of_today = pd.datetime(now.year, now.month, now.day, 0, 0, 0, 0, timezone.utc)
print(pd.to_datetime(start_of_today.timestamp(), unit='s'))
This is because of datetime.timestamp 这是因为datetime.timestamp
it returns (dt - datetime(1970, 1, 1, tzinfo=timezone.utc)).total_seconds()
它返回(dt - datetime(1970, 1, 1, tzinfo=timezone.utc)).total_seconds()
timezone is utc 时区为UTC
You can see 你可以看到
In [65]: start_of_today.timestamp()
Out[65]: 1523203200.0
In [66]: pd.Timestamp('2018-04-09 00:00').timestamp()
Out[66]: 1523232000.0
Their timestamps are different. 他们的时间戳是不同的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.