{"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":744,"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":726,"sourceCodeEnd":762,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/lazyframe/frame.py#L726-L762","documentation":"LazyFrame does not support positional/column subscripting like DataFrame does. __getitem__ only accepts a slice object; any other key (string column name, int, tuple, list) raises TypeError because lazy evaluation cannot cheaply resolve a single column access the way an in-memory DataFrame can. Use select() for column access or filter() for row selection.","triggerScenarios":"Calling lf['colname'], lf[0], lf[['a','b']], or lf[lf['a'] > 1] on a LazyFrame instead of a DataFrame. Common when code written for pl.DataFrame is reused on pl.scan_csv()/pl.LazyFrame output, or when a function receives a DataFrame in tests but a LazyFrame in production.","commonSituations":"Porting DataFrame code to lazy evaluation; generic helper functions that accept either frame type; interactive exploration where users expect pandas-like __getitem__ semantics.","solutions":["Replace lf['a'] with lf.select('a') (optionally .collect() afterwards)","Replace row filtering lf[lf['a'] > 1] with lf.filter(pl.col('a') > 1)","Replace multi-column access lf[['a','b']] with lf.select(['a','b'])","Keep slicing: lf[2:10] is valid and maps to LazyFrame.slice()","If DataFrame semantics are intended, call lf.collect() first and index the resulting DataFrame"],"exampleFix":"# before\nval = lf['a']\n\n# after\nval = lf.select('a').collect()['a']","handlingStrategy":"type-guard","validationCode":"from polars import LazyFrame\nif isinstance(frame, LazyFrame):\n    out = frame.select('a')\nelse:\n    out = frame['a']","typeGuard":"def is_lazy_frame(df) -> bool:\n    from polars import LazyFrame\n    return isinstance(df, LazyFrame)","tryCatchPattern":null,"preventionTips":["Write column access as .select()/.filter() so code works for both frame types","Centralize frame access in helpers that accept DataFrame | LazyFrame","Rely on type hints (pl.LazyFrame vs pl.DataFrame) to catch misuse statically"],"tags":["polars","lazyframe","subscripting","typeerror"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}