Skip to content

Commit 9c611b3

Browse files
committed
CLN: Added doc and fixed variable names.
1 parent a4a90c1 commit 9c611b3

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

pandas/core/generic.py

+19-5
Original file line numberDiff line numberDiff line change
@@ -8763,21 +8763,35 @@ def transform(self, func, *args, **kwargs):
87638763
scalar : type of index
87648764
"""
87658765

8766-
def _find_first_valid(self, direction=1):
8766+
def _find_first_valid(self, reversed=False):
8767+
"""Retrieves the index of the first valid value.
8768+
8769+
Parameters
8770+
----------
8771+
reversed : bool
8772+
If you want invert the original data order (axis=0). Useful to find
8773+
the last valid value.
8774+
8775+
Returns
8776+
-------
8777+
idx_first_valid : type of index
8778+
"""
87678779
if len(self) == 0: # early stop
87688780
return None
87698781
is_valid = ~self.isna()
87708782

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

8774-
if direction == 1:
8786+
if not reversed:
8787+
# first valid value case
87758788
i = is_valid.idxmax()
87768789
if not is_valid[i]:
87778790
return None
87788791
else:
87798792
return i
8780-
elif direction == -1:
8793+
else:
8794+
# last valid value case
87818795
i = is_valid.values[::-1].argmax()
87828796
if not is_valid.iat[len(self) - i - 1]:
87838797
return None
@@ -8787,12 +8801,12 @@ def _find_first_valid(self, direction=1):
87878801
@Appender(_shared_docs['valid_index'] % {'position': 'first',
87888802
'klass': 'NDFrame'})
87898803
def first_valid_index(self):
8790-
return self._find_first_valid(1)
8804+
return self._find_first_valid()
87918805

87928806
@Appender(_shared_docs['valid_index'] % {'position': 'last',
87938807
'klass': 'NDFrame'})
87948808
def last_valid_index(self):
8795-
return self._find_first_valid(-1)
8809+
return self._find_first_valid(True)
87968810

87978811

87988812
def _doc_parms(cls):

0 commit comments

Comments
 (0)