首页 > 解决方案 > 类型错误:scipy.interpolate.interp1d 中的“str”和“float”实例之间不支持“<”

问题描述

尝试使用 interp1d 进行插值时出现此错误

usecols = [i for i in range(0,3)]
VLE_data = pd.read_csv('VLE data.txt',delimiter = ' ',header=1,usecols=usecols)

VLE_data = VLE_data.set_index('methyl')
VLE_data = VLE_data.drop('methyl',axis=0)
VLE_data = VLE_data.reset_index()
T_data = VLE_data.loc[:,'methyl']
T_data1 = VLE_data.loc[0:19,'methyl']
T_data2 = VLE_data.loc[20:37,'methyl']
T_data3 = VLE_data.loc[38:,'methyl']

x_data = VLE_data.loc[:,'acetate']
x_data1 = VLE_data.loc[0:19,'acetate']
x_data2 = VLE_data.loc[20:37,'acetate']
x_data3 = VLE_data.loc[38:,'acetate']

y_data = VLE_data.loc[:,'(1)']
y_data1 = VLE_data.loc[0:19,'(1)']
y_data2 = VLE_data.loc[20:37,'(1)']
y_data3 = VLE_data.loc[38:,'(1)']

VLE_Pdata = pd.read_csv('VLE data.txt',delimiter = ' ')
P_data = VLE_Pdata.loc['methyl','\u03B31']

xnew = np.linspace(0,1,10)
ynew = np.linspace(0,1,10)

funP1x = interp1d(x_data1,T_data1,kind='linear')
funP3x = interp1d(x_data2,T_data2,kind='linear')
funP7x = interp1d(x_data3,T_data3,kind='linear')
funP1y = interp1d(y_data1,T_data1,kind='linear')
funP3y = interp1d(y_data2,T_data2,kind='linear')
funP7y = interp1d(y_data3,T_data3,kind='linear')

T1new = funP1x(xnew)

结果我得到了这个错误:

Traceback (most recent call last):

  File "C:\Users\VLE.py", line 46, in <module>
    T1new = funP1x(xnew)

  File "C:\Anaconda\lib\site-packages\scipy\interpolate\polyint.py", line 78, in __call__
    y = self._evaluate(x)

  File "C:\Anaconda\lib\site-packages\scipy\interpolate\interpolate.py", line 675, in _evaluate
    y_new = self._call(self, x_new)

  File "C:\Anaconda\lib\site-packages\scipy\interpolate\interpolate.py", line 605, in _call_linear
    x_new_indices = searchsorted(self.x, x_new)

  File "<__array_function__ internals>", line 5, in searchsorted

  File "C:\Anaconda\lib\site-packages\numpy\core\fromnumeric.py", line 1348, in searchsorted
    return _wrapfunc(a, 'searchsorted', v, side=side, sorter=sorter)

  File "C:\Anaconda\lib\site-packages\numpy\core\fromnumeric.py", line 67, in _wrapfunc
    return _wrapit(obj, method, *args, **kwds)

  File "C:\Anaconda\lib\site-packages\numpy\core\fromnumeric.py", line 44, in _wrapit
    result = getattr(asarray(obj), method)(*args, **kwds)

TypeError: '<' not supported between instances of 'str' and 'float'

是什么导致这个错误?

标签: python

解决方案


推荐阅读