【问题标题】:Trying to debug mixed datetimes and integers in passed array尝试在传递的数组中调试混合的日期时间和整数
【发布时间】:2019-04-15 19:20:29
【问题描述】:

我有两个带有日期时间索引的熊猫数据框 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)

但是,当我这样做时

subset_mr = new_mr.loc[new_hpm.index]

我收到错误消息:

ValueError: mixed datetimes and integers in passed array

【问题讨论】:

    标签: pandas datetimeindex


    【解决方案1】:

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

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

    subset_mr = new_mr.loc[new_mr.index.intersection(new_hpm.index).drop_duplicates()]
    

    【讨论】:

      猜你喜欢
      • 2022-08-22
      • 1970-01-01
      • 2021-01-19
      • 1970-01-01
      • 2014-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-18
      相关资源
      最近更新 更多