Fill NaN values using an interpolation method.
Please note that only method='linear'
is supported for DataFrame/Series with a MultiIndex.
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.
âpadâ: Fill in NaNs using existing values.
ânearestâ, âzeroâ, âslinearâ, âquadraticâ, âcubicâ, âbarycentricâ, âpolynomialâ: Passed to scipy.interpolate.interp1d, whereas âsplineâ is passed to scipy.interpolate.UnivariateSpline. These methods use the numerical values of the index. Both âpolynomialâ and âsplineâ require that you also specify an order (int), e.g. df.interpolate(method='polynomial', order=5)
. Note that, slinear method in Pandas refers to the Scipy first order spline instead of Pandas first order spline.
âkroghâ, âpiecewise_polynomialâ, âsplineâ, âpchipâ, âakimaâ, âcubicsplineâ: Wrappers around the SciPy interpolation methods of similar names. See Notes.
âfrom_derivativesâ: Refers to scipy.interpolate.BPoly.from_derivatives.
Axis to interpolate along. For Series this parameter is unused and defaults to 0.
Maximum number of consecutive NaNs to fill. Must be greater than 0.
Update the data in place if possible.
Consecutive NaNs will be filled in this direction.
If âmethodâ is âpadâ or âffillâ, âlimit_directionâ must be âforwardâ.
If âmethodâ is âbackfillâ or âbfillâ, âlimit_directionâ must be âbackwardsâ.
If âmethodâ is âbackfillâ or âbfillâ, the default is âbackwardâ
else the default is âforwardâ
method is âbackfillâ or âbfillâ.
method is âpadâ or âffillâ.
If limit is specified, consecutive NaNs will be filled with this restriction.
None
: No fill restriction.
âinsideâ: Only fill NaNs surrounded by valid values (interpolate).
âoutsideâ: Only fill NaNs outside valid values (extrapolate).
Downcast dtypes if possible.
Deprecated since version 2.1.0.
Keyword arguments to pass on to the interpolating function.
Returns the same object type as the caller, interpolated at some or all NaN
values or None if inplace=True
.
Notes
The âkroghâ, âpiecewise_polynomialâ, âsplineâ, âpchipâ and âakimaâ methods are wrappers around the respective SciPy implementations of similar names. These use the actual numerical values of the index. For more information on their behavior, see the SciPy documentation.
Examples
Filling in NaN
in a Series
via linear interpolation.
>>> s = pd.Series([0, 1, np.nan, 3]) >>> s 0 0.0 1 1.0 2 NaN 3 3.0 dtype: float64 >>> s.interpolate() 0 0.0 1 1.0 2 2.0 3 3.0 dtype: float64
Filling in NaN
in a Series via polynomial interpolation or splines: Both âpolynomialâ and âsplineâ methods require that you also specify an order
(int).
>>> s = pd.Series([0, 2, np.nan, 8]) >>> s.interpolate(method='polynomial', order=2) 0 0.000000 1 2.000000 2 4.666667 3 8.000000 dtype: float64
Fill the DataFrame forward (that is, going down) along each column using linear interpolation.
Note how the last entry in column âaâ is interpolated differently, because there is no entry after it to use for interpolation. Note how the first entry in column âbâ remains NaN
, because there is no entry before it to use for interpolation.
>>> df = pd.DataFrame([(0.0, np.nan, -1.0, 1.0), ... (np.nan, 2.0, np.nan, np.nan), ... (2.0, 3.0, np.nan, 9.0), ... (np.nan, 4.0, -4.0, 16.0)], ... columns=list('abcd')) >>> df a b c d 0 0.0 NaN -1.0 1.0 1 NaN 2.0 NaN NaN 2 2.0 3.0 NaN 9.0 3 NaN 4.0 -4.0 16.0 >>> df.interpolate(method='linear', limit_direction='forward', axis=0) a b c d 0 0.0 NaN -1.0 1.0 1 1.0 2.0 -2.0 5.0 2 2.0 3.0 -3.0 9.0 3 2.0 4.0 -4.0 16.0
Using polynomial interpolation.
>>> df['d'].interpolate(method='polynomial', order=2) 0 1.0 1 4.0 2 9.0 3 16.0 Name: d, dtype: float64
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4