Skip to content

ERR: Better error reporting with .transform and an invalid output per GH 10165 #12474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ env:

git:
# for cloning
depth: 200
depth: 300

matrix:
fast_finish: true
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.18.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ Performance Improvements

Bug Fixes
~~~~~~~~~

- Better error reporting on invalid .transform with user defined input (:issue:`10165`)
6 changes: 3 additions & 3 deletions pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down