{"record":{"id":"a77dd8c95be97b30","repo":"pola-rs/polars","slug":"expected-type-int-str-got-qualified-type-nam-a77dd8","errorCode":null,"errorMessage":"expected type 'int | str', got {qualified_type_name(item)!r} ({item!r})","messagePattern":"expected type 'int \\| str', got (.+?) \\((.+?)\\)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/expr/struct.py","lineNumber":75,"sourceCode":"\n        >>> df.select(pl.col(\"s\").struct[0])\n        shape: (2, 1)\n        ┌─────┐\n        │ x   │\n        │ --- │\n        │ i64 │\n        ╞═════╡\n        │ 1   │\n        │ 2   │\n        └─────┘\n        \"\"\"\n        if isinstance(item, str):\n            return self.field(item)\n        elif isinstance(item, int):\n            return wrap_expr(self._pyexpr.struct_field_by_index(item))\n        else:\n            msg = f\"expected type 'int | str', got {qualified_type_name(item)!r} ({item!r})\"\n            raise TypeError(msg)\n\n    def field(self, name: str | list[str], *more_names: str) -> Expr:\n        \"\"\"\n        Retrieve one or multiple `Struct` field(s) as a new Series.\n\n        .. engine-support:: in-memory, streaming, distributed\n\n        Parameters\n        ----------\n        name\n            Name of the struct field to retrieve.\n        *more_names\n            Additional struct field names.\n\n        Examples\n        --------\n        >>> df = pl.DataFrame(\n        ...     {","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/expr/struct.py#L57-L93","documentation":"Type guard in Expr.struct.__getitem__: struct fields can be selected only by string name or integer index; any other key type (float, slice, list, etc.) raises TypeError reporting the received type and value.","triggerScenarios":"Indexing a struct expression with a non-str/non-int key: a float index, an Expr, a list of names, or None.","commonSituations":"Using numpy integer types that aren't Python int; passing a slice or list where `.field([...])` should be used; accidentally indexing with a Series element.","solutions":["Use a plain `str` field name or Python `int` index","Use `.field(['a','b'])` to retrieve multiple fields","Cast numpy ints with `int(x)` before indexing"],"exampleFix":"// before\npl.col('s').struct[np.int64(2)]\n// after\npl.col('s').struct[2]","handlingStrategy":"type-guard","validationCode":"if not isinstance(item, (str, int)):\n    raise TypeError(f'struct[...] needs str or int, got {type(item).__name__}')","typeGuard":"def is_struct_key(k) -> TypeGuard[str | int]:\n    return isinstance(k, (str, int)) and not isinstance(k, bool)","tryCatchPattern":"try:\n    field = pl.col('s').struct[key]\nexcept TypeError:\n    field = pl.col('s').struct.field(str(key))","preventionTips":["Cast numpy integers to Python int before indexing","Use .field() for multiple/named field access instead of __getitem__"],"tags":["polars","type-error","struct","python"],"backgroundTag":"type-mismatch","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"}