Skip to content

Commit 5802b08

Browse files
committed
Merge pull request #11923 from kawochen/API-11885
API: GH11885 DataFrame.round() now returns non-numeric columns unchanged
2 parents 5eb1284 + 4a5569f commit 5802b08

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

doc/source/whatsnew/v0.18.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ Backwards incompatible API changes
165165
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
166166

167167
- The parameter ``out`` has been removed from the ``Series.round()`` method. (:issue:`11763`)
168+
- ``DataFrame.round()`` leaves non-numeric columns unchanged in its return, rather than raises. (:issue:`11885`)
168169

169170
Bug in QuarterBegin with n=0
170171
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

pandas/core/frame.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -4416,18 +4416,23 @@ def round(self, decimals=0, out=None):
44164416
def _dict_round(df, decimals):
44174417
for col, vals in df.iteritems():
44184418
try:
4419-
yield vals.round(decimals[col])
4419+
yield _series_round(vals, decimals[col])
44204420
except KeyError:
44214421
yield vals
44224422

4423+
def _series_round(s, decimals):
4424+
if com.is_integer_dtype(s) or com.is_float_dtype(s):
4425+
return s.round(decimals)
4426+
return s
4427+
44234428
if isinstance(decimals, (dict, Series)):
44244429
if isinstance(decimals, Series):
44254430
if not decimals.index.is_unique:
44264431
raise ValueError("Index of decimals must be unique")
44274432
new_cols = [col for col in _dict_round(self, decimals)]
44284433
elif com.is_integer(decimals):
44294434
# Dispatch to Series.round
4430-
new_cols = [v.round(decimals) for _, v in self.iteritems()]
4435+
new_cols = [_series_round(v, decimals) for _, v in self.iteritems()]
44314436
else:
44324437
raise TypeError("decimals must be an integer, a dict-like or a Series")
44334438

pandas/tests/test_frame.py

+15
Original file line numberDiff line numberDiff line change
@@ -13523,6 +13523,21 @@ def test_round(self):
1352313523
# Make sure this doesn't break existing Series.round
1352413524
tm.assert_series_equal(df['col1'].round(1), expected_rounded['col1'])
1352513525

13526+
13527+
def test_round_mixed_type(self):
13528+
# GH11885
13529+
df = DataFrame({'col1': [1.1, 2.2, 3.3, 4.4], 'col2': ['1', 'a', 'c', 'f'],
13530+
'col3': date_range('20111111', periods=4)})
13531+
round_0 = DataFrame({'col1': [1., 2., 3., 4.], 'col2': ['1', 'a', 'c' ,'f'],
13532+
'col3': date_range('20111111', periods=4)})
13533+
tm.assert_frame_equal(df.round(), round_0)
13534+
tm.assert_frame_equal(df.round(1), df)
13535+
tm.assert_frame_equal(df.round({'col1':1}), df)
13536+
tm.assert_frame_equal(df.round({'col1':0}), round_0)
13537+
tm.assert_frame_equal(df.round({'col1':0, 'col2':1}), round_0)
13538+
tm.assert_frame_equal(df.round({'col3':1}), df)
13539+
13540+
1352613541
def test_round_issue(self):
1352713542
# GH11611
1352813543

0 commit comments

Comments
 (0)