Skip to content

Commit

Permalink
DOC: Add docstring for Extensionarray interpolate (#59749)
Browse files Browse the repository at this point in the history
* Update docstring of Extensionarray.interpolate

* Remove Extensionarray.interpolate from code_checks.sh

* Resolving pre-commit errors

* Resolving pre-commit errors 2

* Resolved ruff formatting error

* Fix issues after review
  • Loading branch information
ammar-qazi authored Sep 10, 2024
1 parent de51d33 commit 16b7288
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
1 change: 0 additions & 1 deletion ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
-i "pandas.Timestamp.resolution PR02" \
-i "pandas.Timestamp.tzinfo GL08" \
-i "pandas.Timestamp.year GL08" \
-i "pandas.api.extensions.ExtensionArray.interpolate PR01,SA01" \
-i "pandas.api.types.is_dict_like PR07,SA01" \
-i "pandas.api.types.is_extension_array_dtype SA01" \
-i "pandas.api.types.is_file_like PR07,SA01" \
Expand Down
78 changes: 76 additions & 2 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -999,16 +999,74 @@ def interpolate(
**kwargs,
) -> Self:
"""
See DataFrame.interpolate.__doc__.
Fill NaN values using an interpolation method.
Parameters
----------
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.
* '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. arr.interpolate(method='polynomial', order=5).
* '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 : int
Axis to interpolate along. For 1-dimensional data, use 0.
index : Index
Index to use for interpolation.
limit : int or None
Maximum number of consecutive NaNs to fill. Must be greater than 0.
limit_direction : {'forward', 'backward', 'both'}
Consecutive NaNs will be filled in this direction.
limit_area : {'inside', 'outside'} or None
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).
copy : bool
If True, a copy of the object is returned with interpolated values.
**kwargs : optional
Keyword arguments to pass on to the interpolating function.
Returns
-------
ExtensionArray
An ExtensionArray with interpolated values.
See Also
--------
Series.interpolate : Interpolate values in a Series.
DataFrame.interpolate : Interpolate values in a DataFrame.
Notes
-----
- All parameters must be specified as keyword arguments.
- 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.
Examples
--------
Interpolating values in a NumPy array:
>>> arr = pd.arrays.NumpyExtensionArray(np.array([0, 1, np.nan, 3]))
>>> arr.interpolate(
... method="linear",
... limit=3,
... limit_direction="forward",
... index=pd.Index([1, 2, 3, 4]),
... index=pd.Index(range(len(arr))),
... fill_value=1,
... copy=False,
... axis=0,
Expand All @@ -1017,6 +1075,22 @@ def interpolate(
<NumpyExtensionArray>
[0.0, 1.0, 2.0, 3.0]
Length: 4, dtype: float64
Interpolating values in a FloatingArray:
>>> arr = pd.array([1.0, pd.NA, 3.0, 4.0, pd.NA, 6.0], dtype="Float64")
>>> arr.interpolate(
... method="linear",
... axis=0,
... index=pd.Index(range(len(arr))),
... limit=None,
... limit_direction="both",
... limit_area=None,
... copy=True,
... )
<FloatingArray>
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
Length: 6, dtype: Float64
"""
# NB: we return type(self) even if copy=False
raise NotImplementedError(
Expand Down

0 comments on commit 16b7288

Please sign in to comment.