{"record":{"id":"782f436df2a2773a","repo":"apache/beam","slug":"indexing-a-series-with-key-of-type-type-key-is-not-supported","errorCode":null,"errorMessage":"Indexing a series with key of type {type(key)} is not supported because it produces a non-deferred result.","messagePattern":"Indexing a series with key of type (.+?) is not supported because it produces a non-deferred result\\.","errorType":"exception","errorClass":"WontImplementError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/dataframe/frames.py","lineNumber":1356,"sourceCode":"      return frame_base.DeferredFrame.wrap(\n          expressions.ComputedExpression(\n              # yapf: disable\n              'getitem',\n              lambda df, indexer: df[indexer],\n              [self._expr, key._expr],\n              requires_partition_by=partitionings.Index(),\n              preserves_partition_by=partitionings.Arbitrary()))\n\n    elif pd.core.series.is_iterator(key) or pd.core.common.is_bool_indexer(key):\n      raise frame_base.WontImplementError(\n          \"Accessing a DeferredSeries with an iterator is sensitive to the \"\n          \"order of the data.\",\n          reason=\"order-sensitive\")\n\n    else:\n      # We could consider returning a deferred scalar, but that might\n      # be more surprising than a clear error.\n      raise frame_base.WontImplementError(\n          f\"Indexing a series with key of type {type(key)} is not supported \"\n          \"because it produces a non-deferred result.\",\n          reason=\"non-deferred-result\")\n\n  @frame_base.with_docs_from(pd.Series)\n  def keys(self):\n    return self.index\n\n  # Series.T == transpose. Both are a no-op\n  T = frame_base._elementwise_method('T', base=pd.Series)\n  transpose = frame_base._elementwise_method('transpose', base=pd.Series)\n  shape = property(\n      frame_base.wont_implement_method(\n          pd.Series, 'shape', reason=\"non-deferred-result\"))\n\n  @frame_base.with_docs_from(pd.Series, removed_method=PD_VERSION >= (2, 0))\n  @frame_base.args_to_kwargs(pd.Series, removed_method=PD_VERSION >= (2, 0))\n  @frame_base.populate_defaults(pd.Series, removed_method=PD_VERSION >= (2, 0))","sourceCodeStart":1338,"sourceCodeEnd":1374,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/dataframe/frames.py#L1338-L1374","documentation":"DeferredSeries.__getitem__ falls through to this error for any key type that is not a slice, callable, DeferredSeries, or accepted scalar — e.g. tuples, dicts, lists of labels. Such a key would produce a single concrete (non-deferred) value or an unrepresentable result, which Beam's deferred model refuses by design (reason \"non-deferred-result\"). Beam prefers a clear error over returning a surprising deferred scalar.","triggerScenarios":"s[(1, 2)], s[{'a': 1}], s[[0, 1, 2]] (non-boolean list), or any custom key object on a DeferredSeries.","commonSituations":"MultiIndex-style tuple keys on deferred series; list-of-labels selection copied from pandas; accidental passing of a wrong-typed variable as the key.","solutions":["Check the key type before indexing; use slices, callables (e.g. s[lambda x: ...]), or DeferredSeries keys.","For list-of-labels selection, build a boolean DeferredSeries mask or use a supported selection method.","Extract concrete values only after to_pandas()."],"exampleFix":"// before\ns[[0, 1, 2]]  # list key -> non-deferred result\n// after\ns[s.index.isin([0, 1, 2], level=None)] if supported, or s[s > threshold]  # deferred mask","handlingStrategy":"type-guard","validationCode":"ALLOWED = (slice,)\nif not (isinstance(key, ALLOWED) or callable(key) or isinstance(key, DeferredSeries)):\n    raise TypeError(f\"unsupported deferred indexing key type: {type(key)}\")","typeGuard":"def is_indexable_key(key):\n    return isinstance(key, slice) or callable(key) or isinstance(key, DeferredSeries)","tryCatchPattern":"from apache_beam.dataframe import frame_base\ntry:\n    out = s[key]\nexcept frame_base.WontImplementError as e:\n    if 'non-deferred result' in str(e):\n        out = s.to_pandas()[key]\n    else:\n        raise","preventionTips":["Restrict deferred indexing keys to slices, callables, and DeferredSeries.","Validate the key's type at pipeline-construction time with a narrow helper.","Move concrete (non-deferred) selections to after to_pandas()."],"tags":["apache-beam","dataframe","pandas","indexing","type-mismatch"],"backgroundTag":"type-mismatch","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"}