Perfplot bench() raises "TypeError: ufunc 'isfinite' not supported for the input types, and the input types"
Perfplot bench() raises "TypeError: ufunc 'isfinite' not supported for the input types, and the input types"
我正在使用 perpflot 库来测试 DatetimeIndex
在搜索 pandas 数据帧时的效果。
我定义了一个设置函数来创建 2 个数据帧。一个带有日期时间索引,另一个带有时间作为列。我还定义了 2 个函数,它们分别在索引和列中使用 .loc
,在子数据中使用 returns。但是,它显示 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
设置为 None,或者传递一些实现您自己的检查逻辑的可调用对象。
out = perfplot.bench(
...
equality_check=lambda x, y: x.equals(y) # equality_check=None
)
请参阅 (滚动到底部)以了解有关如何使用 equality_check
为不同函数计时的更多示例。
我正在使用 perpflot 库来测试 DatetimeIndex
在搜索 pandas 数据帧时的效果。
我定义了一个设置函数来创建 2 个数据帧。一个带有日期时间索引,另一个带有时间作为列。我还定义了 2 个函数,它们分别在索引和列中使用 .loc
,在子数据中使用 returns。但是,它显示 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
设置为 None,或者传递一些实现您自己的检查逻辑的可调用对象。
out = perfplot.bench(
...
equality_check=lambda x, y: x.equals(y) # equality_check=None
)
请参阅 equality_check
为不同函数计时的更多示例。