{"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/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/datetimelike.py#L1187-L1223","documentation":"Raised by _add_nat when self.dtype is PeriodDtype. Adding pd.NaT to a PeriodArray is semantically unclear (Period + timedelta shifts by freq multiples, and NaT has no freq), so pandas refuses. For datetime/timedelta dtypes NaT is treated as a timedelta-like and returns all-NaT.","triggerScenarios":"PeriodIndex + pd.NaT, dispatched via __add__ line 1313 into _add_nat at line 1200; the PeriodDtype check at line 1204 fires.","commonSituations":"Generic 'fill with NaT' code paths that operate uniformly over Datetime/Timedelta/Period indexes; broadcasting NaT through mixed-dtype dictionaries.","solutions":["To null out a PeriodIndex elementwise, assign pd.NaT directly via .iloc or use idx.where(cond, other=pd.NaT).","Convert to datetime if you need NaT arithmetic semantics: idx.to_timestamp() + pd.NaT.","Add an integer multiple of the freq instead: period_idx + n shifts by n periods.","Guard on isinstance(idx.dtype, pd.PeriodDtype) before generic NaT addition."],"exampleFix":"// before\nout = period_idx + pd.NaT  # TypeError\n// after\nout = period_idx.where(pd.Series([True, False, True]), other=pd.NaT)","handlingStrategy":"type-guard","validationCode":"from pandas.api.types import is_period_dtype\nif is_period_dtype(idx.dtype):\n    out = idx.where(pd.Series([True]*len(idx)), other=pd.NaT)\nelse:\n    out = idx + pd.NaT","typeGuard":"def rejects_nat_addition(idx) -> bool:\n    from pandas.api.types import is_period_dtype\n    return is_period_dtype(idx.dtype)","tryCatchPattern":"try:\n    out = idx + pd.NaT\nexcept TypeError as e:\n    if 'Cannot add' in str(e) and 'NaT' in str(e):\n        out = idx.where(pd.Series([True]*len(idx)), other=pd.NaT)\n    else:\n        raise","preventionTips":["Do not add pd.NaT to PeriodIndex; assign NaT via where/iloc instead.","Special-case PeriodDtype in generic NaT-arithmetic helpers.","Convert Period to timestamp if NaT-as-timedelta semantics are required."],"tags":["addition","nat","period","type-mismatch"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}