Skip to content

BUG: Fix selection with ix/loc and non_unique selectors (GH4619) #4622

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

Merged
merged 1 commit into from
Aug 21, 2013
Merged
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
1 change: 1 addition & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ See :ref:`Internal Refactoring<whatsnew_0130.refactoring>`
to a possible lazay frequency inference issue (:issue:`3317`)
- Fixed issue where ``DataFrame.apply`` was reraising exceptions incorrectly
(causing the original stack trace to be truncated).
- Fix selection with ``ix/loc`` and non_unique selectors (:issue:`4619`)

pandas 0.12
===========
Expand Down
17 changes: 14 additions & 3 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,12 @@ def _reindex(keys, level=None):
else:
level = None

if labels.is_unique and Index(keyarr).is_unique:
keyarr_is_unique = Index(keyarr).is_unique

# existing labels are unique and indexer is unique
if labels.is_unique and keyarr_is_unique:
return _reindex(keyarr, level=level)

else:
indexer, missing = labels.get_indexer_non_unique(keyarr)
check = indexer != -1
Expand All @@ -496,8 +500,15 @@ def _reindex(keys, level=None):
new_labels = np.empty(tuple([len(indexer)]),dtype=object)
new_labels[cur_indexer] = cur_labels
new_labels[missing_indexer] = missing_labels
new_indexer = (Index(cur_indexer) + Index(missing_indexer)).values
new_indexer[missing_indexer] = -1

# a unique indexer
if keyarr_is_unique:
new_indexer = (Index(cur_indexer) + Index(missing_indexer)).values
new_indexer[missing_indexer] = -1

# we have a non_unique selector, need to use the original indexer here
else:
new_indexer = indexer

# reindex with the specified axis
ndim = self.obj.ndim
Expand Down
36 changes: 27 additions & 9 deletions pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -796,27 +796,45 @@ def test_dups_fancy_indexing(self):
assert_frame_equal(df,result)

# GH 3561, dups not in selected order
ind = ['A', 'A', 'B', 'C']
df = DataFrame({'test':lrange(len(ind))}, index=ind)
df = DataFrame({'test': [5,7,9,11]}, index=['A', 'A', 'B', 'C'])
rows = ['C', 'B']
res = df.ix[rows]
self.assert_(rows == list(res.index))
expected = DataFrame({'test' : [11,9]},index=rows)
result = df.ix[rows]
assert_frame_equal(result, expected)

res = df.ix[Index(rows)]
self.assert_(Index(rows).equals(res.index))
result = df.ix[Index(rows)]
assert_frame_equal(result, expected)

rows = ['C','B','E']
res = df.ix[rows]
self.assert_(rows == list(res.index))
expected = DataFrame({'test' : [11,9,np.nan]},index=rows)
result = df.ix[rows]
assert_frame_equal(result, expected)

# inconcistent returns for unique/duplicate indices when values are missing
# inconsistent returns for unique/duplicate indices when values are missing
df = DataFrame(randn(4,3),index=list('ABCD'))
expected = df.ix[['E']]

dfnu = DataFrame(randn(5,3),index=list('AABCD'))
result = dfnu.ix[['E']]
assert_frame_equal(result, expected)

# GH 4619; duplicate indexer with missing label
df = DataFrame({"A": [0, 1, 2]})
result = df.ix[[0,8,0]]
expected = DataFrame({"A": [0, np.nan, 0]},index=[0,8,0])
assert_frame_equal(result,expected)

df = DataFrame({"A": list('abc')})
result = df.ix[[0,8,0]]
expected = DataFrame({"A": ['a', np.nan, 'a']},index=[0,8,0])
assert_frame_equal(result,expected)

# non unique with non unique selector
df = DataFrame({'test': [5,7,9,11]}, index=['A','A','B','C'])
expected = DataFrame({'test' : [5,7,5,7,np.nan]},index=['A','A','A','A','E'])
result = df.ix[['A','A','E']]
assert_frame_equal(result, expected)

def test_indexing_mixed_frame_bug(self):

# GH3492
Expand Down