Skip to content

Commit 3f1a616

Browse files
authored
Regression in loc setitem raising ValueError when setting array as cell value (#43450)
1 parent 7556d29 commit 3f1a616

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
@@ -22,6 +22,7 @@ Fixed regressions
2222
- 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`)
2323
- Fixed regression in :meth:`RangeIndex.where` and :meth:`RangeIndex.putmask` raising ``AssertionError`` when result did not represent a :class:`RangeIndex` (:issue:`43240`)
2424
- Fixed regression in :meth:`read_parquet` where the ``fastparquet`` engine would not work properly with fastparquet 0.7.0 (:issue:`43075`)
25+
- Fixed regression in :meth:`DataFrame.loc.__setitem__` raising ``ValueError`` when setting array as cell value (:issue:`43422`)
2526
- Fixed regression in :func:`is_list_like` where objects with ``__iter__`` set to ``None`` would be identified as iterable (:issue:`43373`)
2627
- Fixed regression in :meth:`.Resampler.aggregate` when used after column selection would raise if ``func`` is a list of aggregation functions (:issue:`42905`)
2728
- 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
@@ -2185,6 +2185,11 @@ def can_hold_element(arr: ArrayLike, element: Any) -> bool:
21852185
# ExtensionBlock._can_hold_element
21862186
return True
21872187

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

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

2235-
# error: Non-overlapping equality check (left operand type: "dtype[Any]", right
2236-
# operand type: "Type[object]")
2237-
elif dtype == object: # type: ignore[comparison-overlap]
2238-
return True
2239-
22402240
elif dtype.kind == "S":
22412241
# TODO: test tests.frame.methods.test_replace tests get here,
22422242
# 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)