{"record":{"id":"faec3e61c63ace7a","repo":"apache/beam","slug":"integer-slices-are-not-supported-as-they-are-ambiguous","errorCode":null,"errorMessage":"Integer slices are not supported as they are ambiguous. Please use iloc or loc with integer slices.","messagePattern":"Integer slices are not supported as they are ambiguous\\. Please use iloc or loc with integer slices\\.","errorType":"exception","errorClass":"WontImplementError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/dataframe/frames.py","lineNumber":2539,"sourceCode":"\n  def __getitem__(self, key):\n    # TODO: Replicate pd.DataFrame.__getitem__ logic\n    if isinstance(key, DeferredSeries) and key._expr.proxy().dtype == bool:\n      return self.loc[key]\n\n    elif isinstance(key, frame_base.DeferredBase):\n      # Fail early if key is a DeferredBase as it interacts surprisingly with\n      # key in self._expr.proxy().columns\n      raise NotImplementedError(\n          \"Indexing with a non-bool deferred frame is not yet supported. \"\n          \"Consider using df.loc[...]\")\n\n    elif isinstance(key, slice):\n      if _is_null_slice(key):\n        return self\n      elif _is_integer_slice(key):\n        # This depends on the contents of the index.\n        raise frame_base.WontImplementError(\n            \"Integer slices are not supported as they are ambiguous. Please \"\n            \"use iloc or loc with integer slices.\")\n      else:\n        return self.loc[key]\n\n    elif (\n        (isinstance(key, list) and all(key_column in self._expr.proxy().columns\n                                       for key_column in key)) or\n        key in self._expr.proxy().columns):\n      return self._elementwise(lambda df: df[key], 'get_column')\n\n    else:\n      raise NotImplementedError(key)\n\n  def __contains__(self, key):\n    # Checks if proxy has the given column\n    return self._expr.proxy().__contains__(key)\n","sourceCodeStart":2521,"sourceCodeEnd":2557,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/dataframe/frames.py#L2521-L2557","documentation":"Slicing a DeferredFrame with an integer slice like df[1:3] (or a list key with integer slice) is ambiguous: whether it means positional or label-based slicing depends on the actual contents of the index, which the deferred expression layer cannot determine. The library raises WontImplementError telling you to use iloc or loc explicitly.","triggerScenarios":"Using df[slice(start, stop)] where start/stop are ints (detected by _is_integer_slice), e.g. df[0:5], df[10:], on a DeferredDataFrame/Series __getitem__; null slices (df[:]) are fine and return self.","commonSituations":"Porting pandas df[0:100] head-slicing idioms; truncating data in notebooks; code written for default integer RangeIndex assumed.","solutions":["Use df.iloc[0:100] for positional slicing or df.loc[0:100] for label-based slicing, matching your intent","Skip/truncate rows at the PCollection level (e.g. a limit transform) if you just want the first N rows","Use head() where supported instead of integer slicing"],"exampleFix":"// before\ndf[0:100]\n// after\ndf.iloc[0:100]   # positional, or df.loc[0:100] for label-based","handlingStrategy":"validation","validationCode":"assert not isinstance(key, slice) or not (key.start is not None or key.stop is not None) or not all(isinstance(v, int) for v in (key.start, key.stop) if v is not None), 'use .iloc/.loc for integer slices'","typeGuard":"def is_ambiguous_slice(key):\n    return isinstance(key, slice) and any(\n        isinstance(v, int) for v in (key.start, key.stop) if v is not None)","tryCatchPattern":"from apache_beam.dataframe import frame_base\ntry:\n    out = df.iloc[0:100]\nexcept frame_base.WontImplementError:\n    out = df.iloc[0:100]  # df[0:100] would raise; always use iloc/loc","preventionTips":["Replace df[a:b] with df.iloc[a:b] (positional) or df.loc[a:b] (labels) in Beam code","Never rely on RangeIndex defaults to disambiguate slicing","Keep head()/tail() idioms instead of integer slicing for row limits"],"tags":["pandas","apache-beam","dataframe","ambiguous-slice"],"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"}