Skip to content

Commit 876e568

Browse files
glygjreback
authored andcommitted
Replaces Exceptions with ValueError in MultiIndex, closes #21770 (#21780)
1 parent de664e8 commit 876e568

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

doc/source/whatsnew/v0.24.0.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ Other API Changes
190190

191191
.. _whatsnew_0240.api.incompatibilities:
192192

193+
- Trying to reindex a ``DataFrame`` with a non unique ``MultiIndex`` now raises a ``ValueError`` instead of an ``Exception`` (:issue:`21770`)
194+
193195
Series and Index Data-Dtype Incompatibilities
194196
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
195197

pandas/core/indexes/multi.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1958,8 +1958,8 @@ def get_indexer(self, target, method=None, limit=None, tolerance=None):
19581958
tolerance=tolerance)
19591959

19601960
if not self.is_unique:
1961-
raise Exception('Reindexing only valid with uniquely valued Index '
1962-
'objects')
1961+
raise ValueError('Reindexing only valid with uniquely valued '
1962+
'Index objects')
19631963

19641964
if method == 'pad' or method == 'backfill':
19651965
if tolerance is not None:
@@ -2023,7 +2023,7 @@ def reindex(self, target, method=None, level=None, limit=None,
20232023
limit=limit,
20242024
tolerance=tolerance)
20252025
else:
2026-
raise Exception("cannot handle a non-unique multi-index!")
2026+
raise ValueError("cannot handle a non-unique multi-index!")
20272027

20282028
if not isinstance(target, MultiIndex):
20292029
if indexer is None:

pandas/tests/indexes/multi/test_reindex.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,12 @@ def test_reindex_base(idx):
9797

9898
with tm.assert_raises_regex(ValueError, 'Invalid fill method'):
9999
idx.get_indexer(idx, method='invalid')
100+
101+
102+
def test_reindex_non_unique():
103+
idx = pd.MultiIndex.from_tuples([(0, 0), (1, 1), (1, 1), (2, 2)])
104+
a = pd.Series(np.arange(4), index=idx)
105+
new_idx = pd.MultiIndex.from_tuples([(0, 0), (1, 1), (2, 2)])
106+
with tm.assert_raises_regex(ValueError,
107+
'cannot handle a non-unique multi-index!'):
108+
a.reindex(new_idx)

0 commit comments

Comments
 (0)