Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b9afcf5

Browse files
committed
BUG: Fix cast_pointwise_result with all-NA values
1 parent bc500f7 commit b9afcf5

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

‎doc/source/whatsnew/v3.0.0.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,7 @@ Numeric
942942

943943
Conversion
944944
^^^^^^^^^^
945+
- Bug in :meth:`BaseMaskedArray._cast_pointwise_result` with all-NA values results returned ``object`` dtype instead of preserving the original dtype (:issue:`62344`)
945946
- Bug in :meth:`DataFrame.astype` not casting ``values`` for Arrow-based dictionary dtype correctly (:issue:`58479`)
946947
- Bug in :meth:`DataFrame.update` bool dtype being converted to object (:issue:`55509`)
947948
- Bug in :meth:`Series.astype` might modify read-only array inplace when casting to a string dtype (:issue:`57212`)

‎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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,3 +366,20 @@ def test_loc_setitem_with_expansion_preserves_ea_index_dtype(self, data, request
366366
mark = pytest.mark.xfail(reason="GH#62344 incorrectly casts to object")
367367
request.applymarker(mark)
368368
super().test_loc_setitem_with_expansion_preserves_ea_index_dtype(data)
369+
370+
@pytest.mark.parametrize(
371+
"arr, values",
372+
[
373+
(pd.array([True, False]), [pd.NA, pd.NA]),
374+
(pd.array([1, 2]), [pd.NA, pd.NA]),
375+
],
376+
)
377+
def test_cast_pointwise_result_all_na_respects_dtype(self, arr, values):
378+
"""
379+
GH#62344
380+
Ensure that _cast_pointwise_result respects the original dtype
381+
even when the result consists entirely of NA values.
382+
"""
383+
result = arr._cast_pointwise_result(values)
384+
assert result.dtype == arr.dtype
385+
assert all(x is pd.NA for x in result)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /