【发布时间】:2020-08-28 19:01:20
【问题描述】:
我有两个熊猫数据框,都带有日期时间索引;样本输出:
l1_with_nonlimiting_trace_gas_df.index:
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_with_limiting_trace_gas_df.index:
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 错误:
UFuncTypeError: ufunc 'subtract' 不能使用类型为 dtype(' 这在过去是有效的。现在它不再起作用了。这是我的版本: pd.版本
输出[152]:'1.0.1' np.版本
输出[154]:'1.18.1' 我们将不胜感激。
【问题讨论】:
-
我首先尝试将 pandas 更新到 1.1.1。
-
遗憾的是,我的 Anaconda Pandas 是最新的。
-
你可以考虑用 python 3.8 安装当前的 Anaconda。
-
错误告诉您一个参数(列)是对象 dtype,而不是
datetime64。完整的回溯可能会有所帮助,因为您实际上并没有有所作为。但是get_loc和nearest方法可能会这样做。 -
谢谢特伦顿,成功了。这是将闲置数月的项目吹掉的陷阱之一 - 新版本有很多新错误。
标签: python pandas numpy datetime