这是indexloc提供的服务,不要输入任何密码
Skip to content
Closed
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,7 @@ Numeric

Conversion
^^^^^^^^^^
- Bug in :meth:`BaseMaskedArray._cast_pointwise_result` with all-NA values results returned ``object`` dtype instead of preserving the original dtype (:issue:`62344`)
- Bug in :meth:`DataFrame.astype` not casting ``values`` for Arrow-based dictionary dtype correctly (:issue:`58479`)
- Bug in :meth:`DataFrame.update` bool dtype being converted to object (:issue:`55509`)
- Bug in :meth:`Series.astype` might modify read-only array inplace when casting to a string dtype (:issue:`57212`)
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/arrays/masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ def _from_sequence(cls, scalars, *, dtype=None, copy: bool = False) -> Self:
return cls(values, mask)

def _cast_pointwise_result(self, values) -> ArrayLike:
if isna(values).all():
return type(self)._from_sequence(values, dtype=self.dtype)
values = np.asarray(values, dtype=object)
result = lib.maybe_convert_objects(values, convert_to_nullable_dtype=True)
lkind = self.dtype.kind
Expand Down
20 changes: 17 additions & 3 deletions pandas/tests/extension/test_masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,21 @@ def check_accumulate(self, ser: pd.Series, op_name: str, skipna: bool):
tm.assert_series_equal(result, expected)

def test_loc_setitem_with_expansion_preserves_ea_index_dtype(self, data, request):
if data.dtype.kind == "b":
mark = pytest.mark.xfail(reason="GH#62344 incorrectly casts to object")
request.applymarker(mark)
super().test_loc_setitem_with_expansion_preserves_ea_index_dtype(data)

@pytest.mark.parametrize(
"arr, values",
[
(pd.array([True, False]), [pd.NA, pd.NA]),
(pd.array([1, 2]), [pd.NA, pd.NA]),
],
)
def test_cast_pointwise_result_all_na_respects_dtype(self, arr, values):
"""
GH#62344
Ensure that _cast_pointwise_result respects the original dtype
even when the result consists entirely of NA values.
"""
result = arr._cast_pointwise_result(values)
assert result.dtype == arr.dtype
assert all(x is pd.NA for x in result)
Loading