{"record":{"id":"8ce9e4350845bfbd","repo":"pandas-dev/pandas","slug":"unable-to-avoid-copy-while-creating-an-array-as-re-8ce9e4","errorCode":null,"errorMessage":"Unable to avoid copy while creating an array as requested.","messagePattern":"Unable to avoid copy while creating an array as requested\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/period.py","lineNumber":452,"sourceCode":"            return None  # type: ignore[return-value]\n\n    def __array__(\n        self, dtype: NpDtype | None = None, copy: bool | None = None\n    ) -> np.ndarray:\n        if dtype == \"i8\":\n            # For NumPy 1.x compatibility we cannot use copy=None.  And\n            # `copy=False` has the meaning of `copy=None` here:\n            if not copy:\n                result = np.asarray(self.asi8, dtype=dtype)\n                if self._readonly:\n                    result = result.view()\n                    result.flags.writeable = False\n                return result\n            else:\n                return np.array(self.asi8, dtype=dtype)\n\n        if copy is False:\n            raise ValueError(\n                \"Unable to avoid copy while creating an array as requested.\"\n            )\n\n        if dtype == bool:\n            return ~self._isnan\n\n        # This will raise TypeError for non-object dtypes\n        return np.array(list(self), dtype=object)\n\n    def __arrow_array__(self, type=None):\n        \"\"\"\n        Convert myself into a pyarrow Array.\n        \"\"\"\n        import pyarrow\n\n        from pandas.core.arrays.arrow.extension_types import ArrowPeriodType\n\n        if type is not None:","sourceCodeStart":434,"sourceCodeEnd":470,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/period.py#L434-L470","documentation":"Raised by PeriodArray.__array__ when called with copy=False on a non-int64/non-bool target dtype. PeriodArray stores ordinals as int64, so producing a different dtype (e.g. object array of Period boxes) necessarily requires a copy; passing copy=False forbids it and the request is rejected.","triggerScenarios":"np.asarray(period_array, dtype=object, copy=False), or frameworks that forward copy=False through __array__ (newer NumPy NEP 50 protocol). Also np.array(pa, copy=False) on a PeriodArray.","commonSituations":"NumPy 2.x changed copy semantics (copy=True/False/None); libraries passing copy=False explicitly. Arrow/other integrations that try zero-copy conversion of period arrays to object dtype.","solutions":["Allow the copy: drop copy=False or pass copy=True.","Request the int64 view: np.asarray(pa, dtype='i8', copy=False) which is zero-copy.","Use pa.to_numpy() which manages copy semantics internally."],"exampleFix":"# before\narr = np.asarray(pa, dtype=object, copy=False)\n# after\narr = np.asarray(pa, dtype=object)  # copy allowed\n# or for zero-copy ordinals\narr = np.asarray(pa, dtype='i8', copy=False)","handlingStrategy":"validation","validationCode":"import numpy as np\n\ndef to_numpy_zerocopy_or_allow(pa):\n    try:\n        return np.asarray(pa, dtype='i8', copy=False)\n    except (TypeError, ValueError):\n        return np.asarray(pa)","typeGuard":"import numpy as np\n\ndef is_zerocopy_compatible(pa, dtype) -> bool:\n    return dtype in (None, np.dtype('i8')) or dtype == bool","tryCatchPattern":"try:\n    arr = np.asarray(pa, dtype=target_dtype, copy=False)\nexcept ValueError:\n    arr = np.asarray(pa, dtype=target_dtype)","preventionTips":["Avoid passing copy=False for non-int64 dtypes on PeriodArray.","Use pa.to_numpy() for idiomatic conversion.","Pin/audit NumPy version when copy-protocol behavior matters."],"tags":["period","numpy","copy","conversion","pandas-arrays"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}