Skip to content

Commit a174898

Browse files
gfyoungjreback
authored andcommitted
BUG: Allow assignment by indexing with duplicate column names
closes #12344 Author: gfyoung <[email protected]> Closes #12498 from gfyoung/dup_name_corrupt and squashes the following commits: 7265d29 [gfyoung] BUG: Allow assignment by indexing with duplicate column names
1 parent 547c784 commit a174898

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

doc/source/whatsnew/v0.18.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1202,3 +1202,4 @@ Bug Fixes
12021202
- Bug when initializing categorical series with a scalar value. (:issue:`12336`)
12031203
- Bug when specifying a UTC ``DatetimeIndex`` by setting ``utc=True`` in ``.to_datetime`` (:issue:`11934`)
12041204
- Bug when increasing the buffer size of CSV reader in ``read_csv`` (:issue:`12494`)
1205+
- Bug when setting columns of a ``DataFrame`` with duplicate column names (:issue:`12344`)

pandas/core/indexing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ def can_do_equal_len():
541541
if (len(indexer) > info_axis and
542542
is_integer(indexer[info_axis]) and
543543
all(is_null_slice(idx) for i, idx in enumerate(indexer)
544-
if i != info_axis)):
544+
if i != info_axis) and item_labels.is_unique):
545545
self.obj[item_labels[indexer[info_axis]]] = value
546546
return
547547

pandas/tests/frame/test_nonunique_indexes.py

+16
Original file line numberDiff line numberDiff line change
@@ -452,3 +452,19 @@ def test_as_matrix_duplicates(self):
452452
dtype=object)
453453

454454
self.assertTrue(np.array_equal(result, expected))
455+
456+
def test_set_value_by_index(self):
457+
# See gh-12344
458+
df = DataFrame(np.arange(9).reshape(3, 3).T)
459+
df.columns = list('AAA')
460+
expected = df.iloc[:, 2]
461+
462+
df.iloc[:, 0] = 3
463+
assert_series_equal(df.iloc[:, 2], expected)
464+
465+
df = DataFrame(np.arange(9).reshape(3, 3).T)
466+
df.columns = [2, float(2), str(2)]
467+
expected = df.iloc[:, 1]
468+
469+
df.iloc[:, 0] = 3
470+
assert_series_equal(df.iloc[:, 1], expected)

0 commit comments

Comments
 (0)