{"record":{"id":"6a5358461a53b4a0","repo":"apache/beam","slug":"by","errorCode":null,"errorMessage":"by","messagePattern":"by","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/dataframe/frames.py","lineNumber":552,"sourceCode":"\n      if grouping_columns:\n        # TODO(https://github.com/apache/beam/issues/20759):\n        # It should be possible to do this without creating\n        # an expression manually, by using DeferredDataFrame.set_index, i.e.:\n        #   to_group_with_index = self.set_index([self.index] +\n        #                                        grouping_columns)._expr\n        to_group_with_index = expressions.ComputedExpression(\n            'move_grouped_columns_to_index',\n            lambda df: df.set_index([df.index] + grouping_columns, drop=False),\n            [self._expr],\n            requires_partition_by=partitionings.Arbitrary(),\n            preserves_partition_by=partitionings.Index(\n                list(range(self._expr.proxy().index.nlevels))))\n      else:\n        to_group_with_index = self._expr\n\n    else:\n      raise NotImplementedError(by)\n\n    return DeferredGroupBy(\n        expressions.ComputedExpression(\n            'groupbyindex', lambda df: df.groupby(\n                level=list(range(df.index.nlevels)), group_keys=group_keys, **\n                kwargs), [to_group],\n            requires_partition_by=partitionings.Index(),\n            preserves_partition_by=partitionings.Arbitrary()),\n        kwargs,\n        to_group,\n        to_group_with_index,\n        grouping_columns=grouping_columns,\n        grouping_indexes=grouping_indexes,\n        group_keys=group_keys)\n\n  @property  # type: ignore\n  @frame_base.with_docs_from(pd.DataFrame)\n  def loc(self):","sourceCodeStart":534,"sourceCodeEnd":570,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/dataframe/frames.py#L534-L570","documentation":"Beam's groupby only accepts by as column names or index level names. Other pandas-legal forms of by (e.g. a Series, mapping, function, or axis=1 column grouping) are not supported and hit NotImplementedError(by), echoing the unsupported by value.","triggerScenarios":"df.groupby(lambda x: x % 2), df.groupby(some_series), df.groupby({'a': 'grp'}), or df.groupby('col', axis=1) — any by that is not label(s) present in columns/index names.","commonSituations":"Direct ports of pandas code using callables or external Series as group keys; attempts to group columns (axis=1); passing a Grouping-spec dict for renaming.","solutions":["Convert the callable/mapping to a column first (df['g'] = by_result) then group by that column name","If grouping by an external Series, merge/assign it as a column before groupby","Group only by index level names via level= instead of unsupported by forms"],"exampleFix":"// before\ndf.groupby(lambda x: x[0])\n// after\ndf['first_letter'] = df.index.str[0]\ndf.groupby('first_letter')","handlingStrategy":"type-guard","validationCode":"if callable(by) or not isinstance(by, (str, list, tuple)) or (isinstance(by, (list, tuple)) and not all(isinstance(b, str) for b in by)):\n    raise NotImplementedError(\"Beam groupby supports only column/index label names for by\")","typeGuard":"def by_is_labels(by):\n    return isinstance(by, str) or (isinstance(by, (list, tuple)) and all(isinstance(b, str) for b in by))","tryCatchPattern":"try:\n    out = df.groupby(by)\nexcept NotImplementedError:\n    df = df.assign(gkey=by_fn(df.index) if callable(by) else None)\n    out = df.groupby('gkey')","preventionTips":["Materialize callables/mappings into a column before groupby","Do not use Series, dicts, or functions as by in Beam","Group by named index levels via level= when possible"],"tags":["python","apache-beam","dataframe","not-implemented"],"backgroundTag":"unsupported-operation","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}