Skip to content

Commit e6aaf5e

Browse files
committed
Merge pull request #5555 from jreback/loc_bug
BUG: Bug in selecting from a non-unique index with loc (GH5553)
2 parents 312f777 + d4ad3a5 commit e6aaf5e

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

doc/source/release.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,7 @@ Bug Fixes
813813
- Bug in getitem with a multi-index and ``iloc`` (:issue:`5528`)
814814
- Bug in delitem on a Series (:issue:`5542`)
815815
- Bug fix in apply when using custom function and objects are not mutated (:issue:`5545`)
816+
- Bug in selecting from a non-unique index with ``loc`` (:issue:`5553`)
816817

817818
pandas 0.12.0
818819
-------------

pandas/core/indexing.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -826,8 +826,10 @@ def _reindex(keys, level=None):
826826

827827
# a unique indexer
828828
if keyarr_is_unique:
829-
new_indexer = (Index(cur_indexer) +
830-
Index(missing_indexer)).values
829+
830+
# see GH5553, make sure we use the right indexer
831+
new_indexer = np.arange(len(indexer))
832+
new_indexer[cur_indexer] = np.arange(len(result._get_axis(axis)))
831833
new_indexer[missing_indexer] = -1
832834

833835
# we have a non_unique selector, need to use the original

pandas/tests/test_indexing.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -869,17 +869,25 @@ def test_dups_fancy_indexing(self):
869869
assert_frame_equal(df,result)
870870

871871
# GH 3561, dups not in selected order
872-
df = DataFrame({'test': [5,7,9,11]}, index=['A', 'A', 'B', 'C'])
872+
df = DataFrame({'test': [5,7,9,11], 'test1': [4.,5,6,7], 'other': list('abcd') }, index=['A', 'A', 'B', 'C'])
873873
rows = ['C', 'B']
874-
expected = DataFrame({'test' : [11,9]},index=rows)
874+
expected = DataFrame({'test' : [11,9], 'test1': [ 7., 6], 'other': ['d','c']},index=rows)
875875
result = df.ix[rows]
876876
assert_frame_equal(result, expected)
877877

878878
result = df.ix[Index(rows)]
879879
assert_frame_equal(result, expected)
880880

881881
rows = ['C','B','E']
882-
expected = DataFrame({'test' : [11,9,np.nan]},index=rows)
882+
expected = DataFrame({'test' : [11,9,np.nan], 'test1': [7.,6,np.nan], 'other': ['d','c',np.nan]},index=rows)
883+
result = df.ix[rows]
884+
assert_frame_equal(result, expected)
885+
886+
# see GH5553, make sure we use the right indexer
887+
rows = ['F','G','H','C','B','E']
888+
expected = DataFrame({'test' : [np.nan,np.nan,np.nan,11,9,np.nan],
889+
'test1': [np.nan,np.nan,np.nan,7.,6,np.nan],
890+
'other': [np.nan,np.nan,np.nan,'d','c',np.nan]},index=rows)
883891
result = df.ix[rows]
884892
assert_frame_equal(result, expected)
885893

0 commit comments

Comments
 (0)