Skip to content

Commit 742d81f

Browse files
BUG: Fix error in replace with strings that are large numbers (pandas-dev#25616)
1 parent 16edaaf commit 742d81f

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

doc/source/whatsnew/v0.24.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Fixed Regressions
3232
- Fixed regression in creating a period-dtype array from a read-only NumPy array of period objects. (:issue:`25403`)
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 pip installing from source into an environment without NumPy (:issue:`25193`)
35+
- Fixed regression in :func:`replace` where large strings of numbers would be coerced into int, causing an ``OverflowError`` (:issue:`25616`)
3536

3637
.. _whatsnew_0242.enhancements:
3738

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
@@ -181,6 +181,20 @@ def check_replace(to_rep, val, expected):
181181
tr, v = [3, 4], [3.5, True]
182182
check_replace(tr, v, e)
183183

184+
# GH 25616
185+
# casts to object without Exception due to OverflowError
186+
e = pd.Series([0, 1, 2, '100000000000000000000', 4])
187+
tr, v = [3], ['100000000000000000000']
188+
check_replace(tr, v, e)
189+
190+
# GH 25616
191+
# casts to object without Exception due to OverflowError
192+
original = pd.Series([0, '100000000000000000000',
193+
'100000000000000000001'])
194+
result = original.replace(['100000000000000000000'], [1])
195+
expected = pd.Series([0, 1, '100000000000000000001'])
196+
tm.assert_series_equal(result, expected)
197+
184198
# test an object with dates + floats + integers + strings
185199
dr = pd.date_range('1/1/2001', '1/10/2001',
186200
freq='D').to_series().reset_index(drop=True)

0 commit comments

Comments
 (0)