Skip to content

Commit 85acd65

Browse files
authored
BUG: iloc.__setitem__ with dict value and mixed dtypes (#38335)
1 parent 643d62c commit 85acd65

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

doc/source/whatsnew/v1.3.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ Indexing
129129
^^^^^^^^
130130
- Bug in :meth:`CategoricalIndex.get_indexer` failing to raise ``InvalidIndexError`` when non-unique (:issue:`38372`)
131131
- Bug in inserting many new columns into a :class:`DataFrame` causing incorrect subsequent indexing behavior (:issue:`38380`)
132+
- Bug in :meth:`DataFrame.iloc.__setitem__` and :meth:`DataFrame.loc.__setitem__` with mixed dtypes when setting with a dictionary value (:issue:`38335`)
132133
-
133134
-
134135

pandas/core/indexing.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,8 +1649,10 @@ def _setitem_with_indexer_split_path(self, indexer, value, name: str):
16491649
if isinstance(indexer[0], np.ndarray) and indexer[0].ndim > 2:
16501650
raise ValueError(r"Cannot set values with ndim > 2")
16511651

1652-
if isinstance(value, ABCSeries) and name != "iloc":
1653-
value = self._align_series(indexer, value)
1652+
if (isinstance(value, ABCSeries) and name != "iloc") or isinstance(value, dict):
1653+
from pandas import Series
1654+
1655+
value = self._align_series(indexer, Series(value))
16541656

16551657
# Ensure we have something we can iterate over
16561658
info_axis = indexer[1]

pandas/tests/indexing/test_iloc.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,12 @@ def test_iloc_setitem_dictionary_value(self):
869869
expected = DataFrame({"x": [1, 9], "y": [2, 99]})
870870
tm.assert_frame_equal(df, expected)
871871

872+
# GH#38335 same thing, mixed dtypes
873+
df = DataFrame({"x": [1, 2], "y": [2.0, 2.0]})
874+
df.iloc[1] = rhs
875+
expected = DataFrame({"x": [1, 9], "y": [2.0, 99.0]})
876+
tm.assert_frame_equal(df, expected)
877+
872878

873879
class TestILocErrors:
874880
# NB: this test should work for _any_ Series we can pass as

pandas/tests/indexing/test_indexing.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,13 +741,20 @@ def test_slice_with_zero_step_raises(self):
741741
s.loc[::0]
742742

743743
def test_indexing_assignment_dict_already_exists(self):
744-
df = DataFrame({"x": [1, 2, 6], "y": [2, 2, 8], "z": [-5, 0, 5]}).set_index("z")
744+
index = Index([-5, 0, 5], name="z")
745+
df = DataFrame({"x": [1, 2, 6], "y": [2, 2, 8]}, index=index)
745746
expected = df.copy()
746747
rhs = {"x": 9, "y": 99}
747748
df.loc[5] = rhs
748749
expected.loc[5] = [9, 99]
749750
tm.assert_frame_equal(df, expected)
750751

752+
# GH#38335 same thing, mixed dtypes
753+
df = DataFrame({"x": [1, 2, 6], "y": [2.0, 2.0, 8.0]}, index=index)
754+
df.loc[5] = rhs
755+
expected = DataFrame({"x": [1, 2, 9], "y": [2.0, 2.0, 99.0]}, index=index)
756+
tm.assert_frame_equal(df, expected)
757+
751758
def test_indexing_dtypes_on_empty(self):
752759
# Check that .iloc returns correct dtypes GH9983
753760
df = DataFrame({"a": [1, 2, 3], "b": ["b", "b2", "b3"]})

0 commit comments

Comments
 (0)