Skip to content

df.first_valid_index() fails if there is no valid entry. #17400

Closed
@bjlobo

Description

@bjlobo

pandas/pandas/core/frame.py

Lines 3770 to 3777 in 3a7f956

def first_valid_index(self):
"""
Return label for first non-NA/null value
"""
if len(self) == 0:
return None
return self.index[self.count(1) > 0][0]

import pandas as pd
import numpy as np

df = pd.DataFrame({'a': [np.nan,np.nan]})
df.first_valid_index()

This code results in the following error message, which I believe is called because the boolean series returned by self.count(1) > 0 is all False.

Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    df.first_valid_index()
  File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 3859, in first_valid_index
    return self.index[self.count(1) > 0][0]
  File "C:\Python27\lib\site-packages\pandas\indexes\base.py", line 1423, in __getitem__
    return getitem(key)
IndexError: index 0 is out of bounds for axis 0 with size 0

Should there be a check where None is returned if self.count(1) > 0 is all False?
As an aside, this issue also occurs in df.last_valid_index() for the same reason.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugError ReportingIncorrect or improved errors from pandasIndexingRelated to indexing on series/frames, not to indexes themselves

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions