pandas-dev/pandas · error · TypeError
values must be a 1D list-like
Error message
values must be a 1D list-like
What it means
Raised by pandas.core.arrays.numeric._coerce_to_data_and_mask when the input values, after being coerced to a NumPy array, have more than one dimension (values.ndim != 1). IntegerArray/FloatingArray are 1-D extension arrays backed by a single data ndarray plus a boolean mask, so multi-dimensional data is rejected at construction. The check runs after dtype coercion, so even a valid numeric 2-D array (e.g. a matrix) triggers it.
Solutions
- Flatten the input before construction, e.g. np.ravel(values) or values.reshape(-1).
- Operate column-by-column on a DataFrame (each Series is 1-D) instead of passing the whole frame to a masked array.
- Use pd.DataFrame for genuinely 2-D data rather than a 1-D extension array.
Example fix
# before pd.array([[1, 2], [3, 4]], dtype='Int64') # raises # after pd.array(np.array([[1, 2], [3, 4]]).ravel(), dtype='Int64')
Defensive patterns
Strategy: validation
Validate before calling
import numpy as np
def to_masked_numeric(values, dtype):
arr = np.asarray(values)
if arr.ndim != 1:
raise ValueError(f'expected 1-D input, got ndim={arr.ndim}')
return pd.array(arr, dtype=dtype) Type guard
def is_1d_arraylike(obj) -> bool:
import numpy as np
return hasattr(obj, 'ndim') and np.asarray(obj).ndim == 1 Try / catch
try:
pd.array(values, dtype='Int64')
except TypeError as e:
if '1D list-like' in str(e):
values = np.asarray(values).ravel()
pd.array(values, dtype='Int64')
else:
raise Prevention
- Always flatten matrices with .ravel() or .reshape(-1) before passing to a 1-D array constructor.
- Prefer operating on DataFrame columns (Series) rather than 2-D slices when constructing extension arrays.
- Validate ndim==1 in your data-loading layer.
When it happens
Trigger: Calling pd.array([[1, 2], [3, 4]], dtype='Int64'), constructing IntegerArray/FloatingArray from a 2-D numpy array, or passing a DataFrame column block (2-D) to a masked-numeric constructor. Also reachable via astype('Int64') on a DataFrame (per-column is fine, but internal 2-D blocks route through here).
Common situations: Passing a matrix/list-of-lists where a flat vector was intended; reshaping data upstream and forgetting to flatten; converting a wide DataFrame slice instead of a single Series.
Related errors
- FloatingArray does not support np.float16 dtype.
- mask must be a 1D list-like
- values should be numpy array. Use the 'pd.array' function…
- Cannot cast NaN value to Integer dtype.
- cannot convert to ' '-dtype NumPy array with missing…
AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11).
Data as JSON: /api/errors/11fba0d7f7c57064.
Report an issue: GitHub.
Appendix: source
Thrown at pandas/core/arrays/numeric.py:188
if inferred_type == "boolean" and dtype is None:
# object dtype array of bools
name = dtype_cls.__name__.strip("_")
raise TypeError(f"{values.dtype} cannot be converted to {name}")
elif values.dtype.kind == "b" and checker(dtype):
# fastpath
mask = np.zeros(len(values), dtype=np.bool_)
if not copy:
values = np.asarray(values, dtype=default_dtype)
else:
values = np.array(values, dtype=default_dtype, copy=copy)
elif values.dtype.kind not in "iuf":
name = dtype_cls.__name__.strip("_")
raise TypeError(f"{values.dtype} cannot be converted to {name}")
if values.ndim != 1:
raise TypeError("values must be a 1D list-like")
if mask is None:
if values.dtype.kind in "iu":
# fastpath
mask = np.zeros(len(values), dtype=np.bool_)
elif values.dtype.kind == "f":
# np.isnan is faster than is_numeric_na() for floats
# github issue: #60066
if is_nan_na():
mask = np.isnan(values)
else:
mask = np.zeros(len(values), dtype=np.bool_)
if dtype_cls.__name__.strip("_").startswith(("I", "U")):
wrong = np.isnan(values)
if wrong.any():
raise ValueError("Cannot cast NaN value to Integer dtype.")
elif is_nan_na():
mask = libmissing.is_numeric_na(values)View on GitHub (pinned to 3b7651241d)