Skip to content
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 @@ -73,6 +73,7 @@ Other enhancements
- :meth:`.DataFrameGroupBy.transform`, :meth:`.SeriesGroupBy.transform`, :meth:`.DataFrameGroupBy.agg`, :meth:`.SeriesGroupBy.agg`, :meth:`.SeriesGroupBy.apply`, :meth:`.DataFrameGroupBy.apply` now support ``kurt`` (:issue:`40139`)
- :meth:`DataFrame.apply` supports using third-party execution engines like the Bodo.ai JIT compiler (:issue:`60668`)
- :meth:`DataFrame.iloc` and :meth:`Series.iloc` now support boolean masks in ``__getitem__`` for more consistent indexing behavior (:issue:`60994`)
- :meth:`DataFrame.round` now raises a ``Type Error`` if any columns are non-numeric (:issue:`61679`)
- :meth:`DataFrameGroupBy.transform`, :meth:`SeriesGroupBy.transform`, :meth:`DataFrameGroupBy.agg`, :meth:`SeriesGroupBy.agg`, :meth:`RollingGroupby.apply`, :meth:`ExpandingGroupby.apply`, :meth:`Rolling.apply`, :meth:`Expanding.apply`, :meth:`DataFrame.apply` with ``engine="numba"`` now supports positional arguments passed as kwargs (:issue:`58995`)
- :meth:`Rolling.agg`, :meth:`Expanding.agg` and :meth:`ExponentialMovingWindow.agg` now accept :class:`NamedAgg` aggregations through ``**kwargs`` (:issue:`28333`)
- :meth:`Series.map` can now accept kwargs to pass on to func (:issue:`59814`)
Expand Down
7 changes: 5 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -11152,7 +11152,7 @@ def round(
Returns
-------
DataFrame
A DataFrame with the affected columns rounded to the specified
A DataFrame with columns rounded to the specified
number of decimal places.

See Also
Expand Down Expand Up @@ -11227,7 +11227,10 @@ def _series_round(ser: Series, decimals: int) -> Series:
return ser

nv.validate_round(args, kwargs)

if "object" in self.dtypes.values:
Copy link
Member

Choose a reason for hiding this comment

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

the thing to do is operate pointwise on object columns

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just to confirm, you are saying if there is an object column present in the data frame, we should first try to round just that column and see if an error is raised? Because an object columns intended behavior here would be to raise on non-numeric entries and not raise numeric entries?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I guess I'm a little confused what operate pointwise means in this context because I think the current behavior of Series.round is to raise if the dtype is object regardless if the entries are numeric. And because the goal of the PR is to make Series.round and frame DataFrame.round consistent then shouldn't we also raise if any of the columns are of dtype object? Sorry if I'm super complicating this, just trying to understand.

Copy link
Member

Choose a reason for hiding this comment

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

And because the goal of the PR is to make Series.round and frame DataFrame.round consistent then shouldn't we also raise if any of the columns are of dtype object?

I'm saying the series version should behave like series.map(round)

Copy link
Contributor Author

@sharkipelago sharkipelago Aug 17, 2025

Choose a reason for hiding this comment

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

Oh okay, so it should behave like series.map(round) for object columns but every other dtype should continue to do a non-pointwise operation.

So like for

ser = pd.Series([1.22, 3.33, np.nan], dtype="float64")

round(ser) should continue to not raise an error even though round(ser[2]) does raise?

raise TypeError(
"All columns must be numeric dtype, but got object dtype column"
)
if isinstance(decimals, (dict, Series)):
if isinstance(decimals, Series) and not decimals.index.is_unique:
raise ValueError("Index of decimals must be unique")
Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/copy_view/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -911,20 +911,20 @@ def test_sort_values_inplace(obj, kwargs):

@pytest.mark.parametrize("decimals", [-1, 0, 1])
def test_round(decimals):
df = DataFrame({"a": [1, 2], "b": "c"})
df = DataFrame({"a": [1, 2], "b": [3.3, 4.4]})
df_orig = df.copy()
df2 = df.round(decimals=decimals)

assert tm.shares_memory(get_array(df2, "b"), get_array(df, "b"))
assert not tm.shares_memory(get_array(df2, "b"), get_array(df, "b"))
# TODO: Make inplace by using out parameter of ndarray.round?
if decimals >= 0:
# Ensure lazy copy if no-op
assert np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
else:
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))

df2.iloc[0, 1] = "d"
df2.iloc[0, 0] = 4
df2.iloc[0, 1] = 6.6
df2.iloc[0, 0] = 5
assert not np.shares_memory(get_array(df2, "b"), get_array(df, "b"))
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
tm.assert_frame_equal(df, df_orig)
Expand Down
31 changes: 7 additions & 24 deletions pandas/tests/frame/methods/test_round.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from pandas import (
DataFrame,
Series,
date_range,
)
import pandas._testing as tm

Expand Down Expand Up @@ -143,29 +142,6 @@ def test_round_numpy_with_nan(self):
expected = Series([2.0, np.nan, 0.0]).to_frame()
tm.assert_frame_equal(result, expected)

def test_round_mixed_type(self):
# GH#11885
df = DataFrame(
{
"col1": [1.1, 2.2, 3.3, 4.4],
"col2": ["1", "a", "c", "f"],
"col3": date_range("20111111", periods=4),
}
)
round_0 = DataFrame(
{
"col1": [1.0, 2.0, 3.0, 4.0],
"col2": ["1", "a", "c", "f"],
"col3": date_range("20111111", periods=4),
}
)
tm.assert_frame_equal(df.round(), round_0)
tm.assert_frame_equal(df.round(1), df)
tm.assert_frame_equal(df.round({"col1": 1}), df)
tm.assert_frame_equal(df.round({"col1": 0}), round_0)
tm.assert_frame_equal(df.round({"col1": 0, "col2": 1}), round_0)
tm.assert_frame_equal(df.round({"col3": 1}), df)

def test_round_with_duplicate_columns(self):
# GH#11611

Expand Down Expand Up @@ -223,3 +199,10 @@ def test_round_empty_not_input(self):
result = df.round()
tm.assert_frame_equal(df, result)
assert df is not result

def test_round_non_numeric_columns(self):
# GH#61679
df = DataFrame({"col1": [1.22, 10.32, 3.54], "col2": ["a", "b", "c"]})
msg = "All columns must be numeric dtype, but got object dtype column"
with pytest.raises(TypeError, match=msg):
df.round()
Loading