分别使用参数“index”和“values”调用的 pandas DataFrame 插值函数有什么区别?从文档来看它不明确:
pandas.DataFrame.interpolate
method : str, default ‘linear’
Interpolation technique to use. One of:
‘linear’: Ignore the index and treat the values as equally spaced. This is the only method supported on MultiIndexes.
‘time’: Works on daily and higher resolution data to interpolate given length of interval.
‘index’, ‘values’: use the actual numerical values of the index."
两者似乎都使用索引的数值,是这样吗?
更新:
根据 ansev 的回答,他们确实做了同样的事情
最佳答案
我认为这很清楚,想象一下您要插入点。你的DataFrame的值代表Y值,它是用一些逻辑填充Y中缺失的值,对于他们使用插值函数,在这种情况下,对于变量X,有两个选项,假设固定步长,独立于索引或者考虑索引的值。
线性插值示例:
这里,对于每一行,索引向上增加 1,因此方法之间没有区别。
df=pd.DataFrame({'Y':[1,np.nan,3]})
print(df)
Y
0 1.0
1 NaN
2 3.0
print(df.interpolate(method = 'index'))
Y
0 1.0
1 2.0
2 3.0
print(df.interpolate())
Y
0 1.0
1 2.0
2 3.0
<小时/>
但是如果我们更改索引值...
df.index = [0,1,10000]
print(df.interpolate(method = 'index'))
Y
0 1.0000
1 1.0002 #(3-1)*((1-0)/(10000-0))
10000 3.0000
print(df.interpolate())
Y
0 1.0
1 2.0
10000 3.0
df.index = [0,0.1,1]
print(df.interpolate(method = 'index'))
Y
0.0 1.0
0.1 1.2 #(3-1)*((0.1-0)/(1-0))
1.0 3.0
关于python - pandas 插值函数的参数 'index' 和 'values' 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59877756/