尝试调试传递数组中的混合日期时间和整数

son*_*089 6 pandas datetimeindex

我有两个熊猫数据帧new_hpmnew_mr日期时间索引,我试图根据另一个使用的日期时间索引对一个数据帧进行子集化.loc

两个数据帧的日期时间索引是:

new_hpm.index
DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04',
               '2013-01-05', '2013-01-06', '2013-01-07', '2013-01-08',
               '2013-01-09', '2013-01-10',
               ...
               '2017-12-15', '2017-12-20', '2017-12-21', '2017-12-22',
               '2017-12-23', '2017-12-24', '2017-12-28', '2017-12-29',
               '2017-12-30', '2017-12-31'],
              dtype='datetime64[ns]', name='datetime', length=1093, freq=None)
new_mr.index
DatetimeIndex(['2013-01-07', '2013-01-07', '2013-01-13', '2013-01-13',
               '2013-01-13', '2013-01-13', '2013-01-14', '2013-01-14',
               '2013-01-14', '2013-01-14',
               ...
               '2017-12-31', '2017-12-31', '2017-12-31', '2017-12-31',
               '2017-12-31', '2017-12-31', '2017-12-31', '2017-12-31',
               '2017-12-31', '2017-12-31'],
              dtype='datetime64[ns]', name='date_conv', length=13366, freq=None)
Run Code Online (Sandbox Code Playgroud)

然而,当我这样做时

subset_mr = new_mr.loc[new_hpm.index]
Run Code Online (Sandbox Code Playgroud)

我收到错误消息:

ValueError: mixed datetimes and integers in passed array
Run Code Online (Sandbox Code Playgroud)

Pep*_*ino 1

这很可能是由于 中存在重复项new_mr,如 GH 问题中所示

您可以在切片之前删除索引中的重复项new_mr,或者一般来说,您想要执行集合操作,有更好的方法可以很好地处理索引,例如

subset_mr = new_mr.loc[new_mr.index.intersection(new_hpm.index).drop_duplicates()]
Run Code Online (Sandbox Code Playgroud)