Skip to content

ENH: EA.interpolate #53659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jun 28, 2023
1 change: 1 addition & 0 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
pandas.api.extensions.ExtensionArray.factorize \
pandas.api.extensions.ExtensionArray.fillna \
pandas.api.extensions.ExtensionArray.insert \
pandas.api.extensions.ExtensionArray.interpolate \
pandas.api.extensions.ExtensionArray.isin \
pandas.api.extensions.ExtensionArray.isna \
pandas.api.extensions.ExtensionArray.ravel \
Expand Down
1 change: 1 addition & 0 deletions doc/source/reference/extensions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ objects.
api.extensions.ExtensionArray.factorize
api.extensions.ExtensionArray.fillna
api.extensions.ExtensionArray.insert
api.extensions.ExtensionArray.interpolate
api.extensions.ExtensionArray.isin
api.extensions.ExtensionArray.isna
api.extensions.ExtensionArray.ravel
Expand Down
20 changes: 20 additions & 0 deletions pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,26 @@ def closed(self) -> bool:

# Arguments for fillna()
FillnaOptions = Literal["backfill", "bfill", "ffill", "pad"]
InterpolateOptions = Literal[
"linear",
"time",
"index",
"values",
"nearest",
"zero",
"slinear",
"quadratic",
"cubic",
"barycentric",
"polynomial",
"krogh",
"piecewise_polynomial",
"spline",
"pchip",
"akima",
"cubicspline",
"from_derivatives",
]

# internals
Manager = Union[
Expand Down
26 changes: 26 additions & 0 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
AxisInt,
Dtype,
FillnaOptions,
InterpolateOptions,
NumpySorter,
NumpyValueArrayLike,
PositionalIndexer,
Expand All @@ -90,6 +91,8 @@
npt,
)

from pandas import Index

_extension_array_shared_docs: dict[str, str] = {}


Expand Down Expand Up @@ -118,6 +121,7 @@ class ExtensionArray:
fillna
equals
insert
interpolate
isin
isna
ravel
Expand Down Expand Up @@ -155,6 +159,7 @@ class ExtensionArray:
* take
* copy
* _concat_same_type
* interpolate

A default repr displaying the type, (truncated) data, length,
and dtype is provided. It can be customized or replaced by
Expand Down Expand Up @@ -753,6 +758,27 @@ def argmax(self, skipna: bool = True) -> int:
raise NotImplementedError
return nargminmax(self, "argmax")

def interpolate(
self,
*,
method: InterpolateOptions,
axis: int,
index: Index,
limit,
limit_direction,
limit_area,
fill_value,
copy: bool,
**kwargs,
) -> Self:
"""
See DataFrame.interpolate.__doc__.
"""
# NB: we return type(self) even if copy=False
raise NotImplementedError(
f"{type(self).__name__} does not implement interpolate"
)

def fillna(
self,
value: object | ArrayLike | None = None,
Expand Down
11 changes: 6 additions & 5 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
Dtype,
DtypeObj,
F,
InterpolateOptions,
NpDtype,
PositionalIndexer2D,
PositionalIndexerTuple,
Expand Down Expand Up @@ -2233,23 +2234,23 @@ def copy(self, order: str = "C") -> Self:
def interpolate(
self,
*,
method,
method: InterpolateOptions,
axis: int,
index: Index,
limit,
limit_direction,
limit_area,
inplace: bool,
copy: bool,
**kwargs,
) -> Self:
"""
See NDFrame.interpolate.__doc__.
"""
# NB: we return type(self) even if inplace=True
# NB: we return type(self) even if copy=False
if method != "linear":
raise NotImplementedError

if inplace:
if not copy:
out_data = self._ndarray
else:
out_data = self._ndarray.copy()
Expand All @@ -2264,7 +2265,7 @@ def interpolate(
limit_area=limit_area,
**kwargs,
)
if inplace:
if not copy:
return self
return type(self)._simple_new(out_data, dtype=self.dtype)

Expand Down
11 changes: 6 additions & 5 deletions pandas/core/arrays/numpy_.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
AxisInt,
Dtype,
FillnaOptions,
InterpolateOptions,
NpDtype,
Scalar,
Self,
Expand Down Expand Up @@ -261,20 +262,20 @@ def pad_or_backfill(
def interpolate(
self,
*,
method,
method: InterpolateOptions,
axis: int,
index: Index,
limit,
limit_direction,
limit_area,
inplace: bool,
copy: bool,
**kwargs,
) -> Self:
"""
See NDFrame.interpolate.__doc__.
"""
# NB: we return type(self) even if inplace=True
if inplace:
# NB: we return type(self) even if copy=False
if not copy:
out_data = self._ndarray
else:
out_data = self._ndarray.copy()
Expand All @@ -290,7 +291,7 @@ def interpolate(
limit_area=limit_area,
**kwargs,
)
if inplace:
if not copy:
return self
return type(self)._simple_new(out_data, dtype=self.dtype)

Expand Down
23 changes: 2 additions & 21 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
IgnoreRaise,
IndexKeyFunc,
IndexLabel,
InterpolateOptions,
IntervalClosedType,
JSONSerializable,
Level,
Expand Down Expand Up @@ -7753,27 +7754,7 @@ def replace(
@final
def interpolate(
self,
method: Literal[
"linear",
"time",
"index",
"values",
"pad",
"nearest",
"zero",
"slinear",
"quadratic",
"cubic",
"barycentric",
"polynomial",
"krogh",
"piecewise_polynomial",
"spline",
"pchip",
"akima",
"cubicspline",
"from_derivatives",
] = "linear",
method: InterpolateOptions = "linear",
*,
axis: Axis = 0,
limit: int | None = None,
Expand Down
Loading