In Pandas interpolate
function, is method='time'
equivalent to method='linear'
when the time index is equally spaced?
A basic example suggests this is the case:
even_index = pd.date_range('2019-02-20 10:00 am',
'2019-02-20 2:00 pm', freq='1 h')
values = [10, np.nan, 30, np.nan, 50]
pd.DataFrame(values, index=even_index).interpolate(method='time')
0
2019-02-20 10:00:00 10.0
2019-02-20 11:00:00 20.0
2019-02-20 12:00:00 30.0
2019-02-20 13:00:00 40.0
2019-02-20 14:00:00 50.0
pd.DataFrame(values, index=even_index).interpolate(method='linear')
0
2019-02-20 10:00:00 10.0
2019-02-20 11:00:00 20.0
2019-02-20 12:00:00 30.0
2019-02-20 13:00:00 40.0
2019-02-20 14:00:00 50.0
A difference between 'time' and 'linear' only seems to occur if the time index is not equally spaced:
uneven_index = pd.to_datetime(['2019-02-20 10:00 am',
'2019-02-20 10:30 am', '2019-02-20 12:30 pm',
'2019-02-20 1:30 pm', '2019-02-20 2:00 pm'])
pd.DataFrame(values, index=uneven_index).interpolate(method='time')
0
2019-02-20 10:00:00 10.000000
2019-02-20 10:30:00 14.000000
2019-02-20 12:30:00 30.000000
2019-02-20 13:30:00 43.333333
2019-02-20 14:00:00 50.000000
pd.DataFrame(values, index=uneven_index).interpolate(method='linear')
0
2019-02-20 10:00:00 10.0
2019-02-20 10:30:00 20.0
2019-02-20 12:30:00 30.0
2019-02-20 13:30:00 40.0
2019-02-20 14:00:00 50.0
My question is whether this always holds. Can it be assumed with an equally spaced time index, method='time'
will always perform linear interpolation?
Yes it is
From the documentation:
‘linear’: Ignore the index and treat the values as equally spaced
Therefore, if your index is equally spaced and you use the proper method ('time' method for a time index in your example, but it could also be the 'index' method for a numeric index with equally spaced values), you will indeed get the same result.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With