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 ArrowExtensionArray.__array__ when called with copy=False (e.g. np.asarray(arr, copy=False)). PyArrow-backed arrays cannot expose a zero-copy numpy view in general, so requesting copy=False is impossible to satisfy. The implementation deliberately rejects the request rather than silently copying, following NEP 50/__array__ copy semantics.

Source

Thrown at pandas/core/arrays/arrow/array.py:1023

            remask = functools.partial(pa.array, mask=mask, from_pandas=False)
            if isinstance(result, tuple):
                return tuple(type(self)(remask(res)) for res in result)
            return type(self)(remask(result))

        # Need to wrap np.array results GH#62800
        result = super().__array_ufunc__(ufunc, method, *inputs, **kwargs)
        if type(self) is ArrowExtensionArray:
            # Exclude ArrowStringArray
            return type(self)._from_sequence(result)
        return result

    def __array__(
        self, dtype: NpDtype | None = None, copy: bool | None = None
    ) -> np.ndarray:
        """Correctly construct numpy arrays when passed to `np.asarray()`."""
        if copy is False:
            # TODO: By using `zero_copy_only` it may be possible to implement this
            raise ValueError(
                "Unable to avoid copy while creating an array as requested."
            )
        elif copy is None:
            # `to_numpy(copy=False)` has the meaning of NumPy `copy=None`.
            copy = False

        return self.to_numpy(dtype=dtype, copy=copy)

    def __invert__(self) -> Self:
        # This is a bit wise op for integer types
        if pa.types.is_integer(self._pa_array.type):
            return self._from_pyarrow_array(pc.bit_wise_not(self._pa_array))
        elif pa.types.is_string(self._pa_array.type) or pa.types.is_large_string(
            self._pa_array.type
        ):
            # Raise TypeError instead of pa.ArrowNotImplementedError
            raise TypeError("__invert__ is not supported for string dtypes")
        else:

View on GitHub (pinned to 71959b8cb9)

Solutions

  1. Allow a copy: np.asarray(arrow_arr) or np.asarray(arrow_arr, copy=True).
  2. Use arr.to_numpy(copy=None) which maps None to pandas copy semantics.
  3. If you must avoid copies, work with the underlying pyarrow array: arr._pa_array.to_numpy(zero_copy_only=False).
  4. Refactor the caller to not pass copy=False for extension arrays.

Example fix

# before
np_arr = np.asarray(arrow_arr, copy=False)  # ValueError
# after
np_arr = np.asarray(arrow_arr)              # copy allowed
# or
np_arr = arrow_arr.to_numpy()               # pandas path
Defensive patterns

Strategy: validation

Validate before calling

import numpy as np

def to_numpy_no_fail(arr, copy=None):
    if copy is False:
        copy = None  # ArrowExtensionArray cannot zero-copy to numpy
    return np.asarray(arr, copy=copy) if np.lib.NumpyVersion(np.__version__) >= '2.0.0' else np.asarray(arr)

np_arr = to_numpy_no_fail(arrow_arr, copy=False)

Type guard

def supports_zero_copy_numpy(arr) -> bool:
    # ArrowExtensionArray never supports copy=False via __array__
    from pandas.core.arrays.arrow import ArrowExtensionArray
    return not isinstance(arr, ArrowExtensionArray)

Try / catch

try:
    np_arr = np.asarray(arrow_arr, copy=False)
except ValueError as e:
    if 'avoid copy' in str(e):
        np_arr = np.asarray(arrow_arr)
    else:
        raise

Prevention

When it happens

Trigger: `np.asarray(arrow_arr, copy=False)`, `np.array(arrow_arr, copy=False)`, or libraries (e.g. newer numpy/sklearn) passing copy=False to __array__. Also `arr.to_numpy(copy=False)` is fine (handled separately) but direct np.asarray with copy=False hits __array__.

Common situations: Code optimized to avoid copies passing copy=False unconditionally; sklearn/scipy-style `check_array(copy=False)`; migration to numpy>=2.0 where copy semantics became stricter.

Related errors


AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07). Data as JSON: /api/errors/d5246469177196c3. Report an issue: GitHub.