{"record":{"id":"028e90d1d2abe487","repo":"pola-rs/polars","slug":"lazyframe-is-not-subscriptable-aside-from-slicing","errorCode":null,"errorMessage":"LazyFrame is not subscriptable (aside from slicing)\n\nUse `select()` or `filter()` instead.","messagePattern":"LazyFrame is not subscriptable \\(aside from slicing\\)\n\nUse `select\\(\\)` or `filter\\(\\)` instead\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/lazyframe/frame.py","lineNumber":804,"sourceCode":"        │ 2   ┆ 5   │\n        └─────┴─────┘\n        >>> lf[::2].collect()\n        shape: (2, 2)\n        ┌─────┬─────┐\n        │ a   ┆ b   │\n        │ --- ┆ --- │\n        │ i64 ┆ i64 │\n        ╞═════╪═════╡\n        │ 1   ┆ 4   │\n        │ 3   ┆ 6   │\n        └─────┴─────┘\n        \"\"\"\n        if not isinstance(item, slice):\n            msg = (\n                \"LazyFrame is not subscriptable (aside from slicing)\"\n                \"\\n\\nUse `select()` or `filter()` instead.\"\n            )\n            raise TypeError(msg)\n        return LazyPolarsSlice(self).apply(item)\n\n    def __str__(self) -> str:\n        return f\"\"\"\\\nnaive plan: (run LazyFrame.explain(optimized=True) to see the optimized plan)\n\n{self.explain(optimized=False)}\\\n\"\"\"\n\n    def __repr__(self) -> str:\n        # don't expose internal/private classpath\n        return f\"<{self.__class__.__name__} at 0x{id(self):X}>\"\n\n    def _repr_html_(self) -> str:\n        try:\n            dot = self._ldf.to_dot(optimized=False)\n            svg = subprocess.check_output(\n                [\"dot\", \"-Nshape=box\", \"-Tsvg\"], input=f\"{dot}\".encode()","sourceCodeStart":786,"sourceCodeEnd":822,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/lazyframe/frame.py#L786-L822","documentation":"LazyFrame.__getitem__ only allows slices (row slicing), because a lazy plan cannot eagerly evaluate arbitrary column/row lookups. Any other subscript (integer, string column name, tuple) raises this TypeError. Polars directs you to the lazy-native equivalents select() and filter().","triggerScenarios":"`lf['a']`, `lf[0]`, `lf[:, 'a']`, `lf[0, 1]` — any non-slice index on a LazyFrame.","commonSituations":"Porting DataFrame indexing idioms to the lazy API; interactive exploration habits applied to lazy frames; dynamic column access in pipelines.","solutions":["Use `lf.select('a')` instead of `lf['a']`","Use `lf.filter(...)` predicates instead of row indexing","Collect first if eager indexing is truly needed: `lf.collect()['a']`","Row slicing is allowed: `lf[:10]` works"],"exampleFix":"// before\nvalues = lf['a']\n\n// after\nvalues = lf.select('a')\n# or, if eager access is required:\nvalues = lf.collect()['a']","handlingStrategy":"type-guard","validationCode":"if isinstance(df, pl.LazyFrame) and not isinstance(key, slice):\n    df = df.select(key) if isinstance(key, str) else df.collect()[key]","typeGuard":"def is_lazy_subscriptable(key) -> bool:\n    return isinstance(key, slice)","tryCatchPattern":"try:\n    col = lf[key]\nexcept TypeError as e:\n    if \"not subscriptable\" in str(e):\n        col = lf.select(key)\n    else:\n        raise","preventionTips":["Translate df[...] idioms to select()/filter() when using the lazy API","Only slices are supported by __getitem__ — check isinstance(item, slice)","Materialize with collect() before indexed access"],"tags":["python","polars","lazyframe","subscript"],"backgroundTag":"unsupported-operation","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}