Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Fix error in replace with strings that are large numbers (#25616) #25644

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.24.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Fixed Regressions
- 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`)
- Fixed regression in :func:`to_timedelta` losing precision when converting floating data to ``Timedelta`` data (:issue:`25077`).
- Fixed pip installing from source into an environment without NumPy (:issue:`25193`)
- Fixed regression in :meth:`DataFrame.replace` where large strings of numbers would be coerced into ``int64``, causing an ``OverflowError`` (:issue:`25616`)
- Fixed regression in :func:`factorize` when passing a custom ``na_sentinel`` value with ``sort=True`` (:issue:`25409`).
- Fixed regression in :meth:`DataFrame.to_csv` writing duplicate line endings with gzip compress (:issue:`25311`)

Expand Down Expand Up @@ -90,6 +91,7 @@ A total of 25 people contributed patches to this release. People with a "+" by t
* Joris Van den Bossche
* Josh
* Justin Zheng
* Kendall Masse
* Matthew Roeschke
* Max Bolingbroke +
* rbenes +
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ def coerce_to_target_dtype(self, other):

try:
return self.astype(dtype)
except (ValueError, TypeError):
except (ValueError, TypeError, OverflowError):
pass

return self.astype(object)
Expand Down Expand Up @@ -3210,7 +3210,7 @@ def _putmask_smart(v, m, n):
nv = v.copy()
nv[m] = nn_at
return nv
except (ValueError, IndexError, TypeError):
except (ValueError, IndexError, TypeError, OverflowError):
jreback marked this conversation as resolved.
Show resolved Hide resolved
pass

n = np.asarray(n)
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/series/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,17 @@ def test_replace_mixed_types_with_string(self):
result = s.replace([2, '4'], np.nan)
expected = pd.Series([1, np.nan, 3, np.nan, 4, 5])
tm.assert_series_equal(expected, result)

def test_replace_with_no_overflowerror(self):
# GH 25616
# casts to object without Exception from OverflowError
s = pd.Series([0, 1, 2, 3, 4])
result = s.replace([3], ['100000000000000000000'])
expected = pd.Series([0, 1, 2, '100000000000000000000', 4])
tm.assert_series_equal(result, expected)

s = pd.Series([0, '100000000000000000000',
'100000000000000000001'])
result = s.replace(['100000000000000000000'], [1])
expected = pd.Series([0, 1, '100000000000000000001'])
tm.assert_series_equal(result, expected)