{"record":{"id":"f1b4a1f34f539d68","repo":"apache/beam","slug":"using-iloc-to-select-rows-is-not-supported-because-it-s","errorCode":null,"errorMessage":"Using iloc to select rows is not supported because it's position-based indexing is sensitive to the order of the data.","messagePattern":"Using iloc to select rows is not supported because it's position-based indexing is sensitive to the order of the data\\.","errorType":"exception","errorClass":"WontImplementError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/dataframe/frames.py","lineNumber":4987,"sourceCode":"            requires_partition_by=(\n                partitionings.JoinIndex()\n                if len(args) > 1\n                else partitionings.Arbitrary()),\n            preserves_partition_by=partitionings.Arbitrary()))\n\n  __setitem__ = frame_base.not_implemented_method(\n      'loc.setitem', base_type=pd.core.indexing._LocIndexer)\n\n@populate_not_implemented(pd.core.indexing._iLocIndexer)\nclass _DeferredILoc(object):\n  def __init__(self, frame):\n    self._frame = frame\n\n  def __getitem__(self, index):\n    if isinstance(index, tuple):\n      rows, _ = index\n      if rows != slice(None, None, None):\n        raise frame_base.WontImplementError(\n            \"Using iloc to select rows is not supported because it's \"\n            \"position-based indexing is sensitive to the order of the data.\",\n            reason=\"order-sensitive\")\n      return frame_base.DeferredFrame.wrap(\n          expressions.ComputedExpression(\n              'iloc',\n              lambda df: df.iloc[index],\n              [self._frame._expr],\n              requires_partition_by=partitionings.Arbitrary(),\n              preserves_partition_by=partitionings.Arbitrary()))\n    else:\n      raise frame_base.WontImplementError(\n          \"Using iloc to select rows is not supported because it's \"\n          \"position-based indexing is sensitive to the order of the data.\",\n          reason=\"order-sensitive\")\n\n  def __setitem__(self, index, value):\n    raise frame_base.WontImplementError(","sourceCodeStart":4969,"sourceCodeEnd":5005,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/dataframe/frames.py#L4969-L5005","documentation":"The __getitem__ of the iloc-based row indexer raises WontImplementError when a row selection other than a full slice (:,) is requested. Position-based (integer-location) indexing depends on the physical order of rows, which Beam does not guarantee during distributed execution, so it is order-sensitive and unsupported.","triggerScenarios":"df.iloc[5], df.iloc[2:10], df.iloc[[0, 3]] or any row selection that is not slice(None) on a deferred Beam DataFrame, e.g. df.iloc[rows, ['col']].","commonSituations":"Porting pandas code that grabs rows by position (first N rows, specific row numbers) to Beam; assuming distributed rows keep their original order; using iloc for head/tail-like operations.","solutions":["Use label-based indexing via .loc instead of .iloc when an index exists.","For 'first N rows', use a supported alternative like a global window + combiner, or restructure to avoid position semantics.","If order matters, sort explicitly and use techniques compatible with Beam (e.g. partition by index stored as a column).","Perform the position-based selection in plain pandas before/after the Beam pipeline."],"exampleFix":"// before\nsubset = df.iloc[2:10, ['a']]\n\n// after\nsubset = df.loc[df.index[2:10], ['a']]  # requires meaningful index","handlingStrategy":"type-guard","validationCode":"rows = idx[0] if isinstance(idx, tuple) else idx\nif not (rows == slice(None)):\n    raise ValueError(\"iloc row selection is unsupported in Beam; use .loc\")","typeGuard":"def beam_safe_iloc_index(idx):\n    rows = idx[0] if isinstance(idx, tuple) else idx\n    return rows == slice(None, None, None)","tryCatchPattern":"try:\n    subset = df.iloc[2:10, ['a']]\nexcept apachebeam.WontImplementError:\n    subset = df.loc[df.index[2:10], ['a']]","preventionTips":["Default to .loc / label-based indexing in Beam DataFrames.","Treat row order as undefined in distributed pipelines.","Use df.iloc[:, cols] only for column selection (all-rows slice)."],"tags":["apache-beam","dataframe","order-sensitive","iloc","pandas"],"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"}