{"record":{"id":"a48b2d4cffd8e46f","repo":"pandas-dev/pandas","slug":"unary-not-supported-for-dtype-self-dtype","errorCode":null,"errorMessage":"unary '-' not supported for dtype '{self.dtype}'","messagePattern":"unary '-' not supported for dtype '(.+?)'","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/arrow/array.py","lineNumber":1073,"sourceCode":"        return self.to_numpy(dtype=dtype, copy=copy)\n\n    def __invert__(self) -> Self:\n        # This is a bit wise op for integer types\n        if pa.types.is_integer(self._pa_array.type):\n            return self._from_pyarrow_array(pc.bit_wise_not(self._pa_array))\n        elif pa.types.is_string(self._pa_array.type) or pa.types.is_large_string(\n            self._pa_array.type\n        ):\n            # Raise TypeError instead of pa.ArrowNotImplementedError\n            raise TypeError(\"__invert__ is not supported for string dtypes\")\n        else:\n            return self._from_pyarrow_array(pc.invert(self._pa_array))\n\n    def __neg__(self) -> Self:\n        try:\n            return self._from_pyarrow_array(pc.negate_checked(self._pa_array))\n        except pa.ArrowNotImplementedError as err:\n            raise TypeError(\n                f\"unary '-' not supported for dtype '{self.dtype}'\"\n            ) from err\n\n    def __pos__(self) -> Self:\n        return self._from_pyarrow_array(self._pa_array)\n\n    def __abs__(self) -> Self:\n        return self._from_pyarrow_array(pc.abs_checked(self._pa_array))\n\n    # GH 42600: __getstate__/__setstate__ not necessary once\n    # https://issues.apache.org/jira/browse/ARROW-10739 is addressed\n    def __getstate__(self):\n        state = self.__dict__.copy()\n        state[\"_pa_array\"] = self._pa_array.combine_chunks()\n        # cached properties can be recomputed; don't bloat the pickle\n        state[\"_cache\"] = {}\n        return state\n","sourceCodeStart":1055,"sourceCodeEnd":1091,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/arrow/array.py#L1055-L1091","documentation":"Raised by ArrowExtensionArray.__neg__ (unary minus) when pc.negate_checked fails with ArrowNotImplementedError, i.e. the dtype does not support negation (strings, booleans, dates without time, binary, nested types). The check is wrapped so that unsupported types surface a clear TypeError instead of pyarrow's lower-level error.","triggerScenarios":"-arr on a string/boolean/binary/date32/date64 ArrowExtensionArray; -s where s is a pyarrow-backed Series whose dtype is not numeric or duration; applying np.negative to such an array.","commonSituations":"Code that assumes numeric dtype but received string[pyarrow] after inference; date32/date64 columns where unary minus is undefined; boolean pyarrow arrays.","solutions":["Ensure the operand is numeric or duration: check s.dtype.kind in 'iuf' before negating.","Cast to a numeric pyarrow dtype first: s.astype('int64[pyarrow]').","For datetimes/timestamps where you want a duration, use subtraction against an epoch instead of unary minus.","Branch on dtype: if not pa.types.is_signed_integer(...) and not pa.types.is_floating(...) and not pa.types.is_duration(...): skip negation."],"exampleFix":"# before\ns = pd.Series(['a', 'b'], dtype=\"string[pyarrow]\")\n-s  # raises TypeError: unary '-' not supported for dtype 'string[pyarrow]'\n\n# after\nnum = pd.Series([1, 2], dtype=\"int64[pyarrow]\")\n-num","handlingStrategy":"type-guard","validationCode":"import pyarrow as pa\n\ndef supports_negate(arr) -> bool:\n    t = arr._pa_array.type\n    return (pa.types.is_signed_integer(t) or pa.types.is_unsigned_integer(t)\n            or pa.types.is_floating(t) or pa.types.is_duration(t))\n\ndef safe_neg(arr):\n    if not supports_negate(arr):\n        raise TypeError(f\"unary '-' not supported for dtype {arr.dtype}\")\n    return -arr","typeGuard":"import pyarrow as pa\n\ndef is_negatable_dtype(arr) -> bool:\n    t = arr._pa_array.type\n    return any(f(t) for f in (pa.types.is_signed_integer, pa.types.is_floating, pa.types.is_duration))","tryCatchPattern":"try:\n    result = -arr\nexcept TypeError as e:\n    if \"unary '-' not supported\" in str(e):\n        # cast to a compatible numeric dtype if applicable\n        result = -arr.astype('int64[pyarrow]')\n    else:\n        raise","preventionTips":["Validate dtype supports negation before applying unary minus.","Watch for string inference producing non-numeric arrays.","Cast columns to numeric pyarrow dtypes before arithmetic."],"tags":["operator","unary","type-validation","pyarrow"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}