{"record":{"id":"393610b54f743394","repo":"pandas-dev/pandas","slug":"key-must-be-an-int-or-slice-got-type-key-name","errorCode":null,"errorMessage":"key must be an int or slice, got {type(key).__name__}","messagePattern":"key must be an int or slice, got (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/arrow/accessors.py","lineNumber":195,"sourceCode":"            # TODO: Support negative start/stop/step, ideally this would be added\n            # upstream in pyarrow.\n            start, stop, step = key.start, key.stop, key.step\n            if start is None:\n                # TODO: When adding negative step support\n                #  this should be set to last element of array\n                # when step is negative.\n                start = 0\n            if step is None:\n                step = 1\n            sliced = pc.list_slice(self._pa_array, start, stop, step)\n            return Series(\n                sliced,\n                dtype=ArrowDtype(sliced.type),\n                index=self._data.index,\n                name=self._data.name,\n            )\n        else:\n            raise ValueError(f\"key must be an int or slice, got {type(key).__name__}\")\n\n    def __iter__(self) -> Iterator:\n        raise TypeError(f\"'{type(self).__name__}' object is not iterable\")\n\n    def flatten(self) -> Series:\n        \"\"\"\n        Flatten list values.\n\n        Each list element is expanded into separate rows, preserving the\n        original index. The resulting Series may have a longer length than\n        the original if lists contain more than one element.\n\n        Returns\n        -------\n        pandas.Series\n            The data from all lists in the series flattened.\n\n        See Also","sourceCodeStart":177,"sourceCodeEnd":213,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/arrow/accessors.py#L177-L213","documentation":"ListAccessor.__getitem__ only accepts an int (single element index) or a slice. Any other key type (str, list, float, np.ndarray) raises ValueError naming the offending type. This prevents confusing list-element indexing with struct field access or fancy indexing, neither of which the accessor supports.","triggerScenarios":"s.list['a'], s.list[[0,1]], s.list[1.0], or s.list[np.array([0,1])] on a list[pyarrow] Series.","commonSituations":"Confusing .list indexing with .struct.field for named access; expecting fancy/boolean indexing on list elements.","solutions":["Use an int (s.list[0]) or a slice (s.list[0:2]) for indexing.","For multiple element positions, call pc.list_element per index, or flatten first with s.list.flatten()."],"exampleFix":"// before\ns.list[[0, 1]]\n// after\nimport pyarrow.compute as pc\npd.Series(pc.list_element(s.array._pa_array, 0), index=s.index)","handlingStrategy":"type-guard","validationCode":"def list_get(s, key):\n    if not isinstance(key, (int, slice)):\n        raise TypeError(f\"key must be int or slice, got {type(key).__name__}\")\n    return s.list[key]","typeGuard":"def is_int_or_slice(k) -> bool:\n    return isinstance(k, (int, slice))","tryCatchPattern":null,"preventionTips":["Only pass int or slice to s.list[...]","Use .struct.field(name) for named field access, not .list[name]"],"tags":["pyarrow","list-accessor","indexing","argument-error"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}