Skip to content

Commit c6ad13d

Browse files
committed
BUG: Setting DataFrame values via iloc aligns when arguments are lists
1 parent 44e3c40 commit c6ad13d

File tree

4 files changed

+25
-12
lines changed

4 files changed

+25
-12
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,7 @@ Indexing
718718
- Bug in :meth:`Series.__getitem__` allowing missing labels with ``np.ndarray``, :class:`Index`, :class:`Series` indexers but not ``list``, these now all raise ``KeyError`` (:issue:`33646`)
719719
- Bug in :meth:`DataFrame.truncate` and :meth:`Series.truncate` where index was assumed to be monotone increasing (:issue:`33756`)
720720
- Indexing with a list of strings representing datetimes failed on :class:`DatetimeIndex` or :class:`PeriodIndex`(:issue:`11278`)
721+
- Bug in :meth:`DataFrame.iloc` when setting values with list arguments would align row/column labels (:issue:`22046`)
721722

722723
Missing
723724
^^^^^^^

pandas/core/indexing.py

+4
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,10 @@ def __setitem__(self, key, value):
662662
indexer = self._get_setitem_indexer(key)
663663
self._has_valid_setitem_indexer(key)
664664

665+
if self.name == "iloc" and isinstance(value, (ABCSeries, ABCDataFrame)):
666+
# Strip labels so as to not align with RHS
667+
value = value._values.copy()
668+
665669
iloc = self if self.name == "iloc" else self.obj.iloc
666670
iloc._setitem_with_indexer(indexer, value)
667671

pandas/tests/indexing/test_iloc.py

+16
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,15 @@ def test_iloc_setitem_categorical_updates_inplace(self):
705705
expected = pd.Categorical(["C", "B", "A"])
706706
tm.assert_categorical_equal(cat, expected)
707707

708+
def test_iloc_setitem_frame_no_alignment(self):
709+
# GH 22046
710+
# setting with iloc should not align labels
711+
df = pd.DataFrame({"a": [1, 2], "b": [2, 3]}, index=[2, 1])
712+
expected = df.copy()
713+
df2 = pd.DataFrame({"b": [1, 2], "a": [2, 3]}, index=[1, 2])
714+
df.iloc[:, [0, 1]] = df2.iloc[:, [0, 1]]
715+
tm.assert_frame_equal(df, expected)
716+
708717

709718
class TestILocSetItemDuplicateColumns:
710719
def test_iloc_setitem_scalar_duplicate_columns(self):
@@ -733,3 +742,10 @@ def test_iloc_setitem_series_duplicate_columns(self):
733742
)
734743
df.iloc[:, 0] = df.iloc[:, 0].astype(np.float64)
735744
assert df.dtypes.iloc[2] == np.int64
745+
746+
def test_iloc_settime_frame_duplicate_columns(self):
747+
idx = pd.MultiIndex.from_tuples((("a", "a"), ("a", "a")))
748+
df = pd.DataFrame([[1, 1]], columns=idx)
749+
expected = pd.DataFrame([[2, 2]], columns=idx)
750+
df.iloc[:, [0, 1]] = expected.iloc[:, [0, 1]]
751+
tm.assert_frame_equal(df, expected)

pandas/tests/indexing/test_indexing.py

+4-12
Original file line numberDiff line numberDiff line change
@@ -716,27 +716,19 @@ def test_rhs_alignment(self):
716716
# GH8258, tests that both rows & columns are aligned to what is
717717
# assigned to. covers both uniform data-type & multi-type cases
718718
def run_tests(df, rhs, right):
719-
# label, index, slice
720-
lbl_one, idx_one, slice_one = list("bcd"), [1, 2, 3], slice(1, 4)
721-
lbl_two, idx_two, slice_two = ["joe", "jolie"], [1, 2], slice(1, 3)
719+
# labels
720+
lbl_one = list("bcd")
721+
lbl_two = ["joe", "jolie"]
722722

723723
left = df.copy()
724724
left.loc[lbl_one, lbl_two] = rhs
725725
tm.assert_frame_equal(left, right)
726726

727-
left = df.copy()
728-
left.iloc[idx_one, idx_two] = rhs
729-
tm.assert_frame_equal(left, right)
730-
731-
left = df.copy()
732-
left.iloc[slice_one, slice_two] = rhs
733-
tm.assert_frame_equal(left, right)
734-
735727
xs = np.arange(20).reshape(5, 4)
736728
cols = ["jim", "joe", "jolie", "joline"]
737729
df = DataFrame(xs, columns=cols, index=list("abcde"))
738730

739-
# right hand side; permute the indices and multiplpy by -2
731+
# right hand side; permute the indices and multiply by -2
740732
rhs = -2 * df.iloc[3:0:-1, 2:0:-1]
741733

742734
# expected `right` result; just multiply by -2

0 commit comments

Comments
 (0)