dtype=datetime64[ns] 和时间戳之间的无效比较

Invalid comparison between dtype=datetime64[ns] and Timestamp

在 Python 中,如果日期低于另一个数据框的日期,我将尝试删除数据框的行。但是比较不行。

这是我的两个数据框和我尝试比较的结果。 print(MeteoCH.head()) 将导致:

                       TempAvg  TempMin  TempMax
Date                                                                      
2021-11-15 00:00:00      4.4      4.3      4.5     
2021-11-15 01:00:00      4.3      4.3      4.3     
2021-11-15 02:00:00      4.1      4.1      4.2     
2021-11-15 03:00:00      4.0      3.8      4.1    
2021-11-15 04:00:00      3.6      3.4      3.8    

print(PicoLog.head()) 将导致:

                           Temp1   Temp2   Temp3   
Date                                                                        
2021-11-15 18:34:18+01:00  21.268  21.671  21.190     
2021-11-15 18:34:20+01:00  21.266  21.673  21.194     
2021-11-15 18:34:22+01:00  21.270  21.680  21.194     
2021-11-15 18:34:24+01:00  21.263  21.673  21.180    
2021-11-15 18:34:26+01:00  21.262  21.672  21.185

如果我尝试执行以下命令:

MeteoCH.drop(MeteoCH[MeteoCH.index < PicoLog.index.min()], inplace=True)

结果出现以下错误:

TypeError: Invalid comparison between dtype=datetime64[ns] and Timestamp

为什么?如何解决?

我试图以某种方式“转换”它,但它不起作用。

有人可以帮我吗?

更简单的是按大于或等于过滤,倒置 < 如:

MeteoCH[MeteoCH.index >= PicoLog.index.min()]

MeteoCH[~(MeteoCH.index < PicoLog.index.min())]

您的解决方案可能是更改过滤 MeteoCH.index,但在我看来过于复杂:

MeteoCH.drop(MeteoCH.index[MeteoCH.index < PicoLog.index.min()], inplace=True)

编辑:

最初的问题是时区偏移,解决方案是DatetimeIndex.tz_localize:

PicoLog.index = PicoLog.index.tz_localize(None)