pandas-dev/pandas · error · ValueError
NumpyExtensionArray must be 1-dimensional.
Error message
NumpyExtensionArray must be 1-dimensional.
What it means
Raised by NumpyExtensionArray.__init__ when values.ndim == 0 (a scalar 0-dimensional ndarray). NumpyExtensionArray is documented as 1-dimensional; a 0-D input has no length and cannot back an ExtensionArray. (2-D is technically accepted but not advertised, per the inline comment.) The check is on ndim==0 specifically.
Solutions
- Wrap the scalar in a 1-element 1-D array: np.atleast_1d(np.asarray(value)).
- Use np.array([value]) explicitly to get a 1-D length-1 array.
- Reconsider whether an ExtensionArray is the right container for a single scalar.
Example fix
# before pd.arrays.NumpyExtensionArray(np.array(5)) # raises (0-D) # after pd.arrays.NumpyExtensionArray(np.atleast_1d(np.array(5)))
Defensive patterns
Strategy: validation
Validate before calling
import numpy as np
def to_1d_numpy_extension_array(values):
arr = np.asarray(values)
if arr.ndim == 0:
arr = np.atleast_1d(arr)
return pd.arrays.NumpyExtensionArray(arr) Type guard
import numpy as np
def is_nonzero_nd(obj) -> bool:
return hasattr(obj, 'ndim') and obj.ndim >= 1 Try / catch
try:
pd.arrays.NumpyExtensionArray(values)
except ValueError as e:
if '1-dimensional' in str(e):
pd.arrays.NumpyExtensionArray(np.atleast_1d(np.asarray(values)))
else:
raise Prevention
- Wrap scalars with np.atleast_1d() or np.array([value]) before construction.
- Check .ndim on results of reductions (sum/mean) before re-wrapping.
- Use pd.array(value) which handles scalar-to-1-D promotion.
When it happens
Trigger: pd.arrays.NumpyExtensionArray(np.array(5)) — np.array(5) is 0-D. Passing the result of np.asarray(scalar) or np.float64(1.0). Extracting a scalar via .item()-like ops and feeding it back.
Common situations: Code that indexes a single element and keeps it as a 0-D array (e.g. arr[arr > 0].sum() shape) then tries to wrap it; arithmetic producing a scalar numpy value.
Related errors
- Cannot construct from scalar data. Pass a sequence instead.
- 'values' must be a NumPy array, not
- Array with ndim > 2 is not supported.
- Cannot create a from a MultiIndex.
- cannot evaluate scalar only bool ops
AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11).
Data as JSON: /api/errors/8e949984b3d9d01e.
Report an issue: GitHub.
Appendix: source
Thrown at pandas/core/arrays/numpy_.py:134
_dtype: NumpyEADtype
_internal_fill_value = np.nan
# ------------------------------------------------------------------------
# Constructors
def __init__(
self, values: np.ndarray | NumpyExtensionArray, copy: bool = False
) -> None:
if isinstance(values, type(self)):
values = values._ndarray
if not isinstance(values, np.ndarray):
raise ValueError(
f"'values' must be a NumPy array, not {type(values).__name__}"
)
if values.ndim == 0:
# Technically we support 2, but do not advertise that fact.
raise ValueError("NumpyExtensionArray must be 1-dimensional.")
if copy:
values = values.copy()
dtype = NumpyEADtype(values.dtype)
super().__init__(values, dtype)
@classmethod
def _from_sequence(
cls, scalars, *, dtype: Dtype | None = None, copy: bool = False
) -> NumpyExtensionArray:
if isinstance(dtype, NumpyEADtype):
dtype = dtype._dtype
if dtype is not None:
dtype = np.dtype(dtype) # type: ignore[arg-type]
if dtype is not None and dtype.kind in "iu":
# GH#41724 - validate NaN before casting float -> intView on GitHub (pinned to 3b7651241d)