Slicing/Truncating TimeIndexed DataFrame 引发 KeyError
Slicing/Truncating TimeIndexed DataFrame raises KeyError
我是 运行 许多日期时间索引数据帧,通过一个函数,我根据开始和结束时间段对数据帧进行切片。同时,同一函数对于许多数据帧运行良好,但在某些数据帧上会引发无法解释的 KeyError。
数据框中的数据在数据类型、列和格式方面是相同的。这是 KeyError
:
的示例
引发错误的 DataFrame 摘录:
>> df_boiler_temp
>> @log_date @tariff_indicator #text
2017-04-23 00:12:48.802 none 65.0
2017-04-23 00:19:00.223 none 64.0
2017-04-23 00:24:02.544 none 63.0
2017-04-23 00:29:20.766 none 62.0
2017-04-23 00:35:00.088 none 61.0
2017-04-23 00:41:00.666 none 60.0
2017-04-23 00:46:00.632 none 59.0
2017-04-23 00:53:38.935 none 58.0
2017-04-23 00:59:21.152 none 57.0
2017-04-23 01:05:59.926 none 56.0
2017-04-23 01:09:58.652 none 65.0
2017-04-23 01:11:00.651 none 66.0
2017-04-23 01:14:10.577 none 67.0
2017-04-23 01:19:58.829 none 66.0
2017-04-23 01:28:00.635 none 65.0
引发KeyError
的代码
df = df_boiler_temp.truncate(before=row['start'], after=row['end'])
(这条线也是如此,本质上是相同的
df = df_boiler_temp[row['start']: row['end']]
)
引发的异常是:
KeyError: 1492909671481000000L
在日期时间表示法中转换为 2017-04-23 01:07:51.481000
。
>> row['start']
>> 2017-04-23 01:07:51.481000
>> row['end']
>> 2017-04-23 02:24:07.953000
我不明白为什么使用这些值截断会引发 KeyError,而引发的 KeyError 恰好落在 dataframe.Any 我如何解决这个问题的想法中存在的日期时间之间?
我不明白为什么,但我发现 this 指出数据集未排序。虽然就我分析的数据集而言,它已经排序,但以下行似乎可以解决问题:
df = df_boiler_temp.sort_index().truncate(before=row['start'], after=row['end'])
编辑:
似乎我包含的数据集已被复制并附加到原始数据集。因此很难发现重复项。
我是 运行 许多日期时间索引数据帧,通过一个函数,我根据开始和结束时间段对数据帧进行切片。同时,同一函数对于许多数据帧运行良好,但在某些数据帧上会引发无法解释的 KeyError。
数据框中的数据在数据类型、列和格式方面是相同的。这是 KeyError
:
引发错误的 DataFrame 摘录:
>> df_boiler_temp
>> @log_date @tariff_indicator #text
2017-04-23 00:12:48.802 none 65.0
2017-04-23 00:19:00.223 none 64.0
2017-04-23 00:24:02.544 none 63.0
2017-04-23 00:29:20.766 none 62.0
2017-04-23 00:35:00.088 none 61.0
2017-04-23 00:41:00.666 none 60.0
2017-04-23 00:46:00.632 none 59.0
2017-04-23 00:53:38.935 none 58.0
2017-04-23 00:59:21.152 none 57.0
2017-04-23 01:05:59.926 none 56.0
2017-04-23 01:09:58.652 none 65.0
2017-04-23 01:11:00.651 none 66.0
2017-04-23 01:14:10.577 none 67.0
2017-04-23 01:19:58.829 none 66.0
2017-04-23 01:28:00.635 none 65.0
引发KeyError
df = df_boiler_temp.truncate(before=row['start'], after=row['end'])
(这条线也是如此,本质上是相同的
df = df_boiler_temp[row['start']: row['end']]
)
引发的异常是:
KeyError: 1492909671481000000L
在日期时间表示法中转换为 2017-04-23 01:07:51.481000
。
>> row['start']
>> 2017-04-23 01:07:51.481000
>> row['end']
>> 2017-04-23 02:24:07.953000
我不明白为什么使用这些值截断会引发 KeyError,而引发的 KeyError 恰好落在 dataframe.Any 我如何解决这个问题的想法中存在的日期时间之间?
我不明白为什么,但我发现 this 指出数据集未排序。虽然就我分析的数据集而言,它已经排序,但以下行似乎可以解决问题:
df = df_boiler_temp.sort_index().truncate(before=row['start'], after=row['end'])
编辑: 似乎我包含的数据集已被复制并附加到原始数据集。因此很难发现重复项。