{"record":{"id":"8f9b807ee491b7fe","repo":"pandas-dev/pandas","slug":"cannot-add-type-self-name-and-type-other","errorCode":null,"errorMessage":"cannot add {type(self).__name__} and {type(other).__name__}","messagePattern":"cannot add (.+?) and (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/datetimelike.py","lineNumber":1047,"sourceCode":"        \"\"\"\n        Get the int64 values and b_mask to pass to add_overflowsafe.\n        \"\"\"\n        if isinstance(other, Period):\n            i8values = other.ordinal\n            mask = None\n        elif isinstance(other, (Timestamp, Timedelta)):\n            i8values = other._value\n            mask = None\n        else:\n            # PeriodArray, DatetimeArray, TimedeltaArray\n            mask = other._isnan\n            i8values = other.asi8\n        return i8values, mask\n\n    @final\n    def _add_datetimelike_scalar(self, other) -> DatetimeArray:\n        if not lib.is_np_dtype(self.dtype, \"m\"):\n            raise TypeError(\n                f\"cannot add {type(self).__name__} and {type(other).__name__}\"\n            )\n\n        self = cast(\"TimedeltaArray\", self)\n\n        from pandas.core.arrays import DatetimeArray\n        from pandas.core.arrays.datetimes import tz_to_dtype\n\n        assert other is not NaT\n        if isna(other):\n            # i.e. np.datetime64(\"NaT\")\n            # In this case we specifically interpret NaT as a datetime, not\n            # the timedelta interpretation we would get by returning self + NaT\n            result = self._ndarray + NaT.to_datetime64().astype(f\"M8[{self.unit}]\")\n            # Preserve our resolution\n            return DatetimeArray._simple_new(result, dtype=result.dtype)\n\n        other = Timestamp(other)","sourceCodeStart":1029,"sourceCodeEnd":1065,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/datetimelike.py#L1029-L1065","documentation":"Raised by _add_datetimelike_scalar when a datelike scalar (datetime/Timestamp/np.datetime64) is added to an array whose dtype is not timedelta (kind != 'm'). The rule is that TimedeltaArray + datetime -> DatetimeArray is well-defined, but DatetimeArray + datetime or PeriodArray + datetime is not; pandas refuses rather than guess.","triggerScenarios":"DatetimeIndex + datetime scalar (e.g. idx + pd.Timestamp('2020-01-01')), or PeriodIndex + datetime, dispatched through __add__ at line 1324 into _add_datetimelike_scalar at line 1045, which hits the guard at 1046. Also triggered by reversed ops via __radd__.","commonSituations":"Confusing datetime+datetime with datetime+timedelta arithmetic; forgetting to wrap a date column in pd.Timedelta; data ingestion that stored offsets as datetime instead of timedelta.","solutions":["Replace the datetime operand with a Timedelta: idx + pd.Timedelta(days=1) instead of idx + pd.Timestamp(...).","If you meant to broadcast a base timestamp, compute (idx - base_ts) to get a TimedeltaIndex, or use Timestamp arithmetic elementwise.","Subtract the datetime scalar from each element explicitly via idx.__sub__ if a timedelta result was intended.","Check idx.dtype.kind before the op: timedelta ('m') supports datetime addition, datetime ('M') and Period do not."],"exampleFix":"// before\nidx = pd.date_range('2020-01-01', periods=3)\nout = idx + pd.Timestamp('2020-01-01')  # TypeError: cannot add DatetimeArray and Timestamp\n// after\nout = idx + pd.Timedelta(days=1)","handlingStrategy":"type-guard","validationCode":"from pandas.api.types import is_timedelta64_dtype\nif not is_timedelta64_dtype(idx):\n    # adding a datetime scalar is invalid; convert to Timedelta\n    other = pd.Timedelta(days=1)\nout = idx + other","typeGuard":"def accepts_datetime_addition(idx) -> bool:\n    return getattr(idx.dtype, 'kind', None) == 'm'  # only timedelta dtype","tryCatchPattern":"try:\n    out = idx + ts\nexcept TypeError as e:\n    if 'cannot add' in str(e) and 'Timestamp' in str(e):\n        out = idx + pd.Timedelta(ts - pd.Timestamp(0))\n    else:\n        raise","preventionTips":["Reserve datetime-scalar addition for TimedeltaIndex only.","Use pd.Timedelta(...) rather than pd.Timestamp(...) as the additive operand.","Check idx.dtype.kind == 'm' before datetime addition."],"tags":["addition","datetime","timedelta","type-mismatch"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}