python - 从熊猫系列中删除日期时间索引范围
问题描述
我对以下代码有错误。我正在尝试删除日期范围。
数据:
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
解决方案
这应该可以解决问题:
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]
推荐阅读
- ios - 翠鸟:'setBackgroundImage(with:for:placeholder:options:progressBlock:completionHandler:)' 的模糊使用
- javascript - 调用时如何将未知的附加参数传递给回调函数?
- html - 显示:内联块在手机上不起作用
- c# - 操作员 '!' 不能应用于 x 类型的操作数
- javascript - 如何将带有“nd”、“rd”、“th”的日期转换为 yyyy-mm-dd 格式 Javasciript
- react-native - React-navigation 如何在 redux 中导航到路由
- php - 如何使用 php gettext 在应用程序中获取翻译语言列表
- html - “XPathObject”类型的值没有成员“文本”
- angular - 使用 Angular 4/6 动画在 Mouseover 上滚动 div
- javascript - 使用 JavaScript 为我的网站提供多个图像模式