【发布时间】: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