【发布时间】:2021-11-05 08:15:42
【问题描述】:
我正在尝试在某个时间戳之前删除 pandas 数据帧中的任何数据(我现在在此示例中使用)
candle_data[min] 是我的数据框...
candle_data[min].loc[candle_data[min].index < pd.to_datetime(datetime.datetime.utcnow(), unit='ns')]
TypeError: Invalid comparison between dtype=datetime64[ns, UTC] and Timestamp
这里是数据数据框
candle_data[min].loc[candle_data[min].index]
volume complete o h l c
time
2021-09-06 20:00:00+00:00 353 True 1.25344 1.25374 1.25324 1.25362
2021-09-06 20:05:00+00:00 125 True 1.25357 1.25357 1.25320 1.25338
2021-09-06 20:10:00+00:00 75 True 1.25336 1.25354 1.25332 1.25333
2021-09-06 20:15:00+00:00 70 True 1.25336 1.25356 1.25331 1.25336
2021-09-06 20:20:00+00:00 68 True 1.25333 1.25352 1.25332 1.25338
... ... ... ... ... ... ...
2021-09-08 13:45:00+00:00 429 True 1.26782 1.26891 1.26758 1.26879
2021-09-08 13:50:00+00:00 363 True 1.26877 1.26901 1.26822 1.26831
2021-09-08 13:55:00+00:00 340 True 1.26829 1.26832 1.26657 1.26680
2021-09-08 14:00:00+00:00 1219 True 1.26688 1.26797 1.26460 1.26790
2021-09-08 14:05:00+00:00 654 True 1.26786 1.26858 1.26641 1.26650
[499 rows x 6 columns]
这就是 pd.to_datetime 给我的
pd.to_datetime(datetime.datetime.utcnow(), unit='ns')
Timestamp('2021-09-08 14:34:37.175469')
我尝试查看其他文章和谷歌,但未能避免与此非常相似的消息...如何将时间戳转换为可以比较的东西?
【问题讨论】:
-
为什么要首先使用 Python 日期时间?您可以只使用 pandas 类型,例如
pd.Timestamp('2021-09-08 14:34:37.175469')或pd.Timestamp('now')
标签: python pandas dataframe datetime