From a7127c4bc95a9a67bfee3c48256074d3fb5298a3 Mon Sep 17 00:00:00 2001 From: Manuel Leonhardt Date: Mon, 16 Nov 2015 17:37:56 +0100 Subject: [PATCH 1/3] BUG: fix col iteration in DataFrame.round, #11611 --- pandas/core/frame.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 1e40d77839d3c..20c6329f3d331 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4382,17 +4382,17 @@ def round(self, decimals=0, out=None): from pandas.tools.merge import concat def _dict_round(df, decimals): - for col in df: + for col, vals in df.iteritems(): try: - yield np.round(df[col], decimals[col]) + yield np.round(vals, decimals[col]) except KeyError: - yield df[col] + yield vals if isinstance(decimals, (dict, Series)): new_cols = [col for col in _dict_round(self, decimals)] elif com.is_integer(decimals): # Dispatch to numpy.round - new_cols = [np.round(self[col], decimals) for col in self] + new_cols = [np.round(v, decimals) for _, v in self.iteritems()] else: raise TypeError("decimals must be an integer, a dict-like or a Series") From 645052341eba4f5a971bca6ba85857b2025fb5aa Mon Sep 17 00:00:00 2001 From: Manuel Leonhardt Date: Wed, 18 Nov 2015 14:13:41 +0100 Subject: [PATCH 2/3] BUG: decimals must be unique indexed, #11618 --- pandas/core/frame.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 20c6329f3d331..ca78cda108ac4 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4389,6 +4389,9 @@ def _dict_round(df, decimals): yield vals if isinstance(decimals, (dict, Series)): + if isinstance(decimals, Series): + if not decimals.index.is_unique: + raise ValueError("Index of decimals must be unique") new_cols = [col for col in _dict_round(self, decimals)] elif com.is_integer(decimals): # Dispatch to numpy.round From 05b582c169e9370f3d53de6a89a595658c64cdd8 Mon Sep 17 00:00:00 2001 From: Manuel Leonhardt Date: Wed, 18 Nov 2015 14:13:48 +0100 Subject: [PATCH 3/3] BUG: Added test, added whatsnew entry, #11618 --- doc/source/whatsnew/v0.17.1.txt | 2 ++ pandas/tests/test_frame.py | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/doc/source/whatsnew/v0.17.1.txt b/doc/source/whatsnew/v0.17.1.txt index 046791d4287c9..5a1c607d3cf5a 100755 --- a/doc/source/whatsnew/v0.17.1.txt +++ b/doc/source/whatsnew/v0.17.1.txt @@ -156,3 +156,5 @@ Bug Fixes - Bug in the link-time error caused by C ``inline`` functions on FreeBSD 10+ (with ``clang``) (:issue:`10510`) - Bug in ``DataFrame.to_csv`` in passing through arguments for formatting ``MultiIndexes``, including ``date_format`` (:issue:`7791`) - Bug in ``DataFrame.join()`` with ``how='right'`` producing a ``TypeError`` (:issue:`11519`) +- Bug in ``DataFrame.round()`` with non-unique column index producing a Fatal Python error (:issue:`11611`) +- Bug in ``DataFrame.round()`` with ``decimals`` being a non-unique indexed Series producing extra columns (:issue:`11618`) diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index a743ce4ffef61..9e7b5da08d0ef 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -15791,6 +15791,19 @@ class SubclassedPanel(Panel): dtype='int64') tm.assert_panel_equal(result, expected) + def test_round(self): + # GH11611 + + df = pd.DataFrame(np.random.random([3, 3]), columns=['A', 'B', 'C'], + index=['first', 'second', 'third']) + + dfs = pd.concat((df, df), axis=1) + rounded = dfs.round() + self.assertTrue(rounded.index.equals(dfs.index)) + + decimals = pd.Series([1, 0, 2], index=['A', 'B', 'A']) + self.assertRaises(ValueError, df.round, decimals) + def skip_if_no_ne(engine='numexpr'): if engine == 'numexpr':