From cfd1f37843cb9594c212c61ee4e0696260098679 Mon Sep 17 00:00:00 2001 From: Phillip Cloud Date: Sat, 7 Jun 2014 17:29:17 -0400 Subject: [PATCH] BUG: ix should return a Series for duplicate indices --- pandas/core/indexing.py | 17 ++++++++++++++--- pandas/tests/test_indexing.py | 8 +++++++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 68e5810751d08..c47fcb3a98347 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -36,6 +36,18 @@ class IndexingError(Exception): pass +def _reconstruct_from_dup_ix(key, obj, values): + if np.isscalar(values): + return values + assert len(key) == obj.ndim + + kwargs = dict() + for axis, name in obj._AXIS_NAMES.items(): + index = getattr(obj, name) + kwargs[name] = index[index.get_loc(key[axis])] + return obj._constructor(values, **kwargs).squeeze() + + class _NDFrameIndexer(object): _valid_types = None _exception = KeyError @@ -61,7 +73,8 @@ def __iter__(self): def __getitem__(self, key): if type(key) is tuple: try: - return self.obj.get_value(*key) + values = self.obj.get_value(*key) + return _reconstruct_from_dup_ix(key, self.obj, values) except Exception: pass @@ -1101,8 +1114,6 @@ class _IXIndexer(_NDFrameIndexer): """ A primarily location based indexer, with integer fallback """ def _has_valid_type(self, key, axis): - ax = self.obj._get_axis(axis) - if isinstance(key, slice): return True diff --git a/pandas/tests/test_indexing.py b/pandas/tests/test_indexing.py index 96c67f2ff795c..7610ccc6cdf73 100644 --- a/pandas/tests/test_indexing.py +++ b/pandas/tests/test_indexing.py @@ -3570,8 +3570,14 @@ def test_float_index_to_mixed(self): 'a': [10] * 10}), df) + def test_duplicate_ix_returns_series(self): + df = DataFrame(np.random.randn(3, 3), index=[0.1, 0.2, 0.2], + columns=list('abc')) + r = df.ix[0.2, 'a'] + e = df.loc[0.2, 'a'] + tm.assert_series_equal(r, e) + if __name__ == '__main__': - import nose nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'], exit=False)