为什么在比较两个日期时间索引时,“ufunc'subtract'不能使用dtype('<M8[ns]')和dtype('O')类型的操作数?”?

2024-04-29 13:36:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个数据帧,都带有日期时间索引;样本输出:

具有非限制性跟踪气体测向指数的l1测向指数:

DatetimeIndex(['2018-03-25 11:13:41+00:00', '2018-03-25 11:17:40+00:00',
               '2018-03-25 11:21:38+00:00', '2018-03-25 11:25:23+00:00',
               '2018-03-25 11:29:03+00:00', '2018-03-25 11:34:06+00:00',
               '2018-03-25 11:37:47+00:00', '2018-03-25 11:48:48+00:00',
               '2018-03-25 11:59:23+00:00', '2018-03-25 12:08:32+00:00',
               ...
               '2018-03-25 22:31:00+00:00', '2018-03-25 22:45:34+00:00',
               '2018-03-25 22:55:34+00:00', '2018-03-25 23:04:55+00:00',
               '2018-03-25 23:12:44+00:00', '2018-03-25 23:16:38+00:00',
               '2018-03-25 23:20:38+00:00', '2018-03-25 23:24:33+00:00',
               '2018-03-25 23:28:32+00:00', '2018-03-25 23:33:36+00:00'],
              dtype='datetime64[ns, UTC]', length=124, freq=None)

l1\u带\u限制\u跟踪\u气体\u测向指数:

DatetimeIndex(['2018-03-25 11:11:38+00:00', '2018-03-25 11:15:39+00:00',
               '2018-03-25 11:19:38+00:00', '2018-03-25 11:23:27+00:00',
               '2018-03-25 11:27:11+00:00', '2018-03-25 11:32:21+00:00',
               '2018-03-25 11:35:55+00:00', '2018-03-25 11:40:09+00:00',
               '2018-03-25 11:49:39+00:00', '2018-03-25 12:00:05+00:00',
               ...
               '2018-03-25 22:46:17+00:00', '2018-03-25 22:56:26+00:00',
               '2018-03-25 23:05:53+00:00', '2018-03-25 23:10:49+00:00',
               '2018-03-25 23:14:42+00:00', '2018-03-25 23:18:42+00:00',
               '2018-03-25 23:22:36+00:00', '2018-03-25 23:26:31+00:00',
               '2018-03-25 23:31:33+00:00', '2018-03-25 23:35:34+00:00'],
              dtype='datetime64[ns, UTC]', length=130, freq=None)

当我执行get_loc操作时:

index_location=l1_with_nonlimiting_trace_gas_df.index.get_loc(l1_with_limiting_trace_gas_df.index[i],method='nearest')

我得到了ufunc错误: UFUNCTYPE错误:ufunc“subtract”不能使用dtype(“<;M8[ns]”)和dtype('O'类型)的操作数

这在过去是行之有效的。现在它不再工作了。以下是我的版本:

pd.版本 Out[152]:“1.0.1”

np.版本 Out[154]:“1.18.1”

在此提供一些帮助将不胜感激


Tags: 版本nonel1getindex指数lengthloc
1条回答
网友
1楼 · 发布于 2024-04-29 13:36:08

看看Pandas get_loc with datetimeindex failing

同样的问题也被报道过,最后的评论包含了解释 这个问题发生在熊猫版本1.0.1(只是您的版本)

我有版本1.0.3,例如,以下代码有效:

start = pd.Timestamp('2019-12-12 00:00')
end   = pd.Timestamp('2019-12-12 00:03')
testindex = pd.date_range(start, end, freq='10s')
findtime = pd.Timestamp('2019-12-12 0:01:16')
testindex.get_loc(findtime, method='nearest')

我得到了结果8

因此,请升级您的熊猫

相关问题 更多 >