Skip to content

Too generic Exception in multindex reindex raise statement #21770

Closed
@glyg

Description

@glyg

Code Sample, a copy-pastable example if possible

import pandas as pd, numpy as np

idx = pd.MultiIndex.from_tuples([(0, 0), (1, 1), (1, 1), (2, 2)])
a = pd.Series(np.arange(4), index=idx)
new_idx = pd.MultiIndex.from_tuples([(0, 0), (1, 1), (2, 2)])
a.reindex(new_idx)
(...)  
 2094                 else:
-> 2095                     raise Exception("cannot handle a non-unique multi-index!")
   2096 
   2097         if not isinstance(target, MultiIndex):

Exception: cannot handle a non-unique multi-index!

Problem description

class MultiIndex(Index):
    (...)
    def reindex(self, target, method=None, level=None, limit=None,
        tolerance=None):
        (...)
                else:
                    raise Exception("cannot handle a non-unique multi-index!")

In MultiIndex.reindex, this exception is too generic, and thus not easily filtered. I think it should be an IndexError

Expected Output

raise IndexError("cannot handle a non-unique multi-index!")

I can do a very minimal PR to fix this if needed.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.5.final.0
python-bits: 64
OS: Linux
OS-release: 4.15.0-24-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: fr_FR.UTF-8
LOCALE: fr_FR.UTF-8

pandas: 0.23.1
pytest: 3.6.2
pip: 10.0.1
setuptools: 39.2.0
Cython: 0.28.3
numpy: 1.14.5
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 6.4.0
sphinx: None
patsy: 0.5.0
dateutil: 2.7.3
pytz: 2018.5
blosc: None
bottleneck: None
tables: 3.4.4
numexpr: 2.6.5
feather: None
matplotlib: 2.2.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 1.0.1
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Error ReportingIncorrect or improved errors from pandasIndexingRelated to indexing on series/frames, not to indexes themselvesMultiIndex

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions