The Pandas Series function interpolate() used to fill NaN values using an interpolation method. It returns Series object that has the same object type as the source object, interpolated at some or all NaN values or None if inplace = True.
1 Series.interpolate(method = 'linear', *, axis = 0, limit = None, inplace = False, limit_direction = None,
2 limit_area = None, downcast = None, **kwargs)
method : It is string that specifies the interpolation technique to fill the NA, if not specified the default value will be linear.
1. | linear : It ignores the index and treat the values as equally spaced. It is only applicable on MultiIndexes. |
2. | time : It works on daily and higher resolution data to interpolate given length of interval. |
3. | index, values : It uses the actual numerical values of the index. |
4. | pad : It fills the NaNs value using existing values. |
5. | nearest, zero, slinear, quadratic, cubic, barycentric, polynomial : It uses the numerical values of the index. The slinear method in Pandas refers to the Scipy first order spline instead of Pandas first order spline. |
6. | krogh, piecewise_polynomial, spline, pchip, akima, cubicspline : Its a wrappers around the SciPy interpolation methods of similar names. |
7. | from_derivatives : It refers to scipy.interpolate.BPoly.from_derivatives which replaces piecewise_polynomial interpolation method in scipy 0.18. |
axis : It can be integer or string that specifies the axis to interpolate along. The value can be 0 or index. If not specified the default value will be 0. It is not used for Series.
inplace : It is boolean value that update the data in place if possible. If not specified the default value will be False.
limit_direction : It specifies the limit consecutive NaNs that will be filled in the specified direction. The values can be 'forward', 'backward', 'both'.
1. | If limit is specified and If specified method is pad or ffill, limit_direction must be forward. |
2. | If limit not specified and If method is backfill or bfill, limit_direction must be backwards. |
limit_area : It limits the consecutive NaNs that will be filled with the given restriction.
1. | None : If specified no fill restriction. |
2. | inside : If specified it fills NaNs that surrounded by valid values (interpolate). |
3. | outside : If specified it fills NaNs that outside valid values (extrapolate). |
downcast : It is a infer or None, that specifies what to downcast if possible.
**kwargs : It specifies the keyword arguments to pass on to the interpolating function.
1 import pandas as pd
2 import numpy as np
3
4 ser1 = pd.Series([0, 1, np.nan, 3])
5 print('The original series elements :')
6 print(ser1)
7
8 res1 = ser1.interpolate()
9 print('The interpolate series object :')
10 print(res1)
In the above example, a series object is created by passing an array that include NaN values. A interpolate() function is called without arguments. The result is assigned to the variable that will be printed on console.
1 The original series elements :
2 0 0.0
3 1 1.0
4 2 NaN
5 3 3.0
6 dtype: float64
7 The interpolate series object :
8 0 0.0
9 1 1.0
10 2 2.0
11 3 3.0
12 dtype: float64
Example 2 with pad and limit
Example 2 with pad and limit
1 import pandas as pd
2 import numpy as np
3
4 ser1 = pd.Series([0, 1, np.nan, 4.71, np.nan])
5 print('The original series elements :')
6 print(ser1)
7
8
9 res2 = ser1.interpolate(method = 'pad', limit = 2)
10 print('The interpolate series object :')
11 print(res2)
In the above example, a series object is created by passing an array that include NaN values. A interpolate() function is called by passing a method and limit. The result is assigned to the variable that will be print.
1 The original series elements :
2 0 0.00
3 1 1.00
4 2 NaN
5 3 4.71
6 4 NaN
7 dtype: float64
8
9 The interpolate series object :
10 0 0.00
11 1 1.00
12 2 1.00
13 3 4.71
14 4 4.71
15 dtype: float64
Note : FutureWarning: Series.interpolate with method=pad is deprecated and will raise in a future version. Use obj.ffill() or obj.bfill() instead.
Related options for your search