首页 > 解决方案 > DST 更改日的“combine_first”失败

问题描述

我正在尝试使用combine_first加入两个熊猫系列,以便一个优先于另一个。但它在 DST 更改日失败。我把这个演示放在一起:

import pandas as pd
import numpy as np

fr1 = pd.date_range(pd.to_datetime('2020-10-25').tz_localize('Europe/Berlin'), pd.to_datetime('2020-10-26').tz_localize('Europe/Berlin'), freq='H')
fr2 = fr1 + pd.DateOffset(hours=12)
d1 = pd.Series(data=np.random.randint(0,10, len(fr1)), index = fr1)
d2 = pd.Series(data=np.random.randint(0,10, len(fr2)), index = fr2)
d2.combine_first(d1)

输出:

ValueError: cannot reindex from a duplicate axis

为什么是这样?如何解决这个问题?我当然可以手动分割系列并连接。

标签: python-3.xpandasdatetimetimezonetimestamp-with-timezone

解决方案


如您所见,查看 的第一项DateTimeIndex,时区+xxxx在 DST 更改时发生更改:

>>> date_range = pd.date_range(pd.to_datetime('2020-10-25').tz_localize('Europe/Berlin'), pd.to_datetime('2020-10-26').tz_localize('Europe/Berlin'), freq='H')
>>> date_range[:4]
DatetimeIndex(['2020-10-25 00:00:00+02:00', '2020-10-25 01:00:00+02:00',
               '2020-10-25 02:00:00+02:00', '2020-10-25 02:00:00+01:00'],

当您添加 12 小时时,日期也会更改其时区:

DatetimeIndex(['2020-10-25 12:00:00+01:00', '2020-10-25 13:00:00+01:00',
               '2020-10-25 14:00:00+01:00', '2020-10-25 14:00:00+01:00'],
              dtype='datetime64[ns, Europe/Berlin]', freq=None)

然而,时差不再是 12 小时,而是由于时区变化而变为 11 小时。这是因为当添加DateOffset到 a时DatetimeIndex,您要求 12 小时的“明显”变化,如“2 个不同的时钟读数相隔 12 小时”。这意味着 2 个时间戳会映射到 12 小时后出现的同一个时间戳。

允许表达非常量增量是一种特殊性DateOffset,即如果您添加“1 个月”,您同样不会在日期中添加相同的秒数,具体取决于您所在的月份。

如果您想在 12 个实际小时内换班,请使用Timedelta

>>> (date_range[:4] + pd.Timedelta(hours=12))
DatetimeIndex(['2020-10-25 11:00:00+01:00', '2020-10-25 12:00:00+01:00',
               '2020-10-25 13:00:00+01:00', '2020-10-25 14:00:00+01:00'],
              dtype='datetime64[ns, Europe/Berlin]', freq='H')
>>> (date_range + pd.Timedelta(hours=12)).is_unique
True

现在索引是唯一的,combine_first也可以工作:

>>> d1 = pd.Series(data=np.random.randint(0, 10, 26), index=date_range)
>>> d2 = pd.Series(data=np.random.randint(0, 10, 26), index=date_range + pd.Timedelta(hours=12))
>>> d2.combine_first(d1)
2020-10-25 00:00:00+02:00    0.0
2020-10-25 01:00:00+02:00    1.0
2020-10-25 02:00:00+02:00    2.0
2020-10-25 02:00:00+01:00    1.0
2020-10-25 03:00:00+01:00    7.0
2020-10-25 04:00:00+01:00    4.0
2020-10-25 05:00:00+01:00    0.0
2020-10-25 06:00:00+01:00    1.0
2020-10-25 07:00:00+01:00    1.0
2020-10-25 08:00:00+01:00    1.0
2020-10-25 09:00:00+01:00    6.0
2020-10-25 10:00:00+01:00    7.0
2020-10-25 11:00:00+01:00    4.0
2020-10-25 12:00:00+01:00    8.0
2020-10-25 13:00:00+01:00    6.0
2020-10-25 14:00:00+01:00    0.0
2020-10-25 15:00:00+01:00    6.0
2020-10-25 16:00:00+01:00    0.0
2020-10-25 17:00:00+01:00    0.0
2020-10-25 18:00:00+01:00    9.0
2020-10-25 19:00:00+01:00    7.0
2020-10-25 20:00:00+01:00    9.0
2020-10-25 21:00:00+01:00    3.0
2020-10-25 22:00:00+01:00    4.0
2020-10-25 23:00:00+01:00    0.0
2020-10-26 00:00:00+01:00    6.0
2020-10-26 01:00:00+01:00    5.0
2020-10-26 02:00:00+01:00    9.0
2020-10-26 03:00:00+01:00    1.0
2020-10-26 04:00:00+01:00    4.0
2020-10-26 05:00:00+01:00    4.0
2020-10-26 06:00:00+01:00    3.0
2020-10-26 07:00:00+01:00    1.0
2020-10-26 08:00:00+01:00    8.0
2020-10-26 09:00:00+01:00    1.0
2020-10-26 10:00:00+01:00    6.0
2020-10-26 11:00:00+01:00    5.0
2020-10-26 12:00:00+01:00    6.0
Freq: H, dtype: float64

推荐阅读