Skip to content

Commit 084569f

Browse files
committed
updated for frames to operate pointwise
1 parent 2c3d0e2 commit 084569f

File tree

5 files changed

+54
-16
lines changed

5 files changed

+54
-16
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ Other enhancements
210210
- :meth:`Series.map` can now accept kwargs to pass on to func (:issue:`59814`)
211211
- :meth:`Series.map` now accepts an ``engine`` parameter to allow execution with a third-party execution engine (:issue:`61125`)
212212
- :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`)
213-
- :meth:`Series.round` now operates pointwise on columns of object dtype (:issue:`62174`)
213+
- :meth:`Series.round` and :meth:`DataFrame.round` now operate pointwise on columns of object dtype (:issue:`62174`)
214214
- :meth:`Series.str.get_dummies` now accepts a ``dtype`` parameter to specify the dtype of the resulting DataFrame (:issue:`47872`)
215215
- :meth:`pandas.concat` will raise a ``ValueError`` when ``ignore_index=True`` and ``keys`` is not ``None`` (:issue:`59274`)
216216
- :py:class:`frozenset` elements in pandas objects are now natively printed (:issue:`60690`)

pandas/core/frame.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,8 @@
9999
is_dataclass,
100100
is_dict_like,
101101
is_float,
102-
is_float_dtype,
103102
is_hashable,
104103
is_integer,
105-
is_integer_dtype,
106104
is_iterator,
107105
is_list_like,
108106
is_scalar,
@@ -11308,15 +11306,10 @@ def round(
1130811306
def _dict_round(df: DataFrame, decimals) -> Iterator[Series]:
1130911307
for col, vals in df.items():
1131011308
try:
11311-
yield _series_round(vals, decimals[col])
11309+
yield vals.round(decimals[col])
1131211310
except KeyError:
1131311311
yield vals
1131411312

11315-
def _series_round(ser: Series, decimals: int) -> Series:
11316-
if is_integer_dtype(ser.dtype) or is_float_dtype(ser.dtype):
11317-
return ser.round(decimals)
11318-
return ser
11319-
1132011313
nv.validate_round(args, kwargs)
1132111314

1132211315
if isinstance(decimals, (dict, Series)):

pandas/core/internals/blocks.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ def make_block_same_class(
262262

263263
@final
264264
def __repr__(self) -> str:
265-
# don't want to print out all of the items here
265+
# don't want to out all of the items here
266266
name = type(self).__name__
267267
if self.ndim == 1:
268268
result = f"{name}: {len(self)} dtype: {self.dtype}"
@@ -346,7 +346,6 @@ def apply(self, func, **kwargs) -> list[Block]:
346346
one
347347
"""
348348
result = func(self.values, **kwargs)
349-
350349
result = maybe_coerce_values(result)
351350
return self._split_op_result(result)
352351

@@ -1515,8 +1514,12 @@ def round(self, decimals: int) -> Self:
15151514
"""
15161515
if self.dtype == _dtype_obj:
15171516
round_func = functools.partial(round, ndigits=decimals)
1517+
mapper = functools.partial(algos.map_array, mapper=round_func)
15181518
try:
1519-
values = algos.map_array(self.values, round_func)
1519+
if self.values.ndim == 1:
1520+
values = algos.map_array(self.values, round_func)
1521+
else:
1522+
values = np.apply_along_axis(mapper, 0, self.values)
15201523
except TypeError as err:
15211524
raise TypeError("Expected numeric entries for dtype object.") from err
15221525

pandas/tests/frame/methods/test_round.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,41 @@ def test_round_empty_not_input(self):
223223
result = df.round()
224224
tm.assert_frame_equal(df, result)
225225
assert df is not result
226+
227+
def test_round_object_columns(self):
228+
# GH#62174
229+
df = DataFrame(
230+
{
231+
"a": Series([1.1111, 2.2222, 3.3333], dtype="object"),
232+
"b": Series([4.4444, 5.5555, 6.6666]),
233+
"c": Series([7.7777, 8.8888, 9.9999], dtype="object"),
234+
}
235+
)
236+
result = df.round(2)
237+
expected = DataFrame(
238+
{
239+
"a": Series([1.11, 2.22, 3.33]),
240+
"b": Series([4.44, 5.56, 6.67]),
241+
"c": Series([7.78, 8.89, 10.0]),
242+
}
243+
)
244+
tm.assert_frame_equal(result, expected)
245+
246+
def test_round_object_columns_with_dict(self):
247+
# GH#62174
248+
df = DataFrame(
249+
{
250+
"a": Series([1.1111, 2.2222, 3.3333], dtype="object"),
251+
"b": Series([4.4444, 5.5555, 6.6666]),
252+
"c": Series([7.7777, 8.8888, 9.9999], dtype="object"),
253+
}
254+
)
255+
result = df.round({"a": 1, "b": 2, "c": 3})
256+
expected = DataFrame(
257+
{
258+
"a": Series([1.1, 2.2, 3.3]),
259+
"b": Series([4.44, 5.56, 6.67]),
260+
"c": Series([7.778, 8.889, 10.0]),
261+
}
262+
)
263+
tm.assert_frame_equal(result, expected)

pandas/tests/series/methods/test_round.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,16 @@ def test_round_ea_boolean(self):
7373
result.iloc[0] = False
7474
tm.assert_series_equal(ser, expected)
7575

76-
def test_round_dtype_object(self):
76+
def test_round_numeric_dtype_object(self):
7777
# GH#61206, GH#62174
7878
ser = Series([0.232], dtype="object")
7979
expected = Series([0.2])
80-
tm.assert_series_equal(ser.round(1), expected)
81-
ser2 = Series(["bar"], dtype="object")
80+
result = ser.round(1)
81+
tm.assert_series_equal(result, expected)
82+
83+
def test_round_non_numeric_dtype_object(self):
84+
# GH#62174
85+
ser = Series(["bar"], dtype="object")
8286
msg = "Expected numeric entries for dtype object."
8387
with pytest.raises(TypeError, match=msg):
84-
ser2.round()
88+
ser.round()

0 commit comments

Comments
 (0)