删除早于特定时间的数据
我想从我的数据帧中删除从 00 分钟开始的 2 小时之前的数据(日期时间列位于索引中)。
当我使用下面的代码
df = df[df.index > df.index.max() - pd.Timedelta(hours=2)]
| datetime | value |
|---------------------|-------|
| 17-03-2022 15:05:00 | 78 |
| 17-03-2022 15:07:00 | 86 |
| 17-03-2022 15:57:00 | 77 |
| 17-03-2022 15:58:00 | 22 |
| 17-03-2022 15:59:00 | 10 |
| 17-03-2022 16:00:00 | 22 |
| 17-03-2022 16:01:00 | 25 |
| 17-03-2022 16:02:00 | 25 |
| 17-03-2022 17:05:00 | 34 |
当前日期时间:'17-03-2022 17:05:00'
时, 代码保留 df 中从“17-03-2022 15:05:00”到“17-03-2022”的所有记录17:05:00'
要求:df中从'17-03-2022 15:00:00'到'17-03-2022 17:05:00'的所有记录
应该从-2hrs的第00分钟开始
| datetime | value |
|---------------------|-------|
| 17-03-2022 15:00:00 | 18 |
| 17-03-2022 15:05:00 | 78 |
| 17-03-2022 15:07:00 | 86 |
| 17-03-2022 15:57:00 | 77 |
| 17-03-2022 15:58:00 | 22 |
| 17-03-2022 15:59:00 | 10 |
| 17-03-2022 16:00:00 | 22 |
| 17-03-2022 16:01:00 | 25 |
| 17-03-2022 16:02:00 | 25 |
| 17-03-2022 17:05:00 | 34 |
I want to remove data from my dataframe older than say 2 hours from current time starting with 00 mins (datetime column is in index)
when i use below code
df = df[df.index > df.index.max() - pd.Timedelta(hours=2)]
| datetime | value |
|---------------------|-------|
| 17-03-2022 15:05:00 | 78 |
| 17-03-2022 15:07:00 | 86 |
| 17-03-2022 15:57:00 | 77 |
| 17-03-2022 15:58:00 | 22 |
| 17-03-2022 15:59:00 | 10 |
| 17-03-2022 16:00:00 | 22 |
| 17-03-2022 16:01:00 | 25 |
| 17-03-2022 16:02:00 | 25 |
| 17-03-2022 17:05:00 | 34 |
Current datetime: '17-03-2022 17:05:00'
Issue: My code keeps all records in df from '17-03-2022 15:05:00' to '17-03-2022 17:05:00'
Requirement: All records in df from '17-03-2022 15:00:00' to '17-03-2022 17:05:00'
It should start from 00th minute of -2hrs
| datetime | value |
|---------------------|-------|
| 17-03-2022 15:00:00 | 18 |
| 17-03-2022 15:05:00 | 78 |
| 17-03-2022 15:07:00 | 86 |
| 17-03-2022 15:57:00 | 77 |
| 17-03-2022 15:58:00 | 22 |
| 17-03-2022 15:59:00 | 10 |
| 17-03-2022 16:00:00 | 22 |
| 17-03-2022 16:01:00 | 25 |
| 17-03-2022 16:02:00 | 25 |
| 17-03-2022 17:05:00 | 34 |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
Timestamp.floor
将>
更改为>=
:Use
Timestamp.floor
with change>
to>=
:如果您需要一小时,请考虑使用
Timestamp.round
考虑以下示例输出
If you need full hour consider using
Timestamp.round
consider following exampleoutput