Skip to content

Commit 19c5eea

Browse files
authored
BUG: loc.setitem with expansion expanding rows (#37932)
* BUG: loc.setitem with expansion expanding rows * split test * whatsnew
1 parent 5abc06f commit 19c5eea

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,7 @@ Indexing
622622
- Bug in :meth:`Series.loc` and :meth:`DataFrame.loc` raises when the index was of ``object`` dtype and the given numeric label was in the index (:issue:`26491`)
623623
- Bug in :meth:`DataFrame.loc` returned requested key plus missing values when ``loc`` was applied to single level from a :class:`MultiIndex` (:issue:`27104`)
624624
- Bug in indexing on a :class:`Series` or :class:`DataFrame` with a :class:`CategoricalIndex` using a listlike indexer containing NA values (:issue:`37722`)
625+
- Bug in :meth:`DataFrame.loc.__setitem__` expanding an empty :class:`DataFrame` with mixed dtypes (:issue:`37932`)
625626
- Bug in :meth:`DataFrame.xs` ignored ``droplevel=False`` for columns (:issue:`19056`)
626627
- Bug in :meth:`DataFrame.reindex` raising ``IndexingError`` wrongly for empty DataFrame with ``tolerance`` not None or ``method="nearest"`` (:issue:`27315`)
627628
- Bug in indexing on a :class:`Series` or :class:`DataFrame` with a :class:`CategoricalIndex` using listlike indexer that contains elements that are in the index's ``categories`` but not in the index itself failing to raise ``KeyError`` (:issue:`37901`)

pandas/core/indexing.py

+8
Original file line numberDiff line numberDiff line change
@@ -1694,6 +1694,14 @@ def _setitem_with_indexer_split_path(self, indexer, value, name: str):
16941694
for loc, v in zip(ilocs, value):
16951695
self._setitem_single_column(loc, v, pi)
16961696

1697+
elif len(ilocs) == 1 and com.is_null_slice(pi) and len(self.obj) == 0:
1698+
# This is a setitem-with-expansion, see
1699+
# test_loc_setitem_empty_append_expands_rows_mixed_dtype
1700+
# e.g. df = DataFrame(columns=["x", "y"])
1701+
# df["x"] = df["x"].astype(np.int64)
1702+
# df.loc[:, "x"] = [1, 2, 3]
1703+
self._setitem_single_column(ilocs[0], value, pi)
1704+
16971705
else:
16981706
raise ValueError(
16991707
"Must have equal len keys and value "

pandas/tests/indexing/test_loc.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,7 @@ def test_loc_uint64(self):
952952
result = s.loc[[np.iinfo("uint64").max - 1, np.iinfo("uint64").max]]
953953
tm.assert_series_equal(result, s)
954954

955-
def test_loc_setitem_empty_append(self):
955+
def test_loc_setitem_empty_append_expands_rows(self):
956956
# GH6173, various appends to an empty dataframe
957957

958958
data = [1, 2, 3]
@@ -963,6 +963,18 @@ def test_loc_setitem_empty_append(self):
963963
df.loc[:, "x"] = data
964964
tm.assert_frame_equal(df, expected)
965965

966+
def test_loc_setitem_empty_append_expands_rows_mixed_dtype(self):
967+
# GH#37932 same as test_loc_setitem_empty_append_expands_rows
968+
# but with mixed dtype so we go through take_split_path
969+
data = [1, 2, 3]
970+
expected = DataFrame({"x": data, "y": [None] * len(data)})
971+
972+
df = DataFrame(columns=["x", "y"])
973+
df["x"] = df["x"].astype(np.int64)
974+
df.loc[:, "x"] = data
975+
tm.assert_frame_equal(df, expected)
976+
977+
def test_loc_setitem_empty_append_single_value(self):
966978
# only appends one value
967979
expected = DataFrame({"x": [1.0], "y": [np.nan]})
968980
df = DataFrame(columns=["x", "y"], dtype=float)

0 commit comments

Comments
 (0)