【求助】为何函数两个输入,计算出来的结果类型都变了?
如题,这段代码作用:已知的X、Y,给定特定的X,求对应的Y,结果用于其他运算,批量运行的时候发生错误调试发现在特定的数(0.7504454145097735,不知道是否还有其他数会出问题),返回值类型变了。正常应该是一个float类型的数,但是报错的时候返回的是一个Series
求大神帮助解答和解决一下,十分感谢
程序代码:
import pandas as pd import numpy def getPhi(Tr): #插值方法 data = pd.DataFrame() data["x"] = [0.998833, 0.980093, 0.958801, 0.940904, 0.926416, 0.911097, 0.901715, 0.883854, 0.867716, 0.856648, 0.836247, 0.818398, 0.803934, 0.792023, 0.778426, 0.760601, 0.74362, 0.725772, 0.703708, 0.686762, 0.662193, 0.642707, 0.624942, 0.599566, 0.589425, 0.570842, 0.556486] data["y"] = [0.638418, 0.660927, 0.686246, 0.708767, 0.727066, 0.74393, 0.756608, 0.774859, 0.788864, 0.801518, 0.821157, 0.837984, 0.853437, 0.866079, 0.878696, 0.892678, 0.906671, 0.923499, 0.940266, 0.949989, 0.963875, 0.974985, 0.98185, 0.991453, 0.994156, 0.998161, 1] n = Tr if n<=0.662193: return 0.998833 elif n>=1: return 0.556486 else: new = pd.DataFrame([[n, numpy.NaN]], columns=data.columns) #print(new) data = data.append(new, ignore_index=True) data = data.sort_values(by='x', axis=0, ascending=True) data['y'] = data['y'].interpolate(method='linear') #print(data) new = data[data.x == n] return new.y print(type(getPhi(0.7504454145097735))) print(type(getPhi(0.6)))
下面是结果:
<class 'pandas.core.series.Series'> <class 'float'>