-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
[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
Changes from 1 commit
e82d4e8
56976ec
7fa4133
2499c5e
44ebf70
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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): | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can this be done in the Manager method? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is logic that belongs in the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think that downcasting logic is different at the moment? |
Uh oh!
There was an error while loading. Please reload this page.