diff --git a/doc/source/whatsnew/v0.17.1.txt b/doc/source/whatsnew/v0.17.1.txt index e4b4317c793f7..f1959e0fbb83e 100755 --- a/doc/source/whatsnew/v0.17.1.txt +++ b/doc/source/whatsnew/v0.17.1.txt @@ -177,3 +177,5 @@ Bug Fixes - Bug in ``DataFrame.join()`` with ``how='right'`` producing a ``TypeError`` (:issue:`11519`) - Bug in ``Series.quantile`` with empty list results has ``Index`` with ``object`` dtype (:issue:`11588`) - Bug in ``pd.merge`` results in empty ``Int64Index`` rather than ``Index(dtype=object)`` when the merge result is empty (:issue:`11588`) +- 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/core/frame.py b/pandas/core/frame.py index 5447574c9ea4e..08c8ed1e3e058 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4382,17 +4382,20 @@ 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)): + 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 - 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") diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index 0cae8b356b517..e374c221892ca 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -15821,6 +15821,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':