pandas-dev/pandas · error · TypeError
.from_tuples received an invalid item
Error message
{name}.from_tuples received an invalid item, {d} What it means
Raised in the same from_tuples loop when unpacking `lhs, rhs = d` raises TypeError rather than ValueError — i.e. `d` is not iterable/unpackable at all (an int, None that is not NA-detected, a non-iterable object). NA values are handled before unpacking; anything else that is not a tuple of two triggers this.
Solutions
- Filter or coerce non-tuple items: keep only tuples of length 2 (or recognised NA).
- Replace bare scalars with explicit NA: `pd.NA` or `np.nan` so the NA branch handles them.
- Build with `from_arrays` from two parallel lists derived from your data instead of from_tuples.
Example fix
# before pd.arrays.IntervalArray.from_tuples([42, (0,1)]) # after - normalise entries first clean = [x if isinstance(x, tuple) and len(x) == 2 else pd.NA for x in [42, (0,1)]] pd.arrays.IntervalArray.from_tuples(clean)
Defensive patterns
Strategy: validation
Validate before calling
import pandas as pd
def coerce_to_pairs(data):
out = []
for d in data:
if isinstance(d, tuple) and len(d) == 2:
out.append(d)
elif pd.isna(d):
out.append(pd.NA)
else:
out.append(pd.NA)
return out Type guard
import pandas as pd
from collections.abc import Iterable
def all_unpackable_or_na(data) -> bool:
for d in data:
if pd.isna(d):
continue
if not (isinstance(d, tuple) and len(d) == 2):
return False
return True Try / catch
try:
arr = pd.arrays.IntervalArray.from_tuples(data)
except TypeError as e:
if 'invalid item' in str(e):
clean = [d if isinstance(d, tuple) and len(d) == 2 else pd.NA for d in data]
arr = pd.arrays.IntervalArray.from_tuples(clean)
else:
raise Prevention
- Pre-filter rows so every item is a length-2 tuple or recognised NA.
- Use from_arrays with two parallel lists when source data is irregular.
- Normalise bare scalars to pd.NA upstream.
When it happens
Trigger: `IntervalArray.from_tuples([42])`; `from_tuples([None])` where None is not treated as NA by `isna`; `from_tuples([object()])`.
Common situations: Dirty data where some rows are scalars instead of pairs; user passes a list of mixed intervals and bare numbers.
Related errors
- category, object, and string subtypes are not supported for…
- (...) must be called with a collection of some kind, was…
- dtype must be an IntervalDtype, got
- ExtensionArray.fillna does not support filling with a dict…
- Left and right arrays must have matching signedness. Got
AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11).
Data as JSON: /api/errors/13b1d7ff26685cc2.
Report an issue: GitHub.
Appendix: source
Thrown at pandas/core/arrays/interval.py:589
left, right = [], []
else:
# ensure that empty data keeps input dtype
left = right = data
for d in data:
if not isinstance(d, tuple) and isna(d):
lhs = rhs = np.nan
else:
name = cls.__name__
try:
# need list of length 2 tuples, e.g. [(0, 1), (1, 2), ...]
lhs, rhs = d
except ValueError as err:
msg = f"{name}.from_tuples requires tuples of length 2, got {d}"
raise ValueError(msg) from err
except TypeError as err:
msg = f"{name}.from_tuples received an invalid item, {d}"
raise TypeError(msg) from err
left.append(lhs)
right.append(rhs)
return cls.from_arrays(left, right, closed, copy=False, dtype=dtype)
@classmethod
def _validate(cls, left, right, dtype: IntervalDtype) -> None:
"""
Verify that the IntervalArray is valid.
Checks that
* dtype is correct
* left and right match lengths
* left and right have the same missing values
* left is always below right
"""
if not isinstance(dtype, IntervalDtype):View on GitHub (pinned to 3b7651241d)