Skip to content

Commit 83acc9e

Browse files
committed
moved logic to block-level
1 parent 0ab7faa commit 83acc9e

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

pandas/core/internals/blocks.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import functools
34
import inspect
45
import re
56
from typing import (
@@ -1503,16 +1504,28 @@ def quantile(
15031504
def round(self, decimals: int) -> Self:
15041505
"""
15051506
Rounds the values.
1506-
If the block is not of an integer or float dtype, nothing happens.
1507-
This is consistent with DataFrame.round behavior.
1508-
(Note: Series.round would raise)
1507+
If the block is of object dtype, it will operate pointwise and possibly raise.
1508+
Otherwise, if the block is not of an integer or float dtype, nothing happens.
15091509
15101510
Parameters
15111511
----------
15121512
decimals: int,
15131513
Number of decimal places to round to.
15141514
Caller is responsible for validating this
15151515
"""
1516+
if self.dtype == _dtype_obj:
1517+
round_func = functools.partial(round, ndigits=decimals)
1518+
try:
1519+
values = algos.map_array(self.values, round_func)
1520+
except TypeError as err:
1521+
raise TypeError("Expected numeric entries for dtype object.") from err
1522+
1523+
refs = None
1524+
if values is self.values:
1525+
refs = self.refs
1526+
1527+
return self.make_block_same_class(values, refs=refs)
1528+
15161529
if not self.is_numeric or self.is_bool:
15171530
return self.copy(deep=False)
15181531
# TODO: round only defined on BaseMaskedArray

pandas/core/series.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2514,15 +2514,6 @@ def round(self, decimals: int = 0, *args, **kwargs) -> Series:
25142514
dtype: float64
25152515
"""
25162516
nv.validate_round(args, kwargs)
2517-
if self.dtype == "object":
2518-
try:
2519-
round_func = functools.partial(round, ndigits=decimals)
2520-
new_values = self._map_values(round_func)
2521-
return self._constructor(
2522-
new_values, index=self.index, copy=False
2523-
).__finalize__(self, method="map")
2524-
except TypeError as e:
2525-
raise TypeError("Expected numeric entries for dtype object.") from e
25262517
new_mgr = self._mgr.round(decimals=decimals)
25272518
return self._constructor_from_mgr(new_mgr, axes=new_mgr.axes).__finalize__(
25282519
self, method="round"

0 commit comments

Comments
 (0)