Skip to content

Commit d6f9a53

Browse files
Backport PR #43450: Regression in loc setitem raising ValueError when setting array as cell value (#43453)
Co-authored-by: Patrick Hoefler <[email protected]>
1 parent 93f2de9 commit d6f9a53

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

doc/source/whatsnew/v1.3.3.rst

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Fixed regressions
2121
- Fixed regression in :meth:`merge` where ``on`` columns with ``ExtensionDtype`` or ``bool`` data types were cast to ``object`` in ``right`` and ``outer`` merge (:issue:`40073`)
2222
- Fixed regression in :meth:`RangeIndex.where` and :meth:`RangeIndex.putmask` raising ``AssertionError`` when result did not represent a :class:`RangeIndex` (:issue:`43240`)
2323
- Fixed regression in :meth:`read_parquet` where the ``fastparquet`` engine would not work properly with fastparquet 0.7.0 (:issue:`43075`)
24+
- Fixed regression in :meth:`DataFrame.loc.__setitem__` raising ``ValueError`` when setting array as cell value (:issue:`43422`)
2425
- Fixed regression in :func:`is_list_like` where objects with ``__iter__`` set to ``None`` would be identified as iterable (:issue:`43373`)
2526
- Fixed regression in :meth:`.Resampler.aggregate` when used after column selection would raise if ``func`` is a list of aggregation functions (:issue:`42905`)
2627
- Fixed regression in :meth:`DataFrame.corr` where Kendall correlation would produce incorrect results for columns with repeated values (:issue:`43401`)

pandas/core/dtypes/cast.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -2184,6 +2184,11 @@ def can_hold_element(arr: ArrayLike, element: Any) -> bool:
21842184
# ExtensionBlock._can_hold_element
21852185
return True
21862186

2187+
# error: Non-overlapping equality check (left operand type: "dtype[Any]", right
2188+
# operand type: "Type[object]")
2189+
if dtype == object: # type: ignore[comparison-overlap]
2190+
return True
2191+
21872192
tipo = maybe_infer_dtype_type(element)
21882193

21892194
if dtype.kind in ["i", "u"]:
@@ -2231,11 +2236,6 @@ def can_hold_element(arr: ArrayLike, element: Any) -> bool:
22312236
return tipo.kind == "b"
22322237
return lib.is_bool(element)
22332238

2234-
# error: Non-overlapping equality check (left operand type: "dtype[Any]", right
2235-
# operand type: "Type[object]")
2236-
elif dtype == object: # type: ignore[comparison-overlap]
2237-
return True
2238-
22392239
elif dtype.kind == "S":
22402240
# TODO: test tests.frame.methods.test_replace tests get here,
22412241
# need more targeted tests. xref phofl has a PR about this

pandas/tests/frame/indexing/test_indexing.py

+7
Original file line numberDiff line numberDiff line change
@@ -1206,6 +1206,13 @@ def test_getitem_interval_index_partial_indexing(self):
12061206
res = df.loc[:, 0.5]
12071207
tm.assert_series_equal(res, expected)
12081208

1209+
def test_setitem_array_as_cell_value(self):
1210+
# GH#43422
1211+
df = DataFrame(columns=["a", "b"], dtype=object)
1212+
df.loc[0] = {"a": np.zeros((2,)), "b": np.zeros((2, 2))}
1213+
expected = DataFrame({"a": [np.zeros((2,))], "b": [np.zeros((2, 2))]})
1214+
tm.assert_frame_equal(df, expected)
1215+
12091216

12101217
class TestDataFrameIndexingUInt64:
12111218
def test_setitem(self, uint64_frame):

0 commit comments

Comments
 (0)