【问题标题】:Why am I seeing "ufunc 'subtract' cannot use operands with types dtype('<M8[ns]') and dtype('O')" when comparing two Pandas datetime indices?为什么我在比较两个 Pandas 日期时间索引时看到“ufunc 'subtract' 不能使用类型为 dtype('<M8[ns]') 和 dtype('O') 的操作数”?
【发布时间】: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_locnearest 方法可能会这样做。
  • 谢谢特伦顿,成功了。这是将闲置数月的项目吹掉的陷阱之一 - 新版本有很多新错误。

标签: python pandas numpy datetime


【解决方案1】:

看看Pandas get_loc with datetimeindex failing

报告了同样的问题,最后一条评论包含解释 Pandas 版本 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

所以升级你的熊猫

【讨论】:

    猜你喜欢
    • 2020-12-28
    • 1970-01-01
    • 2019-10-23
    • 2020-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多