Skip to content

Commit 16f57f4

Browse files
authored
BUG: Fix cast_pointwise_result with all-NA values (#62352)
1 parent e79f156 commit 16f57f4

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

pandas/core/arrays/masked.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ def _from_sequence(cls, scalars, *, dtype=None, copy: bool = False) -> Self:
149149
return cls(values, mask)
150150

151151
def _cast_pointwise_result(self, values) -> ArrayLike:
152+
if isna(values).all():
153+
return type(self)._from_sequence(values, dtype=self.dtype)
152154
values = np.asarray(values, dtype=object)
153155
result = lib.maybe_convert_objects(values, convert_to_nullable_dtype=True)
154156
lkind = self.dtype.kind

pandas/tests/extension/test_masked.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,15 @@ def check_accumulate(self, ser: pd.Series, op_name: str, skipna: bool):
362362
tm.assert_series_equal(result, expected)
363363

364364
def test_loc_setitem_with_expansion_preserves_ea_index_dtype(self, data, request):
365-
if data.dtype.kind == "b":
366-
mark = pytest.mark.xfail(reason="GH#62344 incorrectly casts to object")
367-
request.applymarker(mark)
368365
super().test_loc_setitem_with_expansion_preserves_ea_index_dtype(data)
366+
367+
368+
@pytest.mark.parametrize(
369+
"arr", [pd.array([True, False]), pd.array([1, 2]), pd.array([1.0, 2.0])]
370+
)
371+
def test_cast_pointwise_result_all_na_respects_original_dtype(arr):
372+
# GH#62344
373+
values = [pd.NA, pd.NA]
374+
result = arr._cast_pointwise_result(values)
375+
assert result.dtype == arr.dtype
376+
assert all(x is pd.NA for x in result)

0 commit comments

Comments
 (0)