Skip to content

Commit 9b5c052

Browse files
rcarnevajreback
authored andcommitted
BUG: DataFrame.round() drops column index name, #11986
1 parent 3667b76 commit 9b5c052

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

doc/source/whatsnew/v0.18.0.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,8 @@ Bug Fixes
483483

484484
- Bug in rich comparison of ``Timedelta`` with a ``numpy.array`` of ``Timedelta`` that caused an infinite recursion (:issue:`11835`)
485485

486+
- Bug in ``DataFrame.round`` dropping column index name (:issue:`11986`)
487+
486488
- Bug in ``df.replace`` while replacing value in mixed dtype ``Dataframe`` (:issue:`11698`)
487489

488490
- Bug in ``Index`` prevents copying name of passed ``Index``, when a new name is not provided (:issue:`11193`)

pandas/core/frame.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4457,12 +4457,16 @@ def _series_round(s, decimals):
44574457
new_cols = [col for col in _dict_round(self, decimals)]
44584458
elif com.is_integer(decimals):
44594459
# Dispatch to Series.round
4460-
new_cols = [_series_round(v, decimals) for _, v in self.iteritems()]
4460+
new_cols = [_series_round(v, decimals)
4461+
for _, v in self.iteritems()]
44614462
else:
4462-
raise TypeError("decimals must be an integer, a dict-like or a Series")
4463+
raise TypeError("decimals must be an integer, "
4464+
"a dict-like, or a Series")
44634465

44644466
if len(new_cols) > 0:
4465-
return concat(new_cols, axis=1)
4467+
return self._constructor(concat(new_cols, axis=1),
4468+
index=self.index,
4469+
columns=self.columns)
44664470
else:
44674471
return self
44684472

pandas/tests/test_frame.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13545,6 +13545,20 @@ def test_round(self):
1354513545
# Make sure this doesn't break existing Series.round
1354613546
tm.assert_series_equal(df['col1'].round(1), expected_rounded['col1'])
1354713547

13548+
# named columns
13549+
# GH 11986
13550+
decimals = 2
13551+
expected_rounded = DataFrame(
13552+
{'col1': [1.12, 2.12, 3.12], 'col2': [1.23, 2.23, 3.23]})
13553+
df.columns.name = "cols"
13554+
expected_rounded.columns.name = "cols"
13555+
tm.assert_frame_equal(df.round(decimals), expected_rounded)
13556+
13557+
# interaction of named columns & series
13558+
tm.assert_series_equal(df['col1'].round(decimals),
13559+
expected_rounded['col1'])
13560+
tm.assert_series_equal(df.round(decimals)['col1'],
13561+
expected_rounded['col1'])
1354813562

1354913563
def test_round_mixed_type(self):
1355013564
# GH11885

pandas/tests/test_series.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3004,6 +3004,7 @@ def _check_accum_op(self, name):
30043004
def test_round(self):
30053005
# numpy.round doesn't preserve metadata, probably a numpy bug,
30063006
# re: GH #314
3007+
self.ts.index.name = "index_name"
30073008
result = self.ts.round(2)
30083009
expected = Series(np.round(self.ts.values, 2), index=self.ts.index,
30093010
name='ts')

0 commit comments

Comments
 (0)