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 1 commit
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Fixed Regressions
- Fixed regression in creating a period-dtype array from a read-only NumPy array of period objects. (:issue:`25403`)
- 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 pip installing from source into an environment without NumPy (:issue:`25193`)
- Fixed regression in :func:`replace` where large strings of numbers would be coerced into int, causing an ``OverflowError`` (:issue:`25616`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use DataFrame.replace as what you have won't render. use double-backticks around int.


.. _whatsnew_0242.enhancements:

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

# GH 25616
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make a new test

# casts to object without Exception due to OverflowError
e = pd.Series([0, 1, 2, '100000000000000000000', 4])
tr, v = [3], ['100000000000000000000']
check_replace(tr, v, e)

# GH 25616
# casts to object without Exception due to OverflowError
original = pd.Series([0, '100000000000000000000',
'100000000000000000001'])
result = original.replace(['100000000000000000000'], [1])
expected = pd.Series([0, 1, '100000000000000000001'])
tm.assert_series_equal(result, expected)

# test an object with dates + floats + integers + strings
dr = pd.date_range('1/1/2001', '1/10/2001',
freq='D').to_series().reset_index(drop=True)
Expand Down