Skip to content

PERF: Calculate mask in interpolate only once #50326

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 3 commits into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -11643,7 +11643,7 @@ def _find_valid_index(self, *, how: str) -> Hashable | None:
-------
idx_first_valid : type of index
"""
idxpos = find_valid_index(self._values, how=how)
idxpos = find_valid_index(self._values, how=how, is_valid=~isna(self._values))
if idxpos is None:
return None
return self.index[idxpos]
Expand Down
21 changes: 13 additions & 8 deletions pandas/core/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ def clean_interp_method(method: str, index: Index, **kwargs) -> str:
return method


def find_valid_index(values, *, how: str) -> int | None:
def find_valid_index(
values, *, how: str, is_valid: npt.NDArray[np.bool_]
) -> int | None:
"""
Retrieves the index of the first valid value.

Expand All @@ -179,6 +181,8 @@ def find_valid_index(values, *, how: str) -> int | None:
values : ndarray or ExtensionArray
how : {'first', 'last'}
Use this parameter to change between the first or last valid index.
is_valid: np.ndarray
Mask to find na_values.

Returns
-------
Expand All @@ -189,8 +193,6 @@ def find_valid_index(values, *, how: str) -> int | None:
if len(values) == 0: # early stop
return None

is_valid = ~isna(values)

if values.ndim == 2:
is_valid = is_valid.any(axis=1) # reduce axis 1

Expand All @@ -204,7 +206,9 @@ def find_valid_index(values, *, how: str) -> int | None:

if not chk_notna:
return None
return idxpos
# Incompatible return value type (got "signedinteger[Any]",
# expected "Optional[int]")
return idxpos # type: ignore[return-value]


def interpolate_array_2d(
Expand Down Expand Up @@ -400,12 +404,12 @@ def _interpolate_1d(
# These are sets of index pointers to invalid values... i.e. {0, 1, etc...
all_nans = set(np.flatnonzero(invalid))

first_valid_index = find_valid_index(yvalues, how="first")
first_valid_index = find_valid_index(yvalues, how="first", is_valid=valid)
if first_valid_index is None: # no nan found in start
first_valid_index = 0
start_nans = set(range(first_valid_index))

last_valid_index = find_valid_index(yvalues, how="last")
last_valid_index = find_valid_index(yvalues, how="last", is_valid=valid)
if last_valid_index is None: # no nan found in end
last_valid_index = len(yvalues)
end_nans = set(range(1 + last_valid_index, len(valid)))
Expand Down Expand Up @@ -738,12 +742,13 @@ def _interpolate_with_limit_area(
"""

invalid = isna(values)
is_valid = ~invalid

if not invalid.all():
first = find_valid_index(values, how="first")
first = find_valid_index(values, how="first", is_valid=is_valid)
if first is None:
first = 0
last = find_valid_index(values, how="last")
last = find_valid_index(values, how="last", is_valid=is_valid)
if last is None:
last = len(values)

Expand Down