From 7c78de2070b7774ac363655b439c0cf43d247a73 Mon Sep 17 00:00:00 2001 From: phofl Date: Tue, 7 Sep 2021 22:41:21 +0200 Subject: [PATCH] Regression in loc setitem raising ValueError when setting array as cell value --- doc/source/whatsnew/v1.3.3.rst | 1 + pandas/core/dtypes/cast.py | 10 +++++----- pandas/tests/frame/indexing/test_indexing.py | 7 +++++++ 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/doc/source/whatsnew/v1.3.3.rst b/doc/source/whatsnew/v1.3.3.rst index 6ba623b9fdbe5..5ffc1a20b382f 100644 --- a/doc/source/whatsnew/v1.3.3.rst +++ b/doc/source/whatsnew/v1.3.3.rst @@ -22,6 +22,7 @@ Fixed regressions - 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`) - Fixed regression in :meth:`RangeIndex.where` and :meth:`RangeIndex.putmask` raising ``AssertionError`` when result did not represent a :class:`RangeIndex` (:issue:`43240`) - Fixed regression in :meth:`read_parquet` where the ``fastparquet`` engine would not work properly with fastparquet 0.7.0 (:issue:`43075`) +- Fixed regression in :meth:`DataFrame.loc.__setitem__` raising ``ValueError`` when setting array as cell value (:issue:`43422`) - Fixed regression in :func:`is_list_like` where objects with ``__iter__`` set to ``None`` would be identified as iterable (:issue:`43373`) - Fixed regression in :meth:`.Resampler.aggregate` when used after column selection would raise if ``func`` is a list of aggregation functions (:issue:`42905`) - Fixed regression in :meth:`DataFrame.corr` where Kendall correlation would produce incorrect results for columns with repeated values (:issue:`43401`) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 6f10a490c7147..d6a8790afd998 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -2185,6 +2185,11 @@ def can_hold_element(arr: ArrayLike, element: Any) -> bool: # ExtensionBlock._can_hold_element return True + # error: Non-overlapping equality check (left operand type: "dtype[Any]", right + # operand type: "Type[object]") + if dtype == object: # type: ignore[comparison-overlap] + return True + tipo = maybe_infer_dtype_type(element) if dtype.kind in ["i", "u"]: @@ -2232,11 +2237,6 @@ def can_hold_element(arr: ArrayLike, element: Any) -> bool: return tipo.kind == "b" return lib.is_bool(element) - # error: Non-overlapping equality check (left operand type: "dtype[Any]", right - # operand type: "Type[object]") - elif dtype == object: # type: ignore[comparison-overlap] - return True - elif dtype.kind == "S": # TODO: test tests.frame.methods.test_replace tests get here, # need more targeted tests. xref phofl has a PR about this diff --git a/pandas/tests/frame/indexing/test_indexing.py b/pandas/tests/frame/indexing/test_indexing.py index 077301613eb8b..07111af598599 100644 --- a/pandas/tests/frame/indexing/test_indexing.py +++ b/pandas/tests/frame/indexing/test_indexing.py @@ -1206,6 +1206,13 @@ def test_getitem_interval_index_partial_indexing(self): res = df.loc[:, 0.5] tm.assert_series_equal(res, expected) + def test_setitem_array_as_cell_value(self): + # GH#43422 + df = DataFrame(columns=["a", "b"], dtype=object) + df.loc[0] = {"a": np.zeros((2,)), "b": np.zeros((2, 2))} + expected = DataFrame({"a": [np.zeros((2,))], "b": [np.zeros((2, 2))]}) + tm.assert_frame_equal(df, expected) + class TestDataFrameIndexingUInt64: def test_setitem(self, uint64_frame):