{"record":{"id":"43800293fdf007b1","repo":"apache/beam","slug":"groupby-as-index-false","errorCode":null,"errorMessage":"groupby(as_index=False)","messagePattern":"groupby\\(as_index=False\\)","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/dataframe/frames.py","lineNumber":379,"sourceCode":"      return frame_base.DeferredFrame.wrap(\n          expressions.ComputedExpression(\n              'last', lambda df: df.sort_index().last(offset=offset),\n              [per_partition],\n              preserves_partition_by=partitionings.Arbitrary(),\n              requires_partition_by=partitionings.Singleton()))\n\n  @frame_base.with_docs_from(pd.DataFrame)\n  @frame_base.args_to_kwargs(pd.DataFrame)\n  @frame_base.populate_defaults(pd.DataFrame)\n  def groupby(self, by, level, axis, as_index, group_keys, **kwargs):\n    \"\"\"``as_index`` must be ``True``.\n\n    Aggregations grouping by a categorical column with ``observed=False`` set\n    are not currently parallelizable\n    (`Issue 21827 <https://github.com/apache/beam/issues/21827>`_).\n    \"\"\"\n    if not as_index:\n      raise NotImplementedError('groupby(as_index=False)')\n\n    if axis in (1, 'columns'):\n      return _DeferredGroupByCols(\n          expressions.ComputedExpression(\n              'groupbycols', lambda df: df.groupby(\n                  by, axis=axis, group_keys=group_keys, **kwargs), [self._expr],\n              requires_partition_by=partitionings.Arbitrary(),\n              preserves_partition_by=partitionings.Arbitrary()),\n          group_keys=group_keys)\n\n    if level is None and by is None:\n      raise TypeError(\"You have to supply one of 'by' and 'level'\")\n\n    elif level is not None:\n      if isinstance(level, (list, tuple)):\n        grouping_indexes = level\n      else:\n        grouping_indexes = [level]","sourceCodeStart":361,"sourceCodeEnd":397,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/dataframe/frames.py#L361-L397","documentation":"Beam's groupby() only supports grouping by index labels; as_index=False would require resetting the group keys into columns, which the distributed implementation does not support. It raises NotImplementedError immediately when as_index is falsy.","triggerScenarios":"df.groupby('col', as_index=False).mean() or df.groupby(['a','b'], as_index=False).agg(...) — any groupby call with as_index=False, including indirect calls from duplicated()/drop_duplicates()/aggregate().","commonSituations":"pandas users converting code where as_index=False produced flat DataFrames; aggregation reporting scripts expecting columns instead of a group-key index.","solutions":["Keep as_index=True (default) and call reset_index() on the result to flatten group keys into columns","Restructure the pipeline so downstream consumers read group keys from the index","Run this portion with the pandas backend / convert to pandas if the data fits in memory"],"exampleFix":"# before\ndf.groupby('key', as_index=False).sum()\n# after\ndf.groupby('key').sum().reset_index()","handlingStrategy":"validation","validationCode":"if not as_index:\n    gb = df.groupby(by).agg(agg_fn)\n    result = gb.reset_index()\nelse:\n    result = df.groupby(by, as_index=True).agg(agg_fn)","typeGuard":null,"tryCatchPattern":"try:\n    out = df.groupby(by, as_index=False).agg(fn)\nexcept NotImplementedError:\n    out = df.groupby(by).agg(fn).reset_index()","preventionTips":["Avoid as_index=False in Beam DataFrames","Standardize on groupby().reset_index() for flat results","Search codebase for as_index=False before porting to Beam"],"tags":["python","apache-beam","dataframe","not-implemented"],"backgroundTag":"method-not-implemented","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"}