Skip to content

Commit 71c2301

Browse files
phoflpmhatre1
authored andcommitted
REGR: astype introducing decimals when casting from int with na to string (pandas-dev#57489)
1 parent 41da905 commit 71c2301

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
@@ -667,3 +667,11 @@ def test_astype_timedelta64_with_np_nan(self):
667667
result = Series([Timedelta(1), np.nan], dtype="timedelta64[ns]")
668668
expected = Series([Timedelta(1), NaT], dtype="timedelta64[ns]")
669669
tm.assert_series_equal(result, expected)
670+
671+
@td.skip_if_no("pyarrow")
672+
def test_astype_int_na_string(self):
673+
# GH#57418
674+
ser = Series([12, NA], dtype="Int64[pyarrow]")
675+
result = ser.astype("string[pyarrow]")
676+
expected = Series(["12", NA], dtype="string[pyarrow]")
677+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)