pandas-dev/pandas · error · ValueError
Unable to avoid copy while creating an array as requested.
Error message
Unable to avoid copy while creating an array as requested.
What it means
Raised by IntervalArray.__array__ when called with copy=False. The numpy-compatible conversion must build a fresh object array of Interval/NA values, which is inherently a copy; the protocol therefore refuses the no-copy contract rather than silently violating it. Triggered by np.asarray(arr, copy=False) or any code path that requests a zero-copy numpy view.
Source
Thrown at pandas/core/arrays/interval.py:1568
# non-strict inequality when closed != 'both'; at least one side is
# not included in the intervals, so equality does not imply overlapping
return bool(
(self._right[:-1] <= self._left[1:]).all()
or (self._left[:-1] >= self._right[1:]).all()
)
# ---------------------------------------------------------------------
# Conversion
def __array__(
self, dtype: NpDtype | None = None, copy: bool | None = None
) -> np.ndarray:
"""
Return the IntervalArray's data as a numpy array of Interval
objects (with dtype='object')
"""
if copy is False:
raise ValueError(
"Unable to avoid copy while creating an array as requested."
)
left = self._left
right = self._right
mask = self.isna()
closed = self.closed
result = np.empty(len(left), dtype=object)
for i, left_value in enumerate(left):
if mask[i]:
result[i] = np.nan
else:
result[i] = Interval(left_value, right[i], closed)
return result
def __arrow_array__(self, type=None):
"""View on GitHub (pinned to 71959b8cb9)
Solutions
- Let pandas convert: call arr.to_numpy() (defaults to copy=True) or np.asarray(arr) without copy=False.
- If a downstream library passes copy=False, upgrade it or call np.asarray(arr, dtype=object) explicitly.
- Convert to object dtype ahead of time with arr.astype(object).
Example fix
# before np.asarray(interval_arr, copy=False) # after np.asarray(interval_arr, dtype=object)
Defensive patterns
Strategy: fallback
Validate before calling
def to_numpy_object(arr):
return np.asarray(arr, dtype=object) Try / catch
try:
return np.asarray(arr, copy=False)
except ValueError as e:
if 'avoid copy' in str(e):
return np.asarray(arr, dtype=object) Prevention
- Avoid passing copy=False to np.asarray on ExtensionArray subclasses.
- Use arr.to_numpy() for the supported conversion path.
- Pin or upgrade NumPy and downstream libs together to keep __array__ contract aligned.
When it happens
Trigger: Calling np.asarray(interval_array) under NumPy versions that pass copy=False, or library code (e.g. some sklearn/dask paths) explicitly requesting copy=False via __array__.
Common situations: NumPy 2.0 changed __array__ signature to add copy=None|True|False; downstream libs that pass copy=False now hit this. Upgrading NumPy without pinning compatible libs.
Related errors
- {left_base!r} is {right_base!r}
- Unable to avoid copy while creating an array as requested.
- Unable to avoid copy while creating an array as requested.
- Unable to avoid copy while creating an array as requested.
- Unable to import required dependency {_dependency}. Please s
AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07).
Data as JSON: /api/errors/49b1a19a13cdc593.
Report an issue: GitHub.