Skip to content

Commit 7128ade

Browse files
Backport PR pandas-dev#51585 on branch 2.0.x (BUG: not broadcasting when setting empty object) (pandas-dev#51618)
Backport PR pandas-dev#51585: BUG: not broadcasting when setting empty object Co-authored-by: Patrick Hoefler <[email protected]>
1 parent 0c5a570 commit 7128ade

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,7 @@ Indexing
12761276
- Bug in :meth:`DataFrame.sort_values` where ``None`` was not returned when ``by`` is empty list and ``inplace=True`` (:issue:`50643`)
12771277
- Bug in :meth:`DataFrame.loc` coercing dtypes when setting values with a list indexer (:issue:`49159`)
12781278
- Bug in :meth:`Series.loc` raising error for out of bounds end of slice indexer (:issue:`50161`)
1279+
- Bug in :meth:`DataFrame.loc` raising ``ValueError`` with all ``False`` ``bool`` indexer and empty object (:issue:`51450`)
12791280
- Bug in :meth:`DataFrame.loc` raising ``ValueError`` with ``bool`` indexer and :class:`MultiIndex` (:issue:`47687`)
12801281
- Bug in :meth:`DataFrame.loc` raising ``IndexError`` when setting values for a pyarrow-backed column with a non-scalar indexer (:issue:`50085`)
12811282
- Bug in :meth:`DataFrame.__getitem__`, :meth:`Series.__getitem__`, :meth:`DataFrame.__setitem__` and :meth:`Series.__setitem__`

pandas/core/indexing.py

+2-12
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
)
7070
from pandas.core.indexers import (
7171
check_array_indexer,
72-
is_empty_indexer,
7372
is_list_like_indexer,
7473
is_scalar_indexer,
7574
length_of_indexer,
@@ -755,14 +754,8 @@ def _maybe_mask_setitem_value(self, indexer, value):
755754
if ndim == 1:
756755
# We implicitly broadcast, though numpy does not, see
757756
# github.com/pandas-dev/pandas/pull/45501#discussion_r789071825
758-
if len(newkey) == 0:
759-
# FIXME: kludge for
760-
# test_setitem_loc_only_false_indexer_dtype_changed
761-
# TODO(GH#45333): may be fixed when deprecation is enforced
762-
value = value.iloc[:0]
763-
else:
764-
# test_loc_setitem_ndframe_values_alignment
765-
value = self.obj.iloc._align_series(indexer, value)
757+
# test_loc_setitem_ndframe_values_alignment
758+
value = self.obj.iloc._align_series(indexer, value)
766759
indexer = (newkey, icols)
767760

768761
elif ndim == 2 and value.shape[1] == 1:
@@ -2239,9 +2232,6 @@ def ravel(i):
22392232
# we have a frame, with multiple indexers on both axes; and a
22402233
# series, so need to broadcast (see GH5206)
22412234
if sum_aligners == self.ndim and all(is_sequence(_) for _ in indexer):
2242-
# TODO: This is hacky, align Series and DataFrame behavior GH#45778
2243-
if obj.ndim == 2 and is_empty_indexer(indexer[0]):
2244-
return ser._values.copy()
22452235
ser_values = ser.reindex(obj.axes[0][indexer[0]], copy=True)._values
22462236

22472237
# single indexer

pandas/tests/indexing/test_loc.py

+14
Original file line numberDiff line numberDiff line change
@@ -2599,6 +2599,20 @@ def test_loc_setitem_ndframe_values_alignment(self, using_copy_on_write):
25992599
else:
26002600
tm.assert_frame_equal(df, expected)
26012601

2602+
def test_loc_indexer_empty_broadcast(self):
2603+
# GH#51450
2604+
df = DataFrame({"a": [], "b": []}, dtype=object)
2605+
expected = df.copy()
2606+
df.loc[np.array([], dtype=np.bool_), ["a"]] = df["a"]
2607+
tm.assert_frame_equal(df, expected)
2608+
2609+
def test_loc_indexer_all_false_broadcast(self):
2610+
# GH#51450
2611+
df = DataFrame({"a": ["x"], "b": ["y"]}, dtype=object)
2612+
expected = df.copy()
2613+
df.loc[np.array([False], dtype=np.bool_), ["a"]] = df["b"]
2614+
tm.assert_frame_equal(df, expected)
2615+
26022616

26032617
class TestLocListlike:
26042618
@pytest.mark.parametrize("box", [lambda x: x, np.asarray, list])

0 commit comments

Comments
 (0)