diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index 7d6690a0dfa5a..cb05d56216aec 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -646,20 +646,21 @@ def _choose_path(self, fast_path, slow_path, group): # if we make it here, test if we can use the fast path try: res_fast = fast_path(group) - - # verify fast path does not change columns (and names), otherwise - # its results cannot be joined with those of the slow path - if res_fast.columns != group.columns: - return path, res - # verify numerical equality with the slow path - if res.shape == res_fast.shape: - res_r = res.values.ravel() - res_fast_r = res_fast.values.ravel() - mask = notna(res_r) - if (res_r[mask] == res_fast_r[mask]).all(): - path = fast_path except Exception: - pass + # Hard to know ex-ante what exceptions `fast_path` might raise + return path, res + + # verify fast path does not change columns (and names), otherwise + # its results cannot be joined with those of the slow path + if not isinstance(res_fast, DataFrame): + return path, res + + if not res_fast.columns.equals(group.columns): + return path, res + + if res_fast.equals(res): + path = fast_path + return path, res def _transform_item_by_item(self, obj, wrapper):