{"record":{"id":"88f3d9d513a63df6","repo":"apache/beam","slug":"nsmallest-keep-keep-r-is-not-supported-because-it-is-order","errorCode":null,"errorMessage":"nsmallest(keep={keep!r}) is not supported because it is order sensitive. Only keep=\"all\" is supported.","messagePattern":"nsmallest\\(keep=(.+?)\\) is not supported because it is order sensitive\\. Only keep=\"all\" is supported\\.","errorType":"exception","errorClass":"WontImplementError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/dataframe/frames.py","lineNumber":2232,"sourceCode":"      return frame_base.DeferredFrame.wrap(\n          expressions.ComputedExpression(\n              'nlargest', lambda df: df.nlargest(**kwargs), [per_partition],\n              preserves_partition_by=partitionings.Arbitrary(),\n              requires_partition_by=partitionings.Singleton()))\n\n  @frame_base.with_docs_from(pd.Series)\n  @frame_base.args_to_kwargs(pd.Series)\n  @frame_base.populate_defaults(pd.Series)\n  def nsmallest(self, keep, **kwargs):\n    \"\"\"Only ``keep=False`` and ``keep=\"any\"`` are supported. Other values of\n    ``keep`` make this an order-sensitive operation. Note ``keep=\"any\"`` is\n    a Beam-specific option that guarantees only one duplicate will be kept, but\n    unlike ``\"first\"`` and ``\"last\"`` it makes no guarantees about _which_\n    duplicate element is kept.\"\"\"\n    if keep == 'any':\n      keep = 'first'\n    elif keep != 'all':\n      raise frame_base.WontImplementError(\n          f\"nsmallest(keep={keep!r}) is not supported because it is \"\n          \"order sensitive. Only keep=\\\"all\\\" is supported.\",\n          reason=\"order-sensitive\")\n    kwargs['keep'] = keep\n    per_partition = expressions.ComputedExpression(\n        'nsmallest-per-partition', lambda df: df.nsmallest(**kwargs),\n        [self._expr],\n        preserves_partition_by=partitionings.Arbitrary(),\n        requires_partition_by=partitionings.Arbitrary())\n    with expressions.allow_non_parallel_operations(True):\n      return frame_base.DeferredFrame.wrap(\n          expressions.ComputedExpression(\n              'nsmallest', lambda df: df.nsmallest(**kwargs), [per_partition],\n              preserves_partition_by=partitionings.Arbitrary(),\n              requires_partition_by=partitionings.Singleton()))\n\n  @property  # type: ignore\n  @frame_base.with_docs_from(pd.Series)","sourceCodeStart":2214,"sourceCodeEnd":2250,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/dataframe/frames.py#L2214-L2250","documentation":"DeferredDataFrame/Series.nsmallest() with keep='first' or 'last' picks tie winners by row order, which a distributed pipeline cannot guarantee. Only keep='all' is supported; any other keep value raises WontImplementError with reason 'order-sensitive'. (keep='any' is accepted and mapped to an arbitrary pick.)","triggerScenarios":"Calling nsmallest(n, keep='first') or keep='last', or relying on pandas' default keep='first' by omitting keep; any keep value other than 'any' or 'all'.","commonSituations":"Direct pandas ports where keep defaults to 'first'; code depending on which duplicate row is retained; typos in the keep string.","solutions":["Call nsmallest(n, ..., keep='all')","Use keep='any' (Beam-specific) when any single duplicate is acceptable","Post-process with drop_duplicates()/groupby if you need to reduce tied rows","Restructure so ties don't matter (e.g. aggregate ties explicitly)"],"exampleFix":"// before\ndf.nsmallest(3, 'latency')\n// after\ndf.nsmallest(3, 'latency', keep='all')","handlingStrategy":"validation","validationCode":"assert keep == 'all', \"nsmallest on Beam dataframes only supports keep='all' (or keep='any')\"","typeGuard":"def beam_safe_keep(keep):\n    return keep in ('all', 'any')","tryCatchPattern":"from apache_beam.dataframe import frame_base\ntry:\n    bottom = df.nsmallest(n, col, keep='all')\nexcept frame_base.WontImplementError:\n    bottom = df.nsmallest(n, col, keep='all')","preventionTips":["Never rely on keep='first'/'last' tie-breaking in distributed pipelines","Pass keep='all' or Beam-specific keep='any' explicitly","Review ported pandas code for omitted keep arguments"],"tags":["pandas","apache-beam","dataframe","order-sensitive"],"backgroundTag":"invalid-enum-value","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"}