Skip to content

Commit 4fddc41

Browse files
authored
BUG: DataFrame.loc not aligning rhs df for single block case (#47581)
1 parent f8b3585 commit 4fddc41

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

doc/source/whatsnew/v1.5.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,7 @@ Indexing
882882
- Bug in :meth:`Series.__setitem__` when setting ``boolean`` dtype values containing ``NA`` incorrectly raising instead of casting to ``boolean`` dtype (:issue:`45462`)
883883
- Bug in :meth:`Series.__setitem__` where setting :attr:`NA` into a numeric-dtype :class:`Series` would incorrectly upcast to object-dtype rather than treating the value as ``np.nan`` (:issue:`44199`)
884884
- Bug in :meth:`DataFrame.loc` when setting values to a column and right hand side is a dictionary (:issue:`47216`)
885+
- Bug in :meth:`DataFrame.loc` when setting a :class:`DataFrame` not aligning index in some cases (:issue:`47578`)
885886
- Bug in :meth:`Series.__setitem__` with ``datetime64[ns]`` dtype, an all-``False`` boolean mask, and an incompatible value incorrectly casting to ``object`` instead of retaining ``datetime64[ns]`` dtype (:issue:`45967`)
886887
- Bug in :meth:`Index.__getitem__` raising ``ValueError`` when indexer is from boolean dtype with ``NA`` (:issue:`45806`)
887888
- Bug in :meth:`Series.__setitem__` losing precision when enlarging :class:`Series` with scalar (:issue:`32346`)

pandas/core/frame.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -4706,10 +4706,11 @@ def _sanitize_column(self, value) -> ArrayLike:
47064706
"""
47074707
self._ensure_valid_index(value)
47084708

4709-
# We should never get here with DataFrame value
4710-
if isinstance(value, Series):
4709+
# We can get there through isetitem with a DataFrame
4710+
# or through loc single_block_path
4711+
if isinstance(value, DataFrame):
47114712
return _reindex_for_setitem(value, self.index)
4712-
elif isinstance(value, dict):
4713+
elif is_dict_like(value):
47134714
return _reindex_for_setitem(Series(value), self.index)
47144715

47154716
if is_list_like(value):

pandas/tests/frame/indexing/test_indexing.py

+9
Original file line numberDiff line numberDiff line change
@@ -1298,6 +1298,15 @@ def test_loc_expand_empty_frame_keep_midx_names(self):
12981298
)
12991299
tm.assert_frame_equal(df, expected)
13001300

1301+
@pytest.mark.parametrize("val", ["x", 1])
1302+
@pytest.mark.parametrize("idxr", ["a", ["a"]])
1303+
def test_loc_setitem_rhs_frame(self, idxr, val):
1304+
# GH#47578
1305+
df = DataFrame({"a": [1, 2]})
1306+
df.loc[:, "a"] = DataFrame({"a": [val, 11]}, index=[1, 2])
1307+
expected = DataFrame({"a": [np.nan, val]})
1308+
tm.assert_frame_equal(df, expected)
1309+
13011310

13021311
class TestDataFrameIndexingUInt64:
13031312
def test_setitem(self, uint64_frame):

pandas/tests/frame/indexing/test_setitem.py

+11
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,17 @@ def test_setitem_aligning_dict_with_index(self, vals):
721721
)
722722
tm.assert_frame_equal(df, expected)
723723

724+
def test_setitem_rhs_dataframe(self):
725+
# GH#47578
726+
df = DataFrame({"a": [1, 2]})
727+
df["a"] = DataFrame({"a": [10, 11]}, index=[1, 2])
728+
expected = DataFrame({"a": [np.nan, 10]})
729+
tm.assert_frame_equal(df, expected)
730+
731+
df = DataFrame({"a": [1, 2]})
732+
df.isetitem(0, DataFrame({"a": [10, 11]}, index=[1, 2]))
733+
tm.assert_frame_equal(df, expected)
734+
724735

725736
class TestSetitemTZAwareValues:
726737
@pytest.fixture

0 commit comments

Comments
 (0)