Skip to content

Commit ff72521

Browse files
phoflmeeseeksmachine
authored andcommitted
Backport PR pandas-dev#57489: REGR: astype introducing decimals when casting from int with na to string
1 parent 2ae7a10 commit ff72521

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

doc/source/whatsnew/v2.2.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Fixed regressions
3939
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` where values containing the minimum or maximum value for the dtype could produce incorrect results (:issue:`57040`)
4040
- Fixed regression in :meth:`ExtensionArray.to_numpy` raising for non-numeric masked dtypes (:issue:`56991`)
4141
- Fixed regression in :meth:`Index.join` raising ``TypeError`` when joining an empty index to a non-empty index containing mixed dtype values (:issue:`57048`)
42+
- Fixed regression in :meth:`Series.astype` introducing decimals when converting from integer with missing values to string dtype (:issue:`57418`)
4243
- Fixed regression in :meth:`Series.pct_change` raising a ``ValueError`` for an empty :class:`Series` (:issue:`57056`)
4344
- Fixed regression in :meth:`Series.to_numpy` when dtype is given as float and the data contains NaNs (:issue:`57121`)
4445

pandas/_libs/lib.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ cpdef ndarray[object] ensure_string_array(
759759
out = arr.astype(str).astype(object)
760760
out[arr.isna()] = na_value
761761
return out
762-
arr = arr.to_numpy()
762+
arr = arr.to_numpy(dtype=object)
763763
elif not util.is_array(arr):
764764
arr = np.array(arr, dtype="object")
765765

pandas/tests/series/methods/test_astype.py

+8
Original file line numberDiff line numberDiff line change
@@ -673,3 +673,11 @@ def test_astype_timedelta64_with_np_nan(self):
673673
result = Series([Timedelta(1), np.nan], dtype="timedelta64[ns]")
674674
expected = Series([Timedelta(1), NaT], dtype="timedelta64[ns]")
675675
tm.assert_series_equal(result, expected)
676+
677+
@td.skip_if_no("pyarrow")
678+
def test_astype_int_na_string(self):
679+
# GH#57418
680+
ser = Series([12, NA], dtype="Int64[pyarrow]")
681+
result = ser.astype("string[pyarrow]")
682+
expected = Series(["12", NA], dtype="string[pyarrow]")
683+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)