pandas.Series.last_valid_index#
- Series.last_valid_index()[source]#
Return index for last non-missing value or None, if no value is found.
See the User Guide for more information on which values are considered missing.
- Returns:
- type of index
Index of last non-missing value.
See also
DataFrame.last_valid_index
Return index for last non-NA value or None, if no non-NA value is found.
Series.last_valid_index
Return index for last non-NA value or None, if no non-NA value is found.
DataFrame.isna
Detect missing values.
Examples
For Series:
>>> s = pd.Series([None, 3, 4]) >>> s.first_valid_index() 1 >>> 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]}) >>> df A B 0 NaN NaN 1 NaN 3.0 2 2.0 4.0 >>> df.first_valid_index() 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.