Skip to content

Commit f4e1127

Browse files
BUG: Fix error in replace with strings that are large numbers (#25616) (#25644)
(cherry picked from commit 12fd316)
1 parent d589e58 commit f4e1127

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

doc/source/whatsnew/v0.24.2.rst

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Fixed Regressions
3333
- Fixed regression in :class:`Categorical`, where constructing it from a categorical ``Series`` and an explicit ``categories=`` that differed from that in the ``Series`` created an invalid object which could trigger segfaults. (:issue:`25318`)
3434
- Fixed regression in :func:`to_timedelta` losing precision when converting floating data to ``Timedelta`` data (:issue:`25077`).
3535
- Fixed pip installing from source into an environment without NumPy (:issue:`25193`)
36+
- Fixed regression in :meth:`DataFrame.replace` where large strings of numbers would be coerced into ``int64``, causing an ``OverflowError`` (:issue:`25616`)
3637
- Fixed regression in :func:`factorize` when passing a custom ``na_sentinel`` value with ``sort=True`` (:issue:`25409`).
3738
- Fixed regression in :meth:`DataFrame.to_csv` writing duplicate line endings with gzip compress (:issue:`25311`)
3839

@@ -90,6 +91,7 @@ A total of 25 people contributed patches to this release. People with a "+" by t
9091
* Joris Van den Bossche
9192
* Josh
9293
* Justin Zheng
94+
* Kendall Masse
9395
* Matthew Roeschke
9496
* Max Bolingbroke +
9597
* rbenes +

pandas/core/internals/blocks.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ def coerce_to_target_dtype(self, other):
10921092

10931093
try:
10941094
return self.astype(dtype)
1095-
except (ValueError, TypeError):
1095+
except (ValueError, TypeError, OverflowError):
10961096
pass
10971097

10981098
return self.astype(object)
@@ -3272,7 +3272,7 @@ def _putmask_smart(v, m, n):
32723272
nv = v.copy()
32733273
nv[m] = nn_at
32743274
return nv
3275-
except (ValueError, IndexError, TypeError):
3275+
except (ValueError, IndexError, TypeError, OverflowError):
32763276
pass
32773277

32783278
n = np.asarray(n)

pandas/tests/series/test_replace.py

+14
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,17 @@ def test_replace_mixed_types_with_string(self):
280280
result = s.replace([2, '4'], np.nan)
281281
expected = pd.Series([1, np.nan, 3, np.nan, 4, 5])
282282
tm.assert_series_equal(expected, result)
283+
284+
def test_replace_with_no_overflowerror(self):
285+
# GH 25616
286+
# casts to object without Exception from OverflowError
287+
s = pd.Series([0, 1, 2, 3, 4])
288+
result = s.replace([3], ['100000000000000000000'])
289+
expected = pd.Series([0, 1, 2, '100000000000000000000', 4])
290+
tm.assert_series_equal(result, expected)
291+
292+
s = pd.Series([0, '100000000000000000000',
293+
'100000000000000000001'])
294+
result = s.replace(['100000000000000000000'], [1])
295+
expected = pd.Series([0, 1, '100000000000000000001'])
296+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)