Skip to content

REF: implement first_valid_index in core.missing #29400

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
Nov 5, 2019
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
23 changes: 4 additions & 19 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
from pandas.core.indexes.period import Period, PeriodIndex
import pandas.core.indexing as indexing
from pandas.core.internals import BlockManager
from pandas.core.missing import find_valid_index
from pandas.core.ops import _align_method_FRAME

from pandas.io.formats import format as fmt
Expand Down Expand Up @@ -10870,27 +10871,11 @@ def _find_valid_index(self, how: str):
-------
idx_first_valid : type of index
"""
assert how in ["first", "last"]

if len(self) == 0: # early stop
idxpos = find_valid_index(self._values, how)
if idxpos is None:
return None
is_valid = ~self.isna()

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

if how == "first":
idxpos = is_valid.values[::].argmax()

if how == "last":
idxpos = len(self) - 1 - is_valid.values[::-1].argmax()

chk_notna = is_valid.iat[idxpos]
idx = self.index[idxpos]

if not chk_notna:
return None
return idx
return self.index[idxpos]

@Appender(
_shared_docs["valid_index"] % {"position": "first", "klass": "Series/DataFrame"}
Expand Down
45 changes: 39 additions & 6 deletions pandas/core/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,43 @@ def clean_interp_method(method, **kwargs):
return method


def find_valid_index(values, how: str):
"""
Retrieves the index of the first valid value.

Parameters
----------
values : ndarray or ExtensionArray
how : {'first', 'last'}
Use this parameter to change between the first or last valid index.

Returns
-------
int or None
"""
assert how in ["first", "last"]
Copy link
Member

Choose a reason for hiding this comment

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

I realize this is the existing behavior, but does it make sense to change this to a ValueError instead? That seems more consistent with how we've been validating these types of parameters elsewhere.


if len(values) == 0: # early stop
return None

is_valid = ~isna(values)

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

if how == "first":
idxpos = is_valid[::].argmax()

if how == "last":
idxpos = len(values) - 1 - is_valid[::-1].argmax()

chk_notna = is_valid[idxpos]

if not chk_notna:
return None
return idxpos


def interpolate_1d(
xvalues,
yvalues,
Expand Down Expand Up @@ -192,14 +229,10 @@ def interpolate_1d(
# default limit is unlimited GH #16282
limit = algos._validate_limit(nobs=None, limit=limit)

from pandas import Series

ys = Series(yvalues)

# These are sets of index pointers to invalid values... i.e. {0, 1, etc...
all_nans = set(np.flatnonzero(invalid))
start_nans = set(range(ys.first_valid_index()))
end_nans = set(range(1 + ys.last_valid_index(), len(valid)))
start_nans = set(range(find_valid_index(yvalues, "first")))
end_nans = set(range(1 + find_valid_index(yvalues, "last"), len(valid)))
mid_nans = all_nans - start_nans - end_nans

# Like the sets above, preserve_nans contains indices of invalid values,
Expand Down