Skip to content

Commit 7b86fdb

Browse files
committed
PERF: Try fast/slow paths only once in DataFrameGroupby.transform
1 parent 9f6a91a commit 7b86fdb

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

pandas/core/groupby/generic.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -1309,17 +1309,21 @@ def _transform_general(self, func, *args, **kwargs):
13091309
gen = self.grouper.get_iterator(obj, axis=self.axis)
13101310
fast_path, slow_path = self._define_paths(func, *args, **kwargs)
13111311

1312+
path = None
13121313
for name, group in gen:
13131314
object.__setattr__(group, "name", name)
13141315

1315-
# Try slow path and fast path.
1316-
try:
1317-
path, res = self._choose_path(fast_path, slow_path, group)
1318-
except TypeError:
1319-
return self._transform_item_by_item(obj, fast_path)
1320-
except ValueError as err:
1321-
msg = "transform must return a scalar value for each group"
1322-
raise ValueError(msg) from err
1316+
if path is None:
1317+
# Try slow path and fast path.
1318+
try:
1319+
path, res = self._choose_path(fast_path, slow_path, group)
1320+
except TypeError:
1321+
return self._transform_item_by_item(obj, fast_path)
1322+
except ValueError as err:
1323+
msg = "transform must return a scalar value for each group"
1324+
raise ValueError(msg) from err
1325+
else:
1326+
res = path(group)
13231327

13241328
if isinstance(res, Series):
13251329

0 commit comments

Comments
 (0)