{"record":{"id":"510274544a2cc1fd","repo":"apache/beam","slug":"when-axis-axis-r-only-n-and-or-weights-may-be-specified-frac","errorCode":null,"errorMessage":"When axis={axis!r}, only n and/or weights may be specified. frac, random_state, and replace=True are not yet supported (got frac={frac!r}, random_state={random_state!r}, replace={replace!r}). See https://github.com/apache/beam/issues/21010.","messagePattern":"When axis=(.+?), only n and/or weights may be specified\\. frac, random_state, and replace=True are not yet supported \\(got frac=(.+?), random_state=(.+?), replace=(.+?)\\)\\. See https://github\\.com/apache/beam/issues/21010\\.","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/dataframe/frames.py","lineNumber":3214,"sourceCode":"\n    Note that pandas will raise an error if ``n`` is larger than the length\n    of the dataset, while the Beam DataFrame API will simply return the full\n    dataset in that case.\n\n    sample is fully supported for axis='columns'.\"\"\"\n    if axis in (1, 'columns'):\n      # Sampling on axis=columns just means projecting random columns\n      # Eagerly generate proxy to determine the set of columns at construction\n      # time\n      proxy = self._expr.proxy().sample(n=n, frac=frac, replace=replace,\n                                        weights=weights,\n                                        random_state=random_state, axis=axis)\n      # Then do the projection\n      return self[list(proxy.columns)]\n\n    # axis='index'\n    if frac is not None or random_state is not None or replace:\n      raise NotImplementedError(\n          f\"When axis={axis!r}, only n and/or weights may be specified. \"\n          \"frac, random_state, and replace=True are not yet supported \"\n          f\"(got frac={frac!r}, random_state={random_state!r}, \"\n          f\"replace={replace!r}). See \"\n          \"https://github.com/apache/beam/issues/21010.\")\n\n    if n is None:\n      n = 1\n\n    if isinstance(weights, str):\n      weights = self[weights]\n\n    tmp_weight_column_name = \"___Beam_DataFrame_weights___\"\n\n    if weights is None:\n      self_with_randomized_weights = frame_base.DeferredFrame.wrap(\n          expressions.ComputedExpression(\n          'randomized_weights',","sourceCodeStart":3196,"sourceCodeEnd":3232,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/dataframe/frames.py#L3196-L3232","documentation":"DeferredFrame.sample() with axis='index' only supports sampling by count (n) and/or weights. Passing frac, random_state, or replace=True is not implemented because per-partition sampling semantics cannot be replicated; Beam throws NotImplementedError and points to GitHub issue 21010.","triggerScenarios":"Calling df.sample(frac=0.5) or df.sample(n=..., replace=True, random_state=42) on a Beam deferred DataFrame with axis='index' (the default)","commonSituations":"Converting pandas sampling/bootstrap code to Beam; attempting reproducible sampling with a fixed random_state","solutions":["Use only n and/or weights arguments (omit frac, random_state, replace)","Compute the count yourself and pass n=int(len(df)*frac) after materializing","Materialize with to_pandas() and use pandas sample for full argument support"],"exampleFix":"// before\nsampled = df.beam.sample(frac=0.1, random_state=42)\n// after\nsampled = df.beam.sample(n=int(len(df) * 0.1))  # or use to_pandas().sample(...)","handlingStrategy":"validation","validationCode":"if frac is not None or random_state is not None or replace:\n    raise ValueError('beam sample(axis=index) supports only n and/or weights')","typeGuard":null,"tryCatchPattern":"try:\n    sampled = dframe.sample(n=n)\nexcept NotImplementedError:\n    sampled = dframe.to_pandas().sample(frac=frac, random_state=seed, replace=replace)","preventionTips":["Restrict sampling calls on Beam frames to n/weights","Convert frac to an integer count upstream if the total size is known","Document that reproducible seeded sampling requires materialization"],"tags":["python","apache-beam","dataframe","sampling","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-20T03:17:13.778Z"}