Skip to content

BUG: Bug fix for implicit upcast to float64 for large series (more than 1000000 rows) #62077

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,7 @@ Conversion
- Bug in :meth:`Series.astype` might modify read-only array inplace when casting to a string dtype (:issue:`57212`)
- Bug in :meth:`Series.convert_dtypes` and :meth:`DataFrame.convert_dtypes` removing timezone information for objects with :class:`ArrowDtype` (:issue:`60237`)
- Bug in :meth:`Series.reindex` not maintaining ``float32`` type when a ``reindex`` introduces a missing value (:issue:`45857`)
- Bug in :meth:`_array_ops._maybe_prepare_scalar_for_op` not maintaining ``float32`` type when converting NumPy floating scalars (:issue:`61951`)

Strings
^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/ops/array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ def maybe_prepare_scalar_for_op(obj, shape: Shape):
return int(obj)

elif isinstance(obj, np.floating):
return float(obj)
return np.dtype(obj.dtype).type(obj)

return obj

Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/series/methods/test_convert_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,9 @@ def test_convert_dtype_pyarrow_timezone_preserve(self):
result = ser.convert_dtypes(dtype_backend="pyarrow")
expected = ser.copy()
tm.assert_series_equal(result, expected)

def test_float32_series_addition_preserves_dtype(self):
# GH#61951
ser_a = pd.Series(np.zeros(1000000), dtype="float32") + np.float32(1)
ser_b = pd.Series(np.zeros(1000001), dtype="float32") + np.float32(1)
assert all(dtype == np.float32 for dtype in (ser_a.dtype, ser_b.dtype))
Loading