Skip to content

BUG: ix should return a Series for duplicate indices #7390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down
8 changes: 7 additions & 1 deletion pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)