{"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/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/datetimelike.py#L1029-L1065","documentation":"Raised by _add_datetimelike_scalar when adding a datetimelike scalar (Timestamp / np.datetime64) to an array whose dtype is not timedelta64 (kind != 'm'). Only TimedeltaArray + datetime yields a meaningful DatetimeArray; adding a datetime to a DatetimeArray or PeriodArray is undefined, so pandas refuses.","triggerScenarios":"DatetimeArray(...) + Timestamp(...); PeriodArray + np.datetime64('...'); any attempt to add a datetime scalar to a non-timedelta datetimelike array via __add__ dispatch.","commonSituations":"Mis-arithmetic in date math: trying to 'shift' a DatetimeIndex by adding a Timestamp instead of a Timedelta; off-by-type bugs in scheduling code that confuses points and durations.","solutions":["If you meant to shift datetimes by a duration, add a Timedelta / timedelta64 / TimedeltaIndex instead of a Timestamp.","If you meant to add a duration to a Period index, add a Timedelta or use Period arithmetic appropriate to the freq.","Re-check the type of `other` before the operation; the message reports both type(self) and type(other)."],"exampleFix":"// before\nres = dta + pd.Timestamp('2020-01-01')  # TypeError\n\n// after\nres = dta + pd.Timedelta(days=1)","handlingStrategy":"type-guard","validationCode":"import pandas as pd\n\ndef shift_datetime_array(dta, other):\n    if isinstance(other, (pd.Timedelta, pd.TimedeltaIndex,)) or pd.api.types.is_timedelta64_dtype(getattr(other, 'dtype', None)):\n        return dta + other\n    raise TypeError('add a Timedelta, not a Timestamp, to a DatetimeArray')","typeGuard":"import pandas as pd\n\ndef is_datetimelike_scalar(v) -> bool:\n    return isinstance(v, (pd.Timestamp,)) or hasattr(v, 'dtype') and pd.api.types.is_datetime64_dtype(v.dtype)\n\ndef is_duration_operand(other) -> bool:\n    return isinstance(other, (pd.Timedelta,)) or hasattr(other, 'dtype') and pd.api.types.is_timedelta64_dtype(other.dtype)","tryCatchPattern":"try:\n    res = arr + other\nexcept TypeError as e:\n    if 'cannot add' in str(e) and 'Datetime' in str(e):\n        res = arr + pd.Timedelta(other)  # only if conversion is meaningful\n    else:\n        raise","preventionTips":["Distinguish points (Timestamp/datetime64) from durations (Timedelta/timedelta64) in date arithmetic.","Use TimedeltaIndex/Timedelta to shift datetimes; never add two datetimes.","Unit-test arithmetic helpers with both datetime and timedelta operands."],"tags":["datetime","arithmetic","type-error","timedelta"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}