pandas-dev/pandas · error · TypeError
index must be an integer, got {type(index)}
Error message
index must be an integer, got {type(index)} What it means
Raised by ExtensionArray.item(index) when the provided `index` is not an integer (e.g. a string, float, numpy float64, slice, or None passed positionally as something other than None). The method uses pandas.core.dtypes.common.is_integer to validate; only Python ints (and numpy integer scalars) pass. Reached via pd.array(...).item(index) or Series.array.item(index).
Source
Thrown at pandas/core/arrays/base.py:683
>>> arr = pd.array([1], dtype="Int64")
>>> arr.item()
np.int64(1)
>>> arr = pd.array([1, 2, 3], dtype="Int64")
>>> arr.item(0)
np.int64(1)
>>> arr.item(2)
np.int64(3)
"""
if index is None:
if len(self) != 1:
raise ValueError(
"can only convert an array of size 1 to a Python scalar"
)
return self[0]
else:
if not is_integer(index):
raise TypeError(f"index must be an integer, got {type(index)}")
return self[index]
def to_numpy(
self,
dtype: npt.DTypeLike | None = None,
copy: bool = False,
na_value: object = lib.no_default,
) -> np.ndarray:
"""
Convert to a NumPy ndarray.
This is similar to :meth:`numpy.asarray`, but may provide additional control
over how the conversion is done.
Parameters
----------
dtype : str or numpy.dtype, optional
The dtype to pass to :meth:`numpy.asarray`.View on GitHub (pinned to 71959b8cb9)
Solutions
- Coerce to int: `arr.item(int(index))`.
- Use is_integer from pandas to validate before calling: `from pandas.api.types import is_integer`.
- For slices/lists use `arr[key]` directly instead of item().
- Sanitize upstream so indices are Python ints.
Example fix
# before idx = np.float64(2) arr.item(idx) # TypeError: index must be an integer, got float64 # after arr.item(int(idx))
Defensive patterns
Strategy: type-guard
Validate before calling
from pandas.api.types import is_integer
def safe_item(arr, index):
if not is_integer(index):
raise TypeError(f"index must be int, got {type(index).__name__}")
return arr.item(int(index)) Type guard
import numbers
from pandas.api.types import is_integer
def is_valid_index(v) -> bool:
return is_integer(v) or isinstance(v, numbers.Integral) Try / catch
try:
return arr.item(index)
except TypeError as e:
if "index must be an integer" in str(e):
return arr.item(int(index))
raise Prevention
- Coerce indices to Python int before calling item().
- Sanitize JSON/CSV-derived indices at load time.
- Use arr[key] for slices/lists instead of item().
When it happens
Trigger: Calling `arr.item(0.0)`, `arr.item("0")`, or `arr.item(np.float64(2))`. Also when a slice or list is mistakenly passed to item() instead of an integer index.
Common situations: Index values coming from JSON/CSV as strings or floats; numpy operations returning float64 indices; dynamic indexing code that loses int typing.
Related errors
- {func_name} requires a Series, Index, ExtensionArray, np.nda
- cannot diff {type(arr).__name__} on axis={axis}
- {type(arr).__name__} has no 'diff' method. Convert to a suit
- Column {colname} is backed by an extension array, which is n
- func is expected but received {} in **kwargs.
AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07).
Data as JSON: /api/errors/c2348a99204b3374.
Report an issue: GitHub.