我正在使用perpflot库来测试DatetimeIndex
对搜索熊猫数据框的影响。
我定义了一个设置函数来创建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
的那一行显示错误。我觉得好像我在这里错过了一些琐碎的事情。
答案 0 :(得分:2)
默认情况下,bench()
和show()
方法比较内核输出,以确保所有方法产生相同的输出(出于正确性)。该检查是使用numpy函数完成的,可能不适用于所有情况或所有内核输出。
您要执行的操作是指定一个equality_check
参数,该参数在比较输出方式方面具有一定的灵活性。当比较诸如numpy
无法很好处理的字符串或字典等可迭代对象时,这特别有用。
如果您确信自己的函数正确无误,请将equality_check
设置为“无”,否则将传递实现您自己的检查逻辑的可调用对象。
out = perfplot.bench(
...
equality_check=lambda x, y: x.equals(y) # equality_check=None
)
有关如何使用equality_check
计时不同功能的更多示例,请参见this answer(滚动到底部)。