Skip to content

BUG: indexing raises for ea into numpy series #55633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -344,9 +344,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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to also include ABCIndex for df.loc[: ,"a"] = Index[Int64]?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no that's unpacked earlier

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