Skip to content

Commit 80a2d53

Browse files
skycaptainjreback
authored andcommitted
BUG: fix col iteration in DataFrame.round, #11611
BUG: decimals must be unique indexed, #11618 BUG: Added test, added whatsnew entry, #11618 TST: move round testing to test_format.py
1 parent 677ff75 commit 80a2d53

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

doc/source/whatsnew/v0.17.1.txt

+4
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,11 @@ Bug Fixes
191191
- Bug in the link-time error caused by C ``inline`` functions on FreeBSD 10+ (with ``clang``) (:issue:`10510`)
192192
- Bug in ``DataFrame.to_csv`` in passing through arguments for formatting ``MultiIndexes``, including ``date_format`` (:issue:`7791`)
193193
- Bug in ``DataFrame.join()`` with ``how='right'`` producing a ``TypeError`` (:issue:`11519`)
194+
194195
- Bug in ``Series.quantile`` with empty list results has ``Index`` with ``object`` dtype (:issue:`11588`)
195196
- Bug in ``pd.merge`` results in empty ``Int64Index`` rather than ``Index(dtype=object)`` when the merge result is empty (:issue:`11588`)
196197
- Bug in ``Categorical.remove_unused_categories`` when having ``NaN`` values (:issue:`11599`)
197198
- Bug in ``DataFrame.to_sparse()`` loses column names for MultiIndexes (:issue:`11600`)
199+
200+
- Bug in ``DataFrame.round()`` with non-unique column index producing a Fatal Python error (:issue:`11611`)
201+
- Bug in ``DataFrame.round()`` with ``decimals`` being a non-unique indexed Series producing extra columns (:issue:`11618`)

pandas/core/frame.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -4380,17 +4380,20 @@ def round(self, decimals=0, out=None):
43804380
from pandas.tools.merge import concat
43814381

43824382
def _dict_round(df, decimals):
4383-
for col in df:
4383+
for col, vals in df.iteritems():
43844384
try:
4385-
yield np.round(df[col], decimals[col])
4385+
yield np.round(vals, decimals[col])
43864386
except KeyError:
4387-
yield df[col]
4387+
yield vals
43884388

43894389
if isinstance(decimals, (dict, Series)):
4390+
if isinstance(decimals, Series):
4391+
if not decimals.index.is_unique:
4392+
raise ValueError("Index of decimals must be unique")
43904393
new_cols = [col for col in _dict_round(self, decimals)]
43914394
elif com.is_integer(decimals):
43924395
# Dispatch to numpy.round
4393-
new_cols = [np.round(self[col], decimals) for col in self]
4396+
new_cols = [np.round(v, decimals) for _, v in self.iteritems()]
43944397
else:
43954398
raise TypeError("decimals must be an integer, a dict-like or a Series")
43964399

pandas/tests/test_format.py

+13
Original file line numberDiff line numberDiff line change
@@ -3079,6 +3079,19 @@ def test_round_dataframe(self):
30793079
# Make sure this doesn't break existing Series.round
30803080
tm.assert_series_equal(df['col1'].round(1), expected_rounded['col1'])
30813081

3082+
def test_round_issue(self):
3083+
# GH11611
3084+
3085+
df = pd.DataFrame(np.random.random([3, 3]), columns=['A', 'B', 'C'],
3086+
index=['first', 'second', 'third'])
3087+
3088+
dfs = pd.concat((df, df), axis=1)
3089+
rounded = dfs.round()
3090+
self.assertTrue(rounded.index.equals(dfs.index))
3091+
3092+
decimals = pd.Series([1, 0, 2], index=['A', 'B', 'A'])
3093+
self.assertRaises(ValueError, df.round, decimals)
3094+
30823095
class TestSeriesFormatting(tm.TestCase):
30833096
_multiprocess_can_split_ = True
30843097

pandas/tests/test_frame.py

-1
Original file line numberDiff line numberDiff line change
@@ -15859,7 +15859,6 @@ class SubclassedPanel(Panel):
1585915859
dtype='int64')
1586015860
tm.assert_panel_equal(result, expected)
1586115861

15862-
1586315862
def skip_if_no_ne(engine='numexpr'):
1586415863
if engine == 'numexpr':
1586515864
try:

0 commit comments

Comments
 (0)