Skip to content

BUG: Bug in setitem with a duplicate index and an alignable rhs (GH6541) #6542

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
Mar 4, 2014
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 @@ -199,6 +199,7 @@ Bug Fixes
- Bug that caused _ref_locs corruption when slice indexing across columns axis of a DataFrame (:issue:`6525`)
- Regression from 0.13 in the treatmenet of numpy ``datetime64`` non-ns dtypes in Series creation (:issue:`6529`)
- ``.names`` attribute of MultiIndexes passed to ``set_index`` are now preserved (:issue:`6459`).
- Bug in setitem with a duplicate index and an alignable rhs (:issue:`6541`)

pandas 0.13.1
-------------
Expand Down
9 changes: 7 additions & 2 deletions pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -987,8 +987,13 @@ def intersection(self, other):
except TypeError:
pass

indexer = self.get_indexer(other.values)
indexer = indexer.take((indexer != -1).nonzero()[0])
try:
indexer = self.get_indexer(other.values)
indexer = indexer.take((indexer != -1).nonzero()[0])
except:
# duplicates
indexer = self.get_indexer_non_unique(other.values)[0].unique()

return self.take(indexer)

def diff(self, other):
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,9 @@ def can_do_equal_len():
# align to
if item in value:
v = value[item]
v = v.reindex(self.obj[item].index & v.index)
i = self.obj[item].index
v = v.reindex(i & v.index)

setter(item, v.values)
else:
setter(item, np.nan)
Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,29 @@ def test_loc_setitem(self):
expected = DataFrame({'a' : [0.5,-0.5,-1.5], 'b' : [0,1,2] })
assert_frame_equal(df,expected)

def test_loc_setitem_dups(self):

# GH 6541
df_orig = DataFrame({'me' : list('rttti'),
'foo': list('aaade'),
'bar': np.arange(5,dtype='float64')*1.34+2,
'bar2': np.arange(5,dtype='float64')*-.34+2}).set_index('me')

indexer = tuple(['r',['bar','bar2']])
df = df_orig.copy()
df.loc[indexer]*=2.0
assert_series_equal(df.loc[indexer],2.0*df_orig.loc[indexer])

indexer = tuple(['r','bar'])
df = df_orig.copy()
df.loc[indexer]*=2.0
self.assertEqual(df.loc[indexer],2.0*df_orig.loc[indexer])

indexer = tuple(['t',['bar','bar2']])
df = df_orig.copy()
df.loc[indexer]*=2.0
assert_frame_equal(df.loc[indexer],2.0*df_orig.loc[indexer])

def test_chained_getitem_with_lists(self):

# GH6394
Expand Down