From 3fa98951c56b434d47d6cc8fe7b90030049bf89f Mon Sep 17 00:00:00 2001 From: Jeff Reback Date: Fri, 26 Feb 2016 08:35:13 -0500 Subject: [PATCH 1/2] CI: clone depth --- .travis.yml | 2 +- appveyor.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index a408f40f5dc3c..e08bd3b0413c9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,7 +14,7 @@ env: git: # for cloning - depth: 200 + depth: 300 matrix: fast_finish: true diff --git a/appveyor.yml b/appveyor.yml index f6bf947892093..650137b995121 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -9,7 +9,7 @@ matrix: fast_finish: true # immediately finish build once one of the jobs fails. # set clone depth -clone_depth: 200 +clone_depth: 300 environment: global: From 026904ca8ab4b0147c0374d1c4ade405288581af Mon Sep 17 00:00:00 2001 From: Sangmin Park Date: Fri, 26 Feb 2016 23:04:52 -0500 Subject: [PATCH 2/2] ERR: Better error reporting with .transform and an invalid output per GH 10165 --- doc/source/whatsnew/v0.18.1.txt | 2 ++ pandas/core/groupby.py | 6 +++--- pandas/tests/test_groupby.py | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v0.18.1.txt b/doc/source/whatsnew/v0.18.1.txt index 70a1ad4a335ea..57a50064ecac7 100644 --- a/doc/source/whatsnew/v0.18.1.txt +++ b/doc/source/whatsnew/v0.18.1.txt @@ -43,3 +43,5 @@ Performance Improvements Bug Fixes ~~~~~~~~~ + +- Better error reporting on invalid .transform with user defined input (:issue:`10165`) diff --git a/pandas/core/groupby.py b/pandas/core/groupby.py index 8c277568c9e35..ec6a8003c9681 100644 --- a/pandas/core/groupby.py +++ b/pandas/core/groupby.py @@ -3345,9 +3345,9 @@ def _transform_general(self, func, *args, **kwargs): path, res = self._choose_path(fast_path, slow_path, group) except TypeError: return self._transform_item_by_item(obj, fast_path) - except Exception: # pragma: no cover - res = fast_path(group) - path = fast_path + except ValueError: + msg = 'transform must return a scalar value for each group' + raise ValueError(msg) else: res = path(group) diff --git a/pandas/tests/test_groupby.py b/pandas/tests/test_groupby.py index 7ee40a7758011..89ef4decfda39 100644 --- a/pandas/tests/test_groupby.py +++ b/pandas/tests/test_groupby.py @@ -6104,6 +6104,21 @@ def test_nunique_with_object(self): expected = pd.Series([1] * 5, name='name', index=index) tm.assert_series_equal(result, expected) + def test_transform_with_non_scalar_group(self): + # GH 10165 + cols = pd.MultiIndex.from_tuples([ + ('syn', 'A'), ('mis', 'A'), ('non', 'A'), + ('syn', 'C'), ('mis', 'C'), ('non', 'C'), + ('syn', 'T'), ('mis', 'T'), ('non', 'T'), + ('syn', 'G'), ('mis', 'G'), ('non', 'G')]) + df = pd.DataFrame(np.random.randint(1, 10, (4, 12)), + columns=cols, + index=['A', 'C', 'G', 'T']) + self.assertRaisesRegexp(ValueError, 'transform must return a scalar ' + 'value for each group.*', df.groupby + (axis=1, level=1).transform, + lambda z: z.div(z.sum(axis=1), axis=0)) + def assert_fp_equal(a, b): assert (np.abs(a - b) < 1e-12).all()