Skip to content

Commit 12fd316

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

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
@@ -1079,7 +1079,7 @@ def coerce_to_target_dtype(self, other):
10791079

10801080
try:
10811081
return self.astype(dtype)
1082-
except (ValueError, TypeError):
1082+
except (ValueError, TypeError, OverflowError):
10831083
pass
10841084

10851085
return self.astype(object)
@@ -3210,7 +3210,7 @@ def _putmask_smart(v, m, n):
32103210
nv = v.copy()
32113211
nv[m] = nn_at
32123212
return nv
3213-
except (ValueError, IndexError, TypeError):
3213+
except (ValueError, IndexError, TypeError, OverflowError):
32143214
pass
32153215

32163216
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)