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

问题描述 投票:0回答:1

我在以下代码中出错。我正在尝试删除日期范围。

数据:

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
python series
1个回答
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]
© www.soinside.com 2019 - 2024. All rights reserved.