{"record":{"id":"b7218c3fcc49a7d8","repo":"pola-rs/polars","slug":"invalid-input-for-copy-copy-r-b7218c","errorCode":null,"errorMessage":"invalid input for `copy`: {copy!r}","messagePattern":"invalid input for `copy`: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/series/series.py","lineNumber":1606,"sourceCode":"        See Also\n        --------\n        __array_ufunc__\n        \"\"\"\n        # Cast String types to fixed-length string to support string ufuncs\n        # TODO: Use variable-length strings instead when NumPy 2.0.0 comes out:\n        # https://numpy.org/devdocs/reference/routines.dtypes.html#numpy.dtypes.StringDType\n        if dtype is None and not self.has_nulls() and self.dtype == String:\n            dtype = np.dtype(\"U\")\n\n        if copy is None:\n            writable, allow_copy = False, True\n        elif copy is True:\n            writable, allow_copy = True, True\n        elif copy is False:\n            writable, allow_copy = False, False\n        else:\n            msg = f\"invalid input for `copy`: {copy!r}\"\n            raise TypeError(msg)\n\n        arr = self.to_numpy(writable=writable, allow_copy=allow_copy)\n\n        if dtype is not None and dtype != arr.dtype:\n            if copy is False:\n                # TODO: Only raise when data must be copied\n                msg = f\"copy not allowed: cast from {arr.dtype} to {dtype} prohibited\"\n                raise RuntimeError(msg)\n\n            arr = arr.__array__(dtype)\n\n        return arr\n\n    def __array_ufunc__(\n        self, ufunc: np.ufunc, method: str_, *inputs: Any, **kwargs: Any\n    ) -> Series:\n        \"\"\"Numpy universal functions.\"\"\"\n        if self._s.n_chunks() > 1:","sourceCodeStart":1588,"sourceCodeEnd":1624,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/series/series.py#L1588-L1624","documentation":"Raised in Series.__array__ (the numpy array-protocol hook used by np.asarray) when the `copy` argument is anything other than None, True, or False. The protocol contract allows only those three values; anything else - strings like 'never'/'if-needed', integers other than 0/1, custom sentinels - is rejected.","triggerScenarios":"`np.asarray(s, copy='never')`, `s.__array__(copy='if-needed')`, `np.asarray(s, copy=2)`, or code passing a numpy-1.x `np.copy` style constant. Only the three canonical values map to (writable, allow_copy) tuples; everything else falls into the raise.","commonSituations":"Code written for libraries with richer copy semantics (CuPy uses copy='never'/'if-needed' strings); mechanical migration of np.array(...) calls where copy had legacy meanings; kwargs forwarded from another function with an unexpected value.","solutions":["Pass a supported value: copy=None (allow copy when needed), copy=True (writable copy), copy=False (no copies permitted).","Use the direct API instead of the protocol hook: `s.to_numpy()` / `s.to_numpy(writable=True)`.","If forwarding kwargs, normalize before the call: `copy = {None, True, False}.get(copy, None)` or validate explicitly."],"exampleFix":"// before\nnp.asarray(s, copy='never')  # TypeError\n\n// after\nnp.asarray(s, copy=False)\n# or\ns.to_numpy()","handlingStrategy":"validation","validationCode":"assert copy is None or isinstance(copy, bool) or copy in (0, 1), f'invalid copy={copy!r}'\narr = np.asarray(s, copy=copy if isinstance(copy, bool) or copy is None else bool(copy))","typeGuard":"def valid_copy_param(copy) -> bool:\n    return copy is None or isinstance(copy, bool)","tryCatchPattern":"try:\n    arr = np.asarray(s, copy=copy)\nexcept TypeError as e:\n    if 'invalid input for `copy`' not in str(e):\n        raise\n    arr = s.to_numpy()","preventionTips":["Only None/True/False cross the numpy array protocol; normalize CuPy-style strings before forwarding.","Prefer s.to_numpy() when you control the call site."],"tags":["polars","series","numpy","interop","array-protocol","copy"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}