Skip to content

Commit 881b6b2

Browse files
committed
CLN: Added doc and fixed variable names.
1 parent 4408e95 commit 881b6b2

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

pandas/core/generic.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8763,36 +8763,51 @@ def transform(self, func, *args, **kwargs):
87638763
scalar : type of index
87648764
"""
87658765

8766-
def _find_first_valid(self, direction=1):
8766+
def _find_valid_index(self, how):
8767+
"""Retrieves the index of the first valid value.
8768+
8769+
Parameters
8770+
----------
8771+
how : {'first', 'last'}
8772+
Use this parameter to change between the first or last valid index.
8773+
8774+
Returns
8775+
-------
8776+
idx_first_valid : type of index
8777+
"""
87678778
if len(self) == 0: # early stop
87688779
return None
87698780
is_valid = ~self.isna()
87708781

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

8774-
if direction == 1:
8785+
if how == 'first':
8786+
# First valid value case
87758787
i = is_valid.idxmax()
87768788
if not is_valid[i]:
87778789
return None
87788790
else:
87798791
return i
8780-
elif direction == -1:
8792+
elif how == 'last':
8793+
# Last valid value case
87818794
i = is_valid.values[::-1].argmax()
87828795
if not is_valid.iat[len(self) - i - 1]:
87838796
return None
87848797
else:
87858798
return self.index[len(self) - i - 1]
8799+
else:
8800+
raise ValueError
87868801

87878802
@Appender(_shared_docs['valid_index'] % {'position': 'first',
87888803
'klass': 'NDFrame'})
87898804
def first_valid_index(self):
8790-
return self._find_first_valid(1)
8805+
return self._find_valid_index('first')
87918806

87928807
@Appender(_shared_docs['valid_index'] % {'position': 'last',
87938808
'klass': 'NDFrame'})
87948809
def last_valid_index(self):
8795-
return self._find_first_valid(-1)
8810+
return self._find_valid_index('last')
87968811

87978812

87988813
def _doc_parms(cls):

0 commit comments

Comments
 (0)