繁体   English   中英

Perfplot bench() 引发“类型错误:输入类型和输入类型不支持 ufunc 'isfinite'”

[英]Perfplot bench() raises "TypeError: ufunc 'isfinite' not supported for the input types, and the input types"

我正在使用perpflot库来测试DatetimeIndexPandas数据帧搜索的影响。

我定义了一个设置函数来创建 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的行上显示错误。 我觉得我在这里遗漏了一些微不足道的东西。

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

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

设置equality_check为无,如果你确信你的职责是正确的,或以其他方式通过一些可调用它实现了自己的检查逻辑。

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

看到此答案(滚动至底部)关于如何更多示例equality_check已用于定时不同的功能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM