【问题标题】:Drop datetime index range from pandas series从熊猫系列中删除日期时间索引范围
【发布时间】:2020-04-23 03:39:39
【问题描述】:

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

数据:

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操作数是and,不是&
  • 问题出在最后一行代码。操作数在输出中给出了良好的日期范围。

标签: python series


【解决方案1】:

这应该可以解决问题:

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]

【讨论】:

    猜你喜欢
    • 2023-01-12
    • 2017-02-09
    • 2021-03-19
    • 1970-01-01
    • 2019-02-19
    • 2021-12-29
    • 2023-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多