Aware DateTime对象不适应DST中的更改

2024-06-14 22:57:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我用python编写了一个生成器,每次调用数据帧时都会产生一天的新数据。我的数据帧是unix时间戳索引的。我对代码的第一次尝试如下所示(df是数据帧,tz是pytz.timezone(在我的例子中是欧洲/阿姆斯特丹):

def interval_generator(df, tz):
    today = datetime.datetime.fromtimestamp(df.index.min(), tz)
    last_day = datetime.datetime.fromtimestamp(df.index.max(), tz)
    while today <= last_day:
            tomorrow = today + datetime.timedelta(days=1)
            yield df.loc[tz.localize(today).timestamp():tz.localize(tomorrow).timestamp() - 1]
            today = tomorrow

然而,在运行我的代码时,我注意到DateTime对象有一种奇怪的行为,即它确实坚持最初附加到的时区(尤其是递增的小时)。(在我眼中)怪异行为的例子:

 import datetime
 import pytz
 tz = pytz.timezone('Europe/Amsterdam')
 # This is when daylight saving times stops in the Netherlands in 2015.
 t1 = datetime.datetime(2015, 10, 25, 0, 0)
 t2 = t1 + datetime.timedelta(days=1)
 t1_localized = tz.localize(t1)
 t2_localized = tz.localize(t2) 
 t2_loc_incremented = t1_localized + datetime.timedelta(days=1)

打印最后三个变量的输出时,您会得到:

>>> t1_localized
datetime.datetime(2015, 10, 25, 0, 0, tzinfo=<DstTzInfo 'Europe/Amsterdam' CEST+2:00:00 DST>)
>>> t2_localized
datetime.datetime(2015, 10, 26, 0, 0, tzinfo=<DstTzInfo 'Europe/Amsterdam' CET+1:00:00 STD>)
>>> t2_loc_incremented
datetime.datetime(2015, 10, 26, 0, 0, tzinfo=<DstTzInfo 'Europe/Amsterdam' CEST+2:00:00 DST>)

更重要的是,对于我的代码,t2的两个版本的时间戳是不同的:

>>> t2_localized.timestamp()
1445814000.0
>>> t2_loc_incremented.timestamp()
1445810400.0

我在生成器函数中通过以下解决方法解决了此问题:

def interval_generator(df, tz):
    today = datetime.datetime.fromtimestamp(df.index.min(), tz=tz).strftime('%Y-%m-%d')
    today = datetime.datetime.strptime(today, '%Y-%m-%d')
    last_day = datetime.datetime.fromtimestamp(df.index.max(), tz=tz).strftime('%Y-%m-%d')
    last_day = datetime.datetime.strptime(last_day, '%Y-%m-%d')
    while today <= last_day:
            tomorrow = today + datetime.timedelta(days=1)
            yield df.loc[tz.localize(today).timestamp():tz.localize(tomorrow).timestamp() - 1]
            today = tomorrow

这基本上让我得到了想要的功能,但我不禁想知道是否有更好的方法来处理这个问题。有什么好办法解决我的问题吗?这被认为是datetime模块的错误吗(我正在使用python3.4)我试着在google上搜索,但什么也找不到


Tags: 数据dftodaydatetimelocalizedloctimestamptz