Skip to content

BUG/TST: added TypeError if object dtypes are detected in dataframe #61682

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 @@ -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:
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