Skip to content

Commit 0930517

Browse files
committed
added TypeError if object dtypes are dtected in dataframe
1 parent 592a41a commit 0930517

File tree

3 files changed

+18
-26
lines changed

3 files changed

+18
-26
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ Other enhancements
7373
- :meth:`.DataFrameGroupBy.transform`, :meth:`.SeriesGroupBy.transform`, :meth:`.DataFrameGroupBy.agg`, :meth:`.SeriesGroupBy.agg`, :meth:`.SeriesGroupBy.apply`, :meth:`.DataFrameGroupBy.apply` now support ``kurt`` (:issue:`40139`)
7474
- :meth:`DataFrame.apply` supports using third-party execution engines like the Bodo.ai JIT compiler (:issue:`60668`)
7575
- :meth:`DataFrame.iloc` and :meth:`Series.iloc` now support boolean masks in ``__getitem__`` for more consistent indexing behavior (:issue:`60994`)
76+
- :meth:`DataFrame.round` now raises a ``Type Error`` if any columns are non-numeric (:issue:`61679`)
7677
- :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`)
7778
- :meth:`Rolling.agg`, :meth:`Expanding.agg` and :meth:`ExponentialMovingWindow.agg` now accept :class:`NamedAgg` aggregations through ``**kwargs`` (:issue:`28333`)
7879
- :meth:`Series.map` can now accept kwargs to pass on to func (:issue:`59814`)

pandas/core/frame.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11152,7 +11152,7 @@ def round(
1115211152
Returns
1115311153
-------
1115411154
DataFrame
11155-
A DataFrame with the affected columns rounded to the specified
11155+
A DataFrame with columns rounded to the specified
1115611156
number of decimal places.
1115711157
1115811158
See Also
@@ -11227,7 +11227,10 @@ def _series_round(ser: Series, decimals: int) -> Series:
1122711227
return ser
1122811228

1122911229
nv.validate_round(args, kwargs)
11230-
11230+
if "object" in self.dtypes.values:
11231+
raise TypeError(
11232+
"All columns must be numeric dtype, but got object dtype column(s)"
11233+
)
1123111234
if isinstance(decimals, (dict, Series)):
1123211235
if isinstance(decimals, Series) and not decimals.index.is_unique:
1123311236
raise ValueError("Index of decimals must be unique")

pandas/tests/frame/methods/test_round.py

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from pandas import (
66
DataFrame,
77
Series,
8-
date_range,
98
)
109
import pandas._testing as tm
1110

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

146-
def test_round_mixed_type(self):
147-
# GH#11885
148-
df = DataFrame(
149-
{
150-
"col1": [1.1, 2.2, 3.3, 4.4],
151-
"col2": ["1", "a", "c", "f"],
152-
"col3": date_range("20111111", periods=4),
153-
}
154-
)
155-
round_0 = DataFrame(
156-
{
157-
"col1": [1.0, 2.0, 3.0, 4.0],
158-
"col2": ["1", "a", "c", "f"],
159-
"col3": date_range("20111111", periods=4),
160-
}
161-
)
162-
tm.assert_frame_equal(df.round(), round_0)
163-
tm.assert_frame_equal(df.round(1), df)
164-
tm.assert_frame_equal(df.round({"col1": 1}), df)
165-
tm.assert_frame_equal(df.round({"col1": 0}), round_0)
166-
tm.assert_frame_equal(df.round({"col1": 0, "col2": 1}), round_0)
167-
tm.assert_frame_equal(df.round({"col3": 1}), df)
168-
169145
def test_round_with_duplicate_columns(self):
170146
# GH#11611
171147

@@ -223,3 +199,15 @@ def test_round_empty_not_input(self):
223199
result = df.round()
224200
tm.assert_frame_equal(df, result)
225201
assert df is not result
202+
203+
def test_round_non_numeric_columns(self):
204+
# GH#61679
205+
df = DataFrame(
206+
{
207+
"a": [1.2234242333234, 323432.3243423, np.nan],
208+
"b": ["a", "b", "c"],
209+
}
210+
)
211+
msg = "All columns must be numeric dtype, but got object dtype column\\(s\\)"
212+
with pytest.raises(TypeError, match=msg):
213+
df.round()

0 commit comments

Comments
 (0)