Skip to content

Commit 4c5e204

Browse files
committed
Fix Series.round TypeError on object dtype with pd.NA (GH#61712) + tests + whatsnew
1 parent ffd643a commit 4c5e204

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Other enhancements
8282
- :meth:`Series.map` can now accept kwargs to pass on to func (:issue:`59814`)
8383
- :meth:`Series.map` now accepts an ``engine`` parameter to allow execution with a third-party execution engine (:issue:`61125`)
8484
- :meth:`Series.rank` and :meth:`DataFrame.rank` with numpy-nullable dtypes preserve ``NA`` values and return ``UInt64`` dtype where appropriate instead of casting ``NA`` to ``NaN`` with ``float64`` dtype (:issue:`62043`)
85+
- :meth:`Series.round` raising ``TypeError`` for ``object`` dtype with ``pd.NA``. The result now coerces to nullable ``Float64`` and rounds as expected (:issue:`61712`).
8586
- :meth:`Series.str.get_dummies` now accepts a ``dtype`` parameter to specify the dtype of the resulting DataFrame (:issue:`47872`)
8687
- :meth:`pandas.concat` will raise a ``ValueError`` when ``ignore_index=True`` and ``keys`` is not ``None`` (:issue:`59274`)
8788
- :py:class:`frozenset` elements in pandas objects are now natively printed (:issue:`60690`)
@@ -97,7 +98,7 @@ Other enhancements
9798
- Support passing a :class:`Iterable[Hashable]` input to :meth:`DataFrame.drop_duplicates` (:issue:`59237`)
9899
- Support reading Stata 102-format (Stata 1) dta files (:issue:`58978`)
99100
- Support reading Stata 110-format (Stata 7) dta files (:issue:`47176`)
100-
-
101+
101102

102103
.. ---------------------------------------------------------------------------
103104
.. _whatsnew_300.notable_bug_fixes:

pandas/core/series.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2516,12 +2516,13 @@ def round(self, decimals: int = 0, *args, **kwargs) -> Series:
25162516
"""
25172517
nv.validate_round(args, kwargs)
25182518
if self.dtype == "object":
2519-
# Try to safely cast to numeric type (like float) if values are pd.NA or numbers
2519+
# Try to safely cast to a numeric nullable dtype when
2520+
# values are numbers or pd.NA.
25202521
try:
25212522
converted = self.astype("Float64")
25222523
return converted.round(decimals)
2523-
except Exception:
2524-
raise TypeError("Expected numeric dtype, got object instead.")
2524+
except Exception as err:
2525+
raise TypeError("Expected numeric dtype, got object instead.") from err
25252526
new_mgr = self._mgr.round(decimals=decimals)
25262527
return self._constructor_from_mgr(new_mgr, axes=new_mgr.axes).__finalize__(
25272528
self, method="round"

pandas/tests/series/methods/test_round.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,8 @@ def test_round_dtype_object(self):
8181
ser.round()
8282

8383
def test_round_with_pd_na(self):
84-
import pandas as pd
85-
import numpy as np
86-
s = pd.Series([0.5, pd.NA], dtype="object")
84+
# GH61712
85+
s = Series([0.5, pd.NA], dtype="object")
8786
result = s.round(0)
88-
expected = pd.Series([0.0, pd.NA], dtype="Float64")
89-
pd.testing.assert_series_equal(result, expected)
87+
expected = Series([0.0, pd.NA], dtype="Float64")
88+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)