问题描述
我有一个包含UTC日期时间的字符串
I have a string containing a UTC datetime
utc_str = '2017-11-21T23:00+0100'
在我的当地时间(欧洲/柏林)中:
which in my local time (Europe/Berlin) is:
local_time = '2017-11-22 00:00'
这是我想从 utc_string
获得的期望值。
And is the desired value I would like to obtain from utc_string
.
我可以转换 utc_string
到 local_time
可以使用:
I can convert utc_string
to local_time
just fine using:
import datetime as dt
utc_time = dt.datetime.strptime(date_str, '%Y-%m-%dT%H:%M%z')
local_time = utc_time.replace(tzinfo=pytz.utc).astimezone(pytz.timezone('Europe/Berlin'))
print(local_time.strftime('%Y-%m-%d %H:%M'))
>>> 2017-11-22 00:00
但是,当我使用 Pandas
,我得到了不同的结果。似乎未应用UTC偏移量:
However, when I use Pandas
, I get a different result. It doesn't seem to apply the UTC offset:
import pandas as pd
pd_date = pd.to_datetime(date_str, utc=True)
print(pd_date.strftime('%Y-%m-%d %H:%M'))
>>> '2017-11-21 22:00'
天真的,如果我尝试执行与使用 datetime
模块,
的结果仍然不可用:
And naively if I try to do the same process as with the datetime
module,the results are still off:
pd_date = pd.to_datetime(date_str, utc=True)
pd_date = pd_date.replace(tzinfo=pytz.utc).astimezone(pytz.timezone('Europe/Berlin'))
print(pd_date.strftime('%Y-%m-%d %H:%M'))
>>> '2017-11-21 23:00'
我有不明白的地方吗?我在使用 pd.to_datetime
还是其他错误?在Python 3.6和Windows 7上。
Is there something I am not understanding? Am I using pd.to_datetime
or something else wrong? On Python 3.6, Windows 7.
推荐答案
如注释中所述,我认为您的代码 local_time
是错误的
As stated in the comment, I think your code for local_time
is wrong
utc_time
datetime.datetime(2017, 11, 21, 23, 0, tzinfo=datetime.timezone(datetime.timedelta(0, 3600))
utc_time.replace(tzinfo=pytz.utc)
'datetime.datetime(2017, 11, 21, 23, 0, tzinfo=<UTC>)'
因此,此替换
删除了'+ 0100
从日期时间起
,但其余部分保持不变
so this replace
removes the '+0100
from the datetime
, but keeps the rest the same
utc_time.replace(tzinfo=pytz.utc).astimezone(pytz.timezone('Europe/Berlin'))
"datetime.datetime(2017, 11, 22, 0, 0, tzinfo=<DstTzInfo 'Europe/Berlin' CET+1:00:00 STD>)"
然后将1小时加到23:00UTC,因此成为第二天午夜柏林符合预期
This then adds 1 hour to 23:00UTC, so become the next day midnight in Berlin as expected
pd.to_datetime(utc_str, utc=True)
Timestamp('2017-11-21 22:00:00+0000', tz='UTC')
行为上的差异是由于构造函数。 pd.to_datetime
计算的时间和时区返回22:00UTC,而不是23:00 + 0100,因此,如果您将时区信息替换为UTC,则不会更改
The difference in behaviour is due to the constructor. pd.to_datetime
calculates the time and timezone back to 22:00UTC instead of 23:00+0100, so if there you replace the timezone info with UTC, it changes nothing
您的 utc_time
对象正确时区,所以如果您想要当地时间,可以在熊猫中执行 utc_time.strftime('%Y-%m-%d%H:%M')
必须做 pd.to_datetime(utc_str,utc = True).astimezone(pytz.timezone('Europe / Berlin'))。strftime('%Y-%m-%d%H:% M')
Your utc_time
object is in the correct timezone, so if you want the local time you can just do utc_time.strftime('%Y-%m-%d %H:%M')
in pandas you'll have to do pd.to_datetime(utc_str, utc=True).astimezone(pytz.timezone('Europe/Berlin')).strftime('%Y-%m-%d %H:%M')
这篇关于日期时间模块和 pandas to_datetime给出不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!