Skip to content

Commit c96544e

Browse files
authored
ENH: EA.interpolate (#53659)
* ENH: EA.interpolate * update code check * update docstring to appease code check * inplace->copy * whatsnew
1 parent ee8e01e commit c96544e

File tree

10 files changed

+114
-99
lines changed

10 files changed

+114
-99
lines changed

ci/code_checks.sh

+1
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
231231
pandas.api.extensions.ExtensionArray.factorize \
232232
pandas.api.extensions.ExtensionArray.fillna \
233233
pandas.api.extensions.ExtensionArray.insert \
234+
pandas.api.extensions.ExtensionArray.interpolate \
234235
pandas.api.extensions.ExtensionArray.isin \
235236
pandas.api.extensions.ExtensionArray.isna \
236237
pandas.api.extensions.ExtensionArray.ravel \

doc/source/reference/extensions.rst

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ objects.
5151
api.extensions.ExtensionArray.factorize
5252
api.extensions.ExtensionArray.fillna
5353
api.extensions.ExtensionArray.insert
54+
api.extensions.ExtensionArray.interpolate
5455
api.extensions.ExtensionArray.isin
5556
api.extensions.ExtensionArray.isna
5657
api.extensions.ExtensionArray.ravel

doc/source/whatsnew/v2.1.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,15 @@ Other enhancements
110110
- :meth:`Series.str.join` now supports ``ArrowDtype(pa.string())`` (:issue:`53646`)
111111
- :meth:`SeriesGroupby.agg` and :meth:`DataFrameGroupby.agg` now support passing in multiple functions for ``engine="numba"`` (:issue:`53486`)
112112
- :meth:`SeriesGroupby.transform` and :meth:`DataFrameGroupby.transform` now support passing in a string as the function for ``engine="numba"`` (:issue:`53579`)
113+
- Added :meth:`ExtensionArray.interpolate` used by :meth:`Series.interpolate` and :meth:`DataFrame.interpolate` (:issue:`53659`)
113114
- Added ``engine_kwargs`` parameter to :meth:`DataFrame.to_excel` (:issue:`53220`)
114115
- Added a new parameter ``by_row`` to :meth:`Series.apply`. When set to ``False`` the supplied callables will always operate on the whole Series (:issue:`53400`).
115116
- Groupby aggregations (such as :meth:`DataFrameGroupby.sum`) now can preserve the dtype of the input instead of casting to ``float64`` (:issue:`44952`)
116117
- Improved error message when :meth:`DataFrameGroupBy.agg` failed (:issue:`52930`)
117118
- Many read/to_* functions, such as :meth:`DataFrame.to_pickle` and :func:`read_csv`, support forwarding compression arguments to lzma.LZMAFile (:issue:`52979`)
118119
- Performance improvement in :func:`concat` with homogeneous ``np.float64`` or ``np.float32`` dtypes (:issue:`52685`)
119120
- Performance improvement in :meth:`DataFrame.filter` when ``items`` is given (:issue:`52941`)
121+
-
120122

121123
.. ---------------------------------------------------------------------------
122124
.. _whatsnew_210.notable_bug_fixes:

pandas/_typing.py

+20
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,26 @@ def closed(self) -> bool:
307307

308308
# Arguments for fillna()
309309
FillnaOptions = Literal["backfill", "bfill", "ffill", "pad"]
310+
InterpolateOptions = Literal[
311+
"linear",
312+
"time",
313+
"index",
314+
"values",
315+
"nearest",
316+
"zero",
317+
"slinear",
318+
"quadratic",
319+
"cubic",
320+
"barycentric",
321+
"polynomial",
322+
"krogh",
323+
"piecewise_polynomial",
324+
"spline",
325+
"pchip",
326+
"akima",
327+
"cubicspline",
328+
"from_derivatives",
329+
]
310330

311331
# internals
312332
Manager = Union[

pandas/core/arrays/base.py

+26
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
AxisInt,
7979
Dtype,
8080
FillnaOptions,
81+
InterpolateOptions,
8182
NumpySorter,
8283
NumpyValueArrayLike,
8384
PositionalIndexer,
@@ -90,6 +91,8 @@
9091
npt,
9192
)
9293

94+
from pandas import Index
95+
9396
_extension_array_shared_docs: dict[str, str] = {}
9497

9598

@@ -118,6 +121,7 @@ class ExtensionArray:
118121
fillna
119122
equals
120123
insert
124+
interpolate
121125
isin
122126
isna
123127
ravel
@@ -155,6 +159,7 @@ class ExtensionArray:
155159
* take
156160
* copy
157161
* _concat_same_type
162+
* interpolate
158163
159164
A default repr displaying the type, (truncated) data, length,
160165
and dtype is provided. It can be customized or replaced by
@@ -753,6 +758,27 @@ def argmax(self, skipna: bool = True) -> int:
753758
raise NotImplementedError
754759
return nargminmax(self, "argmax")
755760

761+
def interpolate(
762+
self,
763+
*,
764+
method: InterpolateOptions,
765+
axis: int,
766+
index: Index,
767+
limit,
768+
limit_direction,
769+
limit_area,
770+
fill_value,
771+
copy: bool,
772+
**kwargs,
773+
) -> Self:
774+
"""
775+
See DataFrame.interpolate.__doc__.
776+
"""
777+
# NB: we return type(self) even if copy=False
778+
raise NotImplementedError(
779+
f"{type(self).__name__} does not implement interpolate"
780+
)
781+
756782
def fillna(
757783
self,
758784
value: object | ArrayLike | None = None,

pandas/core/arrays/datetimelike.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
Dtype,
5959
DtypeObj,
6060
F,
61+
InterpolateOptions,
6162
NpDtype,
6263
PositionalIndexer2D,
6364
PositionalIndexerTuple,
@@ -2233,23 +2234,23 @@ def copy(self, order: str = "C") -> Self:
22332234
def interpolate(
22342235
self,
22352236
*,
2236-
method,
2237+
method: InterpolateOptions,
22372238
axis: int,
22382239
index: Index,
22392240
limit,
22402241
limit_direction,
22412242
limit_area,
2242-
inplace: bool,
2243+
copy: bool,
22432244
**kwargs,
22442245
) -> Self:
22452246
"""
22462247
See NDFrame.interpolate.__doc__.
22472248
"""
2248-
# NB: we return type(self) even if inplace=True
2249+
# NB: we return type(self) even if copy=False
22492250
if method != "linear":
22502251
raise NotImplementedError
22512252

2252-
if inplace:
2253+
if not copy:
22532254
out_data = self._ndarray
22542255
else:
22552256
out_data = self._ndarray.copy()
@@ -2264,7 +2265,7 @@ def interpolate(
22642265
limit_area=limit_area,
22652266
**kwargs,
22662267
)
2267-
if inplace:
2268+
if not copy:
22682269
return self
22692270
return type(self)._simple_new(out_data, dtype=self.dtype)
22702271

pandas/core/arrays/numpy_.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
AxisInt,
3737
Dtype,
3838
FillnaOptions,
39+
InterpolateOptions,
3940
NpDtype,
4041
Scalar,
4142
Self,
@@ -261,20 +262,20 @@ def pad_or_backfill(
261262
def interpolate(
262263
self,
263264
*,
264-
method,
265+
method: InterpolateOptions,
265266
axis: int,
266267
index: Index,
267268
limit,
268269
limit_direction,
269270
limit_area,
270-
inplace: bool,
271+
copy: bool,
271272
**kwargs,
272273
) -> Self:
273274
"""
274275
See NDFrame.interpolate.__doc__.
275276
"""
276-
# NB: we return type(self) even if inplace=True
277-
if inplace:
277+
# NB: we return type(self) even if copy=False
278+
if not copy:
278279
out_data = self._ndarray
279280
else:
280281
out_data = self._ndarray.copy()
@@ -290,7 +291,7 @@ def interpolate(
290291
limit_area=limit_area,
291292
**kwargs,
292293
)
293-
if inplace:
294+
if not copy:
294295
return self
295296
return type(self)._simple_new(out_data, dtype=self.dtype)
296297

pandas/core/generic.py

+2-21
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
IgnoreRaise,
6363
IndexKeyFunc,
6464
IndexLabel,
65+
InterpolateOptions,
6566
IntervalClosedType,
6667
JSONSerializable,
6768
Level,
@@ -7753,27 +7754,7 @@ def replace(
77537754
@final
77547755
def interpolate(
77557756
self,
7756-
method: Literal[
7757-
"linear",
7758-
"time",
7759-
"index",
7760-
"values",
7761-
"pad",
7762-
"nearest",
7763-
"zero",
7764-
"slinear",
7765-
"quadratic",
7766-
"cubic",
7767-
"barycentric",
7768-
"polynomial",
7769-
"krogh",
7770-
"piecewise_polynomial",
7771-
"spline",
7772-
"pchip",
7773-
"akima",
7774-
"cubicspline",
7775-
"from_derivatives",
7776-
] = "linear",
7757+
method: InterpolateOptions = "linear",
77777758
*,
77787759
axis: Axis = 0,
77797760
limit: int | None = None,

0 commit comments

Comments
 (0)