Skip to content

Commit

Permalink
DOC: Fix doc for first_valid_index (pandas-dev#55236)
Browse files Browse the repository at this point in the history
* Fix doc for first_valid_index

* Replace notes with examples

* Add explicit prints
  • Loading branch information
jfadia committed Sep 22, 2023
1 parent d9a2ad1 commit 9e1096e
Showing 1 changed file with 41 additions and 5 deletions.
46 changes: 41 additions & 5 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -12466,11 +12466,6 @@ def first_valid_index(self) -> Hashable | None:
-------
type of index
Notes
-----
If all elements are non-NA/null, returns None.
Also returns None for empty {klass}.
Examples
--------
For Series:
Expand All @@ -12481,6 +12476,22 @@ def first_valid_index(self) -> Hashable | None:
>>> s.last_valid_index()
2
>>> s = pd.Series([None, None])
>>> print(s.first_valid_index())
None
>>> print(s.last_valid_index())
None
If all elements in Series are NA/null, returns None.
>>> s = pd.Series()
>>> print(s.first_valid_index())
None
>>> print(s.last_valid_index())
None
If Series is empty, returns None.
For DataFrame:
>>> df = pd.DataFrame({{'A': [None, None, 2], 'B': [None, 3, 4]}})
Expand All @@ -12493,6 +12504,31 @@ def first_valid_index(self) -> Hashable | None:
1
>>> df.last_valid_index()
2
>>> df = pd.DataFrame({{'A': [None, None, None], 'B': [None, None, None]}})
>>> df
A B
0 None None
1 None None
2 None None
>>> print(df.first_valid_index())
None
>>> print(df.last_valid_index())
None
If all elements in DataFrame are NA/null, returns None.
>>> df = pd.DataFrame()
>>> df
Empty DataFrame
Columns: []
Index: []
>>> print(df.first_valid_index())
None
>>> print(df.last_valid_index())
None
If DataFrame is empty, returns None.
"""
return self._find_valid_index(how="first")

Expand Down

0 comments on commit 9e1096e

Please sign in to comment.