Skip to content

Commit

Permalink
Backport PR #43450: Regression in loc setitem raising ValueError when…
Browse files Browse the repository at this point in the history
… setting array as cell value (#43453)

Co-authored-by: Patrick Hoefler <61934744+phofl@users.noreply.github.com>
  • Loading branch information
meeseeksmachine and phofl authored Sep 7, 2021
1 parent 93f2de9 commit d6f9a53
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,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`)
Expand Down
10 changes: 5 additions & 5 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -2184,6 +2184,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"]:
Expand Down Expand Up @@ -2231,11 +2236,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
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/frame/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit d6f9a53

Please sign in to comment.