yash_g5
yash_g5

Reputation: 121

Difference in pandas interpolation methods

i need to check the difference between "index" method of interpolation and "linear" method of interpolation

i created one random pandas series with a missing values, and then checked the interpolation results with linear method and index method

but both of them are returning same result. So, should it return same result? if yes, in what case can i see different results ?

s = pd.Series([21,13,np.nan,152,np.nan,46,98])
s.interpolate(method = 'index')
s.interpolate(method = 'linear')

I am getting following results:

s.interpolate(method = 'index')
0     21.0
1     13.0
2     82.5
3    152.0
4     99.0
5     46.0
6     98.0
dtype: float64


s.interpolate(method = 'linear')
0     21.0
1     13.0
2     82.5
3    152.0
4     99.0
5     46.0
6     98.0
dtype: float64

Upvotes: 0

Views: 448

Answers (2)

tegancp
tegancp

Reputation: 1202

Both the linear and index methods will perform a linear interpolation on the Series; the difference lies in what values are being considered as the independent variable:

  • method = 'index' uses the numerical index values (if your Series doesn't have an index specified, this defaults to 0, 1, 2, ..., n - 1)
  • method = 'linear' treats the elements of the Series as equally spaced (disregarding any values specified in the index); this is of course equivalent to using the sequence 0, 1, 2, ..., n - 1 as the independent variable range

So, for any Series where the index is the default (or any other arithmetic progression, e.g. 0, 2, 4, 6, ...) both of these options will yield the same results.

Upvotes: 2

BENY
BENY

Reputation: 323366

When your index is range or have same gap index and linear will yield same result , try using below example

s = pd.Series([21,13,np.nan,152,np.nan,46,98],index=[0,1,3,4,7,9,10])
s.interpolate(method = 'index')
Out[536]: 
0      21.000000
1      13.000000
3     105.666667
4     152.000000
7      88.400000
9      46.000000
10     98.000000
dtype: float64
s.interpolate(method = 'linear')
Out[537]: 
0      21.0
1      13.0
3      82.5
4     152.0
7      99.0
9      46.0
10     98.0
dtype: float64

Upvotes: 3

Related Questions