Skip to content

[ArrayManager] Array version of interpolate logic #44736

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

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions pandas/core/internals/array_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
new_block,
to_native_types,
)
from pandas.core.missing import interpolate_array

if TYPE_CHECKING:
from pandas import Float64Index
Expand Down Expand Up @@ -367,8 +368,13 @@ def diff(self: T, n: int, axis: int) -> T:
axis = 0
return self.apply(algos.diff, n=n, axis=axis)

def interpolate(self: T, **kwargs) -> T:
return self.apply_with_block("interpolate", swap_axis=False, **kwargs)
def interpolate(self: T, axis: int = 0, **kwargs) -> T:
if axis == 0:
return self.apply(interpolate_array, **kwargs)
else:
return self.apply_with_block(
"interpolate", swap_axis=False, axis=axis, **kwargs
)

def shift(self: T, periods: int, axis: int, fill_value) -> T:
if fill_value is lib.no_default:
Expand Down
71 changes: 70 additions & 1 deletion pandas/core/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@
npt,
)
from pandas.compat._optional import import_optional_dependency
from pandas.util._validators import validate_bool_kwarg

from pandas.core.dtypes.cast import infer_dtype_from
from pandas.core.dtypes.cast import (
infer_dtype_from,
maybe_downcast_to_dtype,
soft_convert_objects,
)
from pandas.core.dtypes.common import (
is_array_like,
is_numeric_v_string_like,
Expand All @@ -41,6 +46,7 @@

if TYPE_CHECKING:
from pandas import Index
from pandas.core.arrays import ExtensionArray


def check_value_size(value, mask: np.ndarray, length: int):
Expand Down Expand Up @@ -973,3 +979,66 @@ def _rolling_window(a: npt.NDArray[np.bool_], window: int) -> npt.NDArray[np.boo
shape = a.shape[:-1] + (a.shape[-1] - window + 1, window)
strides = a.strides + (a.strides[-1],)
return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)


def _maybe_downcast(arr: np.ndarray, downcast=None):
if arr.dtype == np.dtype(object):
if downcast is None:
arr = soft_convert_objects(arr, datetime=True, numeric=False)

if downcast:
arr = maybe_downcast_to_dtype(arr, downcast)
return arr


def interpolate_array(
arr: np.ndarray | ExtensionArray,
method: str = "pad",
axis: int = 0,
index: Index | None = None,
inplace: bool = False,
limit: int | None = None,
limit_direction: str = "forward",
limit_area: str | None = None,
fill_value: Any | None = None,
coerce: bool = False,
downcast: str | None = None,
**kwargs,
) -> np.ndarray | ExtensionArray:

inplace = validate_bool_kwarg(inplace, "inplace")

# first check for extensionarrays
if not isinstance(arr, np.ndarray):
return arr.fillna(value=fill_value, method=method, limit=limit)

if arr.dtype.kind in ["b", "i", "u"]:
# those dtypes can never hold NAs
# If there are no NAs, then interpolate is a no-op
return arr if inplace else arr.copy()

try:
m = clean_fill_method(method)
except ValueError:
m = None
if m is None and arr.dtype.kind != "f":
# only deal with floats
# bc we already checked that can_hold_na, we dont have int dtype here
# TODO: make a copy if not inplace?
return arr

data = arr if inplace else arr.copy()

interp_values = interpolate_array_2d(
data,
method=method,
axis=axis,
index=index,
limit=limit,
limit_direction=limit_direction,
limit_area=limit_area,
fill_value=fill_value,
**kwargs,
)

return _maybe_downcast(interp_values, downcast)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be done in the Manager method?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is logic that belongs in the interpolate function? (for putting this in the manager, I would need to create a small wrapper function that combines the above method with _maybe_downcast to use in ArrayManager.apply. I don't really see an advantage of that)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a huge deal. My thought is that post-op downcasting is likely to be the part where AM/BM are different, so putting the logic in the AM/BM method is more likely to be conducive to sharing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that downcasting logic is different at the moment?