熊猫系列的删除日期时间索引范围

时间:2020-04-23 03:39:39

标签: python series

我的以下代码有错误。我正在尝试删除日期范围。

数据:

timestamp
2020-01-31 04:00:00    0.004923
2020-01-31 05:00:00    0.008942
2020-01-31 06:00:00    0.006695
2020-01-31 07:00:00    0.005026
2020-01-31 08:00:00    0.005724
2020-01-31 09:00:00    0.004783
2020-01-31 10:00:00    0.009536
2020-01-31 11:00:00    0.004379
2020-01-31 12:00:00    0.007783
2020-01-31 13:00:00    0.008245

drop_list = volatility.index[(volatility.index >= '2020-03-12 00:00:00') & (volatility.index <= '2020-03-13 00:00:00')].strftime('%Y-%m-%d %H:%M:%S')

Index(['2020-03-12 00:00:00', '2020-03-12 01:00:00', '2020-03-12 02:00:00',
       '2020-03-12 03:00:00', '2020-03-12 04:00:00', '2020-03-12 05:00:00'],
      dtype='object')

volatility = volatility.drop(volatility.index[drop_list], inplace=True)

错误:

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

1 个答案:

答案 0 :(得分:0)

这应该可以解决问题:

keep_list = volatility.index[(volatility.index < '2020-03-12 00:00:00') | (volatility.index > '2020-03-13 00:00:00')]
volatility = volatility.loc[keep_list]