From 3a8b0f83a52e1ad87b7ccd19d50365fdb7f5e603 Mon Sep 17 00:00:00 2001 From: Chang She Date: Fri, 2 Nov 2012 11:40:25 -0400 Subject: [PATCH] BUG: get_indexer for MultiIndex does not raise Exception for non-unique #2101 --- pandas/core/index.py | 4 ++++ pandas/tests/test_frame.py | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/pandas/core/index.py b/pandas/core/index.py index 6629024e4d033..5c85d48536081 100644 --- a/pandas/core/index.py +++ b/pandas/core/index.py @@ -1894,6 +1894,10 @@ def get_indexer(self, target, method=None, limit=None): if target_index.dtype != object: return np.ones(len(target_index)) * -1 + if not self.is_unique: + raise Exception('Reindexing only valid with uniquely valued Index ' + 'objects') + self_index = self._tuple_index if method == 'pad': diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index c989e8c981231..214847e423d5a 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -4339,6 +4339,21 @@ def test_drop_col_still_multiindex(self): del df[('a','','')] assert(isinstance(df.columns, MultiIndex)) + def test_drop_nonuq_multiindex(self): + df = DataFrame([["x-a", "x", "a", 1.5],["x-a", "x", "a", 1.2], + ["z-c", "z", "c", 3.1], ["x-a", "x", "a", 4.1], + ["x-b", "x", "b", 5.1],["x-b", "x", "b", 4.1], + ["x-b", "x", "b", 2.2], + ["y-a", "y", "a", 1.2],["z-b", "z", "b", 2.1]], + columns=["var1", "var2", "var3", "var4"]) + + grp_size = df.groupby("var1").size() + drop_idx = grp_size.ix[grp_size == 1] + + df = df.set_index(["var1", "var2", "var3"]) + + self.assertRaises(Exception, df.drop, drop_idx.index, level=0) + def test_fillna(self): self.tsframe['A'][:5] = nan self.tsframe['A'][-5:] = nan