Skip to content

BUG: Fixed an issue with a duplicate index and duplicate selector with loc (GH4825) #4833

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
Sep 16, 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 @@ -411,6 +411,7 @@ Bug Fixes
- Fixed an issue related to ticklocs/ticklabels with log scale bar plots
across different versions of matplotlib (:issue:`4789`)
- Suppressed DeprecationWarning associated with internal calls issued by repr() (:issue:`4391`)
- Fixed an issue with a duplicate index and duplicate selector with ``.loc`` (:issue:`4825`)

pandas 0.12.0
-------------
Expand Down
18 changes: 13 additions & 5 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,19 +701,27 @@ def _reindex(keys, level=None):
new_labels[cur_indexer] = cur_labels
new_labels[missing_indexer] = missing_labels

# reindex with the specified axis
ndim = self.obj.ndim
if axis+1 > ndim:
raise AssertionError("invalid indexing error with non-unique index")

# 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
if axis+1 > ndim:
raise AssertionError("invalid indexing error with non-unique index")
# need to retake to have the same size as the indexer
rindexer = indexer.values
rindexer[~check] = 0
result = self.obj.take(rindexer, axis=axis, convert=False)

# reset the new indexer to account for the new size
new_indexer = np.arange(len(result))
new_indexer[~check] = -1

result = result._reindex_with_indexers({ axis : [ new_labels, new_indexer ] }, copy=True, allow_dups=True)

Expand Down
43 changes: 43 additions & 0 deletions pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1436,6 +1436,49 @@ def f():
p.loc[:,:,'C'] = Series([30,32],index=p_orig.items)
assert_panel_equal(p,expected)

def test_series_partial_set(self):
# partial set with new index
# Regression from GH4825
ser = Series([0.1, 0.2], index=[1, 2])

# loc
expected = Series([np.nan, 0.2, np.nan], index=[3, 2, 3])
result = ser.loc[[3, 2, 3]]
assert_series_equal(result, expected)

expected = Series([np.nan, np.nan, np.nan], index=[3, 3, 3])
result = ser.loc[[3, 3, 3]]
assert_series_equal(result, expected)

expected = Series([0.2, 0.2, np.nan], index=[2, 2, 3])
result = ser.loc[[2, 2, 3]]
assert_series_equal(result, expected)

expected = Series([0.3, np.nan, np.nan], index=[3, 4, 4])
result = Series([0.1, 0.2, 0.3], index=[1,2,3]).loc[[3,4,4]]
assert_series_equal(result, expected)

expected = Series([np.nan, 0.3, 0.3], index=[5, 3, 3])
result = Series([0.1, 0.2, 0.3, 0.4], index=[1,2,3,4]).loc[[5,3,3]]
assert_series_equal(result, expected)

expected = Series([np.nan, 0.4, 0.4], index=[5, 4, 4])
result = Series([0.1, 0.2, 0.3, 0.4], index=[1,2,3,4]).loc[[5,4,4]]
assert_series_equal(result, expected)

expected = Series([0.4, np.nan, np.nan], index=[7, 2, 2])
result = Series([0.1, 0.2, 0.3, 0.4], index=[4,5,6,7]).loc[[7,2,2]]
assert_series_equal(result, expected)

expected = Series([0.4, np.nan, np.nan], index=[4, 5, 5])
result = Series([0.1, 0.2, 0.3, 0.4], index=[1,2,3,4]).loc[[4,5,5]]
assert_series_equal(result, expected)

# iloc
expected = Series([0.2,0.2,0.1,0.1], index=[2,2,1,1])
result = ser.iloc[[1,1,0,0]]
assert_series_equal(result, expected)

if __name__ == '__main__':
import nose
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
Expand Down