{"record":{"id":"4abf76894ee7e138","repo":"apache/beam","slug":"len-df-is-not-currently-supported-because-it-produces-a-non","errorCode":null,"errorMessage":"len(df) is not currently supported because it produces a non-deferred result. Consider using df.length() instead.","messagePattern":"len\\(df\\) is not currently supported because it produces a non-deferred result\\. Consider using df\\.length\\(\\) instead\\.","errorType":"exception","errorClass":"WontImplementError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/dataframe/frames.py","lineNumber":778,"sourceCode":"    used in arithmetic with :class:`DeferredSeries` or\n    :class:`DeferredDataFrame` instances.\"\"\"\n    lengths = expressions.ComputedExpression(\n        'get_lengths',\n        # Wrap scalar results in a Series for easier concatenation later\n        lambda df: pd.Series(len(df)),\n        [self._expr],\n        requires_partition_by=partitionings.Arbitrary(),\n        preserves_partition_by=partitionings.Singleton())\n\n    with expressions.allow_non_parallel_operations(True):\n      return frame_base.DeferredFrame.wrap(\n          expressions.ComputedExpression(\n              'sum_lengths', lambda lengths: lengths.sum(), [lengths],\n              requires_partition_by=partitionings.Singleton(),\n              preserves_partition_by=partitionings.Singleton()))\n\n  def __len__(self):\n    raise frame_base.WontImplementError(\n        \"len(df) is not currently supported because it produces a non-deferred \"\n        \"result. Consider using df.length() instead.\",\n        reason=\"non-deferred-result\")\n\n  @property  # type: ignore\n  @frame_base.with_docs_from(pd.DataFrame)\n  def empty(self):\n    empties = expressions.ComputedExpression(\n        'get_empties',\n        # Wrap scalar results in a Series for easier concatenation later\n        lambda df: pd.Series(df.empty),\n        [self._expr],\n        requires_partition_by=partitionings.Arbitrary(),\n        preserves_partition_by=partitionings.Singleton())\n\n    with expressions.allow_non_parallel_operations(True):\n      return frame_base.DeferredFrame.wrap(\n          expressions.ComputedExpression(","sourceCodeStart":760,"sourceCodeEnd":796,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/dataframe/frames.py#L760-L796","documentation":"len(df) on a DeferredDataFrame would have to compute the number of rows immediately, producing a concrete (non-deferred) scalar — which breaks the deferred-execution contract of the Beam DataFrame API. The API raises WontImplementError and points you to df.length(), which returns a deferred scalar expression integrated into the pipeline.","triggerScenarios":"Calling len(df) or bool(df) (which uses __len__ for truthiness) on a DeferredDataFrame.","commonSituations":"Debug prints of row counts in notebooks; `if len(df) == 0:` guards ported from pandas; assertions in pipeline code converted from pandas to Beam.","solutions":["Use df.length() which returns a deferred expression; feed it into the pipeline or collect it via compute().","If a concrete count is required, call beam.dataframe.compute(df.length()) (or to_pandas on the length expression) at a pipeline boundary.","Replace `if len(df)` checks with logic that stays deferred, or restructure to avoid data-dependent control flow."],"exampleFix":"// before\nn = len(df)\n// after\nn_expr = df.length()  # deferred; compute() it if a concrete value is needed","handlingStrategy":"try-catch","validationCode":"if isinstance(df, DeferredDataFrame):\n    n = df.length()  # deferred\nelse:\n    n = len(df)","typeGuard":"from apache_beam.dataframe.frames import DeferredDataFrame\ndef is_deferred_frame(obj) -> bool:\n    return isinstance(obj, DeferredDataFrame)","tryCatchPattern":"try:\n    n = len(df)\nexcept frame_base.WontImplementError:\n    n = beam.dataframe.compute(df.length())","preventionTips":["Never use len() or truthiness checks on deferred frames","Use df.length() for row counts","Avoid data-dependent `if len(df):` control flow in pipelines"],"tags":["apache-beam","dataframe","pandas","len","deferred"],"backgroundTag":"unsupported-operation","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}