Skip to content

Commit 953e27b

Browse files
committed
Merge pull request #7006 from jreback/iloc_dups
BUG: duplicate indexing with setitem with iloc (GH6766)
2 parents 79df67a + 0046886 commit 953e27b

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

doc/source/release.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ Bug Fixes
450450
- Bug in enabling ``subplots=True`` in ``DataFrame.plot`` only has single column raises ``TypeError``, and ``Series.plot`` raises ``AttributeError`` (:issue:`6951`)
451451
- Bug in ``DataFrame.plot`` draws unnecessary axes when enabling ``subplots`` and ``kind=scatter`` (:issue:`6951`)
452452
- Bug in ``read_csv`` from a filesystem with non-utf-8 encoding (:issue:`6807`)
453+
- Bug in ``iloc`` when setting / aligning (:issue:``6766`)
453454

454455
pandas 0.13.1
455456
-------------

pandas/core/indexing.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ def setter(item, v):
385385
s._data = s._data.setitem(indexer=pi, value=v)
386386
s._maybe_update_cacher(clear=True)
387387

388+
# reset the sliced object if unique
388389
self.obj[item] = s
389390

390391
def can_do_equal_len():

pandas/tests/test_indexing.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,36 @@ def test_loc_setitem_dups(self):
630630
df.loc[indexer]*=2.0
631631
assert_frame_equal(df.loc[indexer],2.0*df_orig.loc[indexer])
632632

633+
def test_iloc_setitem_dups(self):
634+
635+
# GH 6766
636+
# iloc with a mask aligning from another iloc
637+
df1 = DataFrame([{'A':None, 'B':1},{'A':2, 'B':2}])
638+
df2 = DataFrame([{'A':3, 'B':3},{'A':4, 'B':4}])
639+
df = concat([df1, df2], axis=1)
640+
641+
expected = df.fillna(3)
642+
expected['A'] = expected['A'].astype('float64')
643+
inds = np.isnan(df.iloc[:, 0])
644+
mask = inds[inds].index
645+
df.iloc[mask,0] = df.iloc[mask,2]
646+
assert_frame_equal(df, expected)
647+
648+
# del a dup column across blocks
649+
expected = DataFrame({ 0 : [1,2], 1 : [3,4] })
650+
expected.columns=['B','B']
651+
del df['A']
652+
assert_frame_equal(df, expected)
653+
654+
# assign back to self
655+
df.iloc[[0,1],[0,1]] = df.iloc[[0,1],[0,1]]
656+
assert_frame_equal(df, expected)
657+
658+
# reversed x 2
659+
df.iloc[[1,0],[0,1]] = df.iloc[[1,0],[0,1]].reset_index(drop=True)
660+
df.iloc[[1,0],[0,1]] = df.iloc[[1,0],[0,1]].reset_index(drop=True)
661+
assert_frame_equal(df, expected)
662+
633663
def test_chained_getitem_with_lists(self):
634664

635665
# GH6394

0 commit comments

Comments
 (0)