Skip to content

Commit 03a4981

Browse files
authored
BUG: indexing raises for ea into numpy series (#55633)
1 parent 2b6af64 commit 03a4981

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

doc/source/whatsnew/v2.2.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,9 @@ Interval
345345

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

352352
Missing
353353
^^^^^^^

pandas/core/dtypes/cast.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1816,7 +1816,8 @@ def np_can_hold_element(dtype: np.dtype, element: Any) -> Any:
18161816
if not isinstance(tipo, np.dtype):
18171817
# i.e. nullable IntegerDtype; we can put this into an ndarray
18181818
# losslessly iff it has no NAs
1819-
if element._hasna:
1819+
arr = element._values if isinstance(element, ABCSeries) else element
1820+
if arr._hasna:
18201821
raise LossySetitemError
18211822
return element
18221823

pandas/tests/frame/indexing/test_setitem.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,17 @@ def test_setitem_frame_midx_columns(self):
761761
df[col_name] = df[[col_name]]
762762
tm.assert_frame_equal(df, expected)
763763

764+
def test_loc_setitem_ea_dtype(self):
765+
# GH#55604
766+
df = DataFrame({"a": np.array([10], dtype="i8")})
767+
df.loc[:, "a"] = Series([11], dtype="Int64")
768+
expected = DataFrame({"a": np.array([11], dtype="i8")})
769+
tm.assert_frame_equal(df, expected)
770+
771+
df = DataFrame({"a": np.array([10], dtype="i8")})
772+
df.iloc[:, 0] = Series([11], dtype="Int64")
773+
tm.assert_frame_equal(df, expected)
774+
764775

765776
class TestSetitemTZAwareValues:
766777
@pytest.fixture

0 commit comments

Comments
 (0)