python-3.x - 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
为什么是这样?如何解决这个问题?我当然可以手动分割系列并连接。
解决方案
如您所见,查看 的第一项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
推荐阅读
- java - 我的 Vaadin 登录页面不会触发 Chrome 的“保存密码”功能
- corda - 在 Corda 中验证流
- python - 如何通过终端或通过pycharm安装torch包
- vb.net - VB .NET - 加法过程中的奇怪结果
- node.js - 为什么我的 node.js 网络服务器收到来自浏览器的多个请求?
- javascript - 如何在 D3.js 中读取 Blob
- powershell - 是否可以在 NewComplianceSearch 中使用 ContentMatchQuery 中的变量?
- c - c - 如何初始化一个常量结构
- php - 从字符串中提取月份名称和年份
- angular - 如何在 Angular 8 的 Mat-Paginator 中显示页面?