{"record":{"id":"5a742b7e4f3b805d","repo":"pandas-dev/pandas","slug":"cannot-add-type-self-name-and-type-nat","errorCode":null,"errorMessage":"Cannot add {type(self).__name__} and {type(NaT).__name__}","messagePattern":"Cannot add (.+?) and (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/datetimelike.py","lineNumber":1205,"sourceCode":"            \"DatetimeArray | TimedeltaArray\", self\n        )._ensure_matching_resos(other)\n        return self._add_timedeltalike(other)\n\n    @final\n    def _add_timedeltalike(self, other: Timedelta | TimedeltaArray) -> Self:\n        other_i8, o_mask = self._get_i8_values_and_mask(other)\n        new_values = add_overflowsafe(self.asi8, np.asarray(other_i8, dtype=\"i8\"))\n        res_values = new_values.view(self._ndarray.dtype)\n\n        return type(self)._simple_new(res_values, dtype=self.dtype)\n\n    @final\n    def _add_nat(self) -> Self:\n        \"\"\"\n        Add pd.NaT to self\n        \"\"\"\n        if isinstance(self.dtype, PeriodDtype):\n            raise TypeError(\n                f\"Cannot add {type(self).__name__} and {type(NaT).__name__}\"\n            )\n\n        # GH#19124 pd.NaT is treated like a timedelta for both timedelta\n        # and datetime dtypes\n        result = np.empty(self.shape, dtype=np.int64)\n        result.fill(iNaT)\n        result = result.view(self._ndarray.dtype)  # preserve reso\n        return type(self)._simple_new(result, dtype=self.dtype)\n\n    @final\n    def _sub_nat(self) -> np.ndarray:\n        \"\"\"\n        Subtract pd.NaT from self\n        \"\"\"\n        # GH#19124 Timedelta - datetime is not in general well-defined.\n        # We make an exception for pd.NaT, which in this case quacks\n        # like a timedelta.","sourceCodeStart":1187,"sourceCodeEnd":1223,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/datetimelike.py#L1187-L1223","documentation":"Raised by _add_nat when isinstance(self.dtype, PeriodDtype). Adding NaT to a PeriodArray is undefined (Period has no 'shift by missing duration' semantics), whereas for DatetimeArray/TimedeltaArray NaT is treated as a timedelta producing all-NaT. PeriodArray is therefore explicitly rejected.","triggerScenarios":"PeriodArray + pd.NaT; PeriodIndex arithmetic where the right operand evaluates to NaT (e.g. result of a computation that yields NaT); fillna-style arithmetic that introduces NaT into a Period operation.","commonSituations":"Code generic over datetimelike dtypes that special-cases NaT for datetime/timedelta but is run against a PeriodIndex; NA-propagation logic that adds NaT to shift values.","solutions":["Do not add NaT to a PeriodArray; if you need all-NaT output, construct it directly: PeriodArray(np.full(len, iNaT, dtype='i8'), dtype=self.dtype).","Branch on dtype: handle Period separately (e.g. mask with isna() and assign pd.NaT rather than adding).","If the NaT came from upstream computation, guard it: skip the addition when other is NaT."],"exampleFix":"// before\nres = parr + pd.NaT  # TypeError\n\n// after\nres = parr.copy()\nres[:] = pd.NaT  # produce all-NaT PeriodArray without arithmetic","handlingStrategy":"validation","validationCode":"import numpy as np, pandas as pd\n\ndef add_nat_safe(arr):\n    if isinstance(arr.dtype, pd.PeriodDtype):\n        out = arr.copy()\n        out[:] = pd.NaT\n        return out\n    return arr + pd.NaT","typeGuard":"import pandas as pd\n\ndef is_period_array(a) -> bool:\n    return isinstance(getattr(a, 'dtype', None), pd.PeriodDtype)","tryCatchPattern":"try:\n    res = arr + pd.NaT\nexcept TypeError as e:\n    if 'Cannot add' in str(e) and 'NaT' in str(e):\n        out = arr.copy()\n        out[:] = pd.NaT\n        res = out\n    else:\n        raise","preventionTips":["Do not add NaT to PeriodArray; construct all-NaT directly.","Branch NA-handling on dtype when writing generic datetimelike code.","Skip arithmetic when the other operand is NaT."],"tags":["datetime","arithmetic","period","nat","type-error"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}