From 844a4ffa36751be22d9f429f811dd9fdb6e7874d Mon Sep 17 00:00:00 2001 From: jreback Date: Wed, 11 Dec 2013 09:29:28 -0500 Subject: [PATCH] BUG: Bug in repeated indexing of object with resultant non-unique index (GH5678) --- doc/source/release.rst | 1 + pandas/core/series.py | 5 ++++- pandas/tests/test_indexing.py | 8 ++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/doc/source/release.rst b/doc/source/release.rst index 8a911a6e41d0b..f6cbbd23011a8 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -821,6 +821,7 @@ Bug Fixes - Bug in selecting from a non-unique index with ``loc`` (:issue:`5553`) - Bug in groupby returning non-consistent types when user function returns a ``None``, (:issue:`5592`) - Work around regression in numpy 1.7.0 which erroneously raises IndexError from ``ndarray.item`` (:issue:`5666`) + - Bug in repeated indexing of object with resultant non-unique index (:issue:`5678`) pandas 0.12.0 ------------- diff --git a/pandas/core/series.py b/pandas/core/series.py index bc9f6af61b9ec..ecfd99e61a090 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -481,7 +481,10 @@ def _slice(self, slobj, axis=0, raise_on_error=False, typ=None): def __getitem__(self, key): try: - return self.index.get_value(self, key) + result = self.index.get_value(self, key) + if isinstance(result, np.ndarray): + return self._constructor(result,index=[key]*len(result)).__finalize__(self) + return result except InvalidIndexError: pass except (KeyError, ValueError): diff --git a/pandas/tests/test_indexing.py b/pandas/tests/test_indexing.py index b6e7b10232bf5..d941224fec52e 100644 --- a/pandas/tests/test_indexing.py +++ b/pandas/tests/test_indexing.py @@ -316,6 +316,14 @@ def test_at_timestamp(self): def test_iat_invalid_args(self): pass + def test_repeated_getitem_dups(self): + # GH 5678 + # repeated gettitems on a dup index returing a ndarray + df = DataFrame(np.random.random_sample((20,5)), index=['ABCDE'[x%5] for x in range(20)]) + expected = df.loc['A',0] + result = df.loc[:,0].loc['A'] + assert_series_equal(result,expected) + def test_iloc_getitem_int(self): # integer