Skip to content

Commit

Permalink
BUG: indexing raises for ea into numpy series (#55633)
Browse files Browse the repository at this point in the history
  • Loading branch information
phofl authored Oct 23, 2023
1 parent 2b6af64 commit 03a4981
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,9 @@ Interval

Indexing
^^^^^^^^
- Bug in :meth:`DataFrame.loc` when setting :class:`Series` with extension dtype into NumPy dtype (:issue:`55604`)
- Bug in :meth:`Index.difference` not returning a unique set of values when ``other`` is empty or ``other`` is considered non-comparable (:issue:`55113`)
- Bug in setting :class:`Categorical` values into a :class:`DataFrame` with numpy dtypes raising ``RecursionError`` (:issue:`52927`)
-

Missing
^^^^^^^
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1816,7 +1816,8 @@ def np_can_hold_element(dtype: np.dtype, element: Any) -> Any:
if not isinstance(tipo, np.dtype):
# i.e. nullable IntegerDtype; we can put this into an ndarray
# losslessly iff it has no NAs
if element._hasna:
arr = element._values if isinstance(element, ABCSeries) else element
if arr._hasna:
raise LossySetitemError
return element

Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/frame/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,17 @@ def test_setitem_frame_midx_columns(self):
df[col_name] = df[[col_name]]
tm.assert_frame_equal(df, expected)

def test_loc_setitem_ea_dtype(self):
# GH#55604
df = DataFrame({"a": np.array([10], dtype="i8")})
df.loc[:, "a"] = Series([11], dtype="Int64")
expected = DataFrame({"a": np.array([11], dtype="i8")})
tm.assert_frame_equal(df, expected)

df = DataFrame({"a": np.array([10], dtype="i8")})
df.iloc[:, 0] = Series([11], dtype="Int64")
tm.assert_frame_equal(df, expected)


class TestSetitemTZAwareValues:
@pytest.fixture
Expand Down

0 comments on commit 03a4981

Please sign in to comment.