【问题标题】:Perfplot bench() raises "TypeError: ufunc 'isfinite' not supported for the input types, and the input types"Perfplot bench() 引发“TypeError:输入类型和输入类型不支持 ufunc 'isfinite'”
【发布时间】:2019-11-04 15:00:55
【问题描述】:

我正在使用perpflot 库来测试DatetimeIndex 对搜索pandas 数据框的影响。

我已经定义了一个设置函数来创建 2 个数据帧。一个带有日期时间索引,另一个带有时间作为列。我还定义了 2 个函数,它们分别在索引和列中使用 .loc 并返回子数据。但是,它向我显示了typeError

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

代码:

import numpy as np
import pandas as pd
from datetime import datetime
import perfplot


def setup_code(n):
    timeline = pd.date_range(end=datetime.now(), freq='1s', periods=n)
    sensor_readings = np.random.randint(100, size=(n, 4))
    col_labels = ['Sensor1', 'Sensor2', 'Sensor3', 'Sensor4']
    data = pd.DataFrame(sensor_readings, columns=col_labels)
    data['time'] = timeline
    data['time'] = pd.to_datetime(data['time'])
    data2 = data.copy()
    data2 = data2.set_index('time')
    print(n)
    return [data, data2]


def f1(ldata):
    data = ldata[0]
    subdata = data.loc[(data['time'] >= '2019-06-21 08:00:00') & (data['time'] <= '2019-06-21 11:00:00')]
    return subdata


def f2(ldata):
    data = ldata[1]
    subdata = data.loc['2019-06-21 04:00:00':'2019-06-21 10:00:00']
    return subdata


out = perfplot.bench(
    setup=setup_code,  
    kernels=[
        f1, f2
    ],
    n_range=[1000 ** k for k in range(1, 3)],
    labels=['Without Indexing', 'With Indexing'],
    xlabel='Length of DataFrame'
)
out.show()

追溯:

Traceback (most recent call last):                                                                                                | 0/2 [00:00<?, ?it/s]
  File ".\scratchpad.py", line 39, in <module>
    xlabel='Length of DataFrame'
  File "C:\Users\hpandya\AppData\Local\Continuum\anaconda3\lib\site-packages\perfplot\main.py", line 128, in bench
    reference, kernel(data)
  File "C:\Users\hpandya\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\core\numeric.py", line 2423, in allclose
    res = all(isclose(a, b, rtol=rtol, atol=atol, equal_nan=equal_nan))
  File "C:\Users\hpandya\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\core\numeric.py", line 2521, in isclose
    xfin = isfinite(x)
TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

奇怪的是,它在我定义xlabel 的行上显示错误。我觉得我在这里遗漏了一些微不足道的东西。

【问题讨论】:

  • 我没有安装 perfplot 来测试,但你提到使用 datetime 对象。我认为这些是使用整数存储的,您将无法使用 isfinite 函数,该函数旨在使用双精度数。
  • 根据回溯,perfplot 在bench 函数的main.py 的第128 行调用isfinite
  • @user545424,这很奇怪。如果我将返回语句从return subdata 更改为return True,它不会向我显示错误。我不明白为什么库会检查函数返回元素。

标签: python pandas numpy matplotlib perfplot


【解决方案1】:

bench()show() 方法默认比较内核输出以确保所有方法产生相同的输出(为了正确性)。检查是使用 numpy 函数完成的,这些函数可能不适用于所有情况或所有内核输出。

您想要做的是指定一个equality_check 参数,它允许比较输出的方式具有一定的灵活性。这在比较诸如字符串或字典的迭代之类的东西时特别有用,numpy 不能很好地处理这些东西。

如果您确信您的函数是正确的,则将 equality_check 设置为 None,或者传递一些实现您自己的检查逻辑的可调用对象。

out = perfplot.bench(
    ...
    equality_check=lambda x, y: x.equals(y)  # equality_check=None
)

请参阅this answer(滚动到底部)了解更多关于如何使用equality_check 对不同功能进行计时的示例。

【讨论】:

  • 似乎equality_check=Nonesetup 时间与功能一起考虑在内。对于我的示例,它表明索引时间会对搜索产生负面影响。 equality_check=lambda x, y: x.equals(y) 也会失败,因为一个数据帧将有 time 列,而其他数据帧将有时间作为索引。
  • @harvpan 据我所知,相等性检查不是时间的一部分。回复:您的第二点,您总是可以在检查之前执行lambda x, y: x.reset_index().equals(y) 将索引重置为列。
猜你喜欢
  • 2020-02-16
  • 2020-09-29
  • 2020-07-28
  • 2018-01-08
  • 1970-01-01
  • 1970-01-01
  • 2022-12-17
  • 2018-03-09
  • 1970-01-01
相关资源
最近更新 更多