Skip to content

first_valid_index fails when dataframe has non-unique row index #21441

Closed
@adamklein

Description

@adamklein

Code Sample, a copy-pastable example if possible

In [2]: import pandas as pd

In [3]: pd.__version__
Out[3]: '0.23.0rc2'

In [4]: x = pd.DataFrame({'b': [1,2,3]}, index=[1,1,2])

In [5]: x
Out[5]: 
   b
1  1
1  2
2  3

In [6]: x.first_valid_index()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-baa2c482d9a6> in <module>()
----> 1 x.first_valid_index()

lib/python3.5/site-packages/pandas/core/generic.py in first_valid_index(self)
   8881                                              'klass': 'NDFrame'})
   8882     def first_valid_index(self):
-> 8883         return self._find_valid_index('first')
   8884 
   8885     @Appender(_shared_docs['valid_index'] % {'position': 'last',

lib/python3.5/site-packages/pandas/core/generic.py in _find_valid_index(self, how)
   8867             # First valid value case
   8868             i = is_valid.idxmax()
-> 8869             if not is_valid[i]:
   8870                 return None
   8871             return i

lib/python3.5/site-packages/pandas/core/generic.py in __nonzero__(self)
   1572         raise ValueError("The truth value of a {0} is ambiguous. "
   1573                          "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
-> 1574                          .format(self.__class__.__name__))
   1575 
   1576     __bool__ = __nonzero__

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Problem description

first_valid_index did not raise on a row index with duplicate values on pandas <= 0.22.0

Expected Output

0

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.5.4.final.0
python-bits: 64
OS: Linux
OS-release: 4.1.35-pv-ts2
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.23.0
pytest: 3.2.1
pip: 9.0.1
setuptools: 36.4.0
Cython: None
numpy: 1.12.1
scipy: 0.19.1
pyarrow: 0.8.0
xarray: None
IPython: 6.1.0
sphinx: 1.7.5
patsy: 0.4.1
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: None
tables: 3.4.2
numexpr: 2.6.2
feather: None
matplotlib: 2.0.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: 4.6.0
html5lib: 0.9999999
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

cc @jreback

Metadata

Metadata

Assignees

No one assigned

    Labels

    IndexingRelated to indexing on series/frames, not to indexes themselvesRegressionFunctionality that used to work in a prior pandas version

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions