{"record":{"id":"20759286d1cd2afa","repo":"pandas-dev/pandas","slug":"cannot-add-period-to-a-type-self-name","errorCode":null,"errorMessage":"cannot add Period to a {type(self).__name__}","messagePattern":"cannot add Period to a (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/datetimelike.py","lineNumber":1140,"sourceCode":"\n        from pandas.core.arrays import TimedeltaArray\n\n        try:\n            self._assert_tzawareness_compat(other)\n        except TypeError as err:\n            new_message = str(err).replace(\"compare\", \"subtract\")\n            raise type(err)(new_message) from err\n\n        other_i8, o_mask = self._get_i8_values_and_mask(other)\n        res_values = add_overflowsafe(self.asi8, np.asarray(-other_i8, dtype=\"i8\"))\n        res_m8 = res_values.view(f\"timedelta64[{self.unit}]\")\n\n        return TimedeltaArray._simple_new(res_m8, dtype=res_m8.dtype)\n\n    @final\n    def _add_period(self, other: Period) -> PeriodArray:\n        if not lib.is_np_dtype(self.dtype, \"m\"):\n            raise TypeError(f\"cannot add Period to a {type(self).__name__}\")\n\n        # We will wrap in a PeriodArray and defer to the reversed operation\n        from pandas.core.arrays.period import PeriodArray\n\n        i8vals = np.broadcast_to(other.ordinal, self.shape)\n        dtype = PeriodDtype(other.freq)\n        parr = PeriodArray(i8vals, dtype=dtype)\n        return parr + self\n\n    def _add_offset(self, offset):\n        raise AbstractMethodError(self)\n\n    def _add_timedeltalike_scalar(self, other):\n        \"\"\"\n        Add a delta of a timedeltalike\n\n        Returns\n        -------","sourceCodeStart":1122,"sourceCodeEnd":1158,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/datetimelike.py#L1122-L1158","documentation":"Raised by _add_period when a Period scalar is added to an array whose dtype is not timedelta (kind != 'm'). Only TimedeltaArray + Period is defined (yielding PeriodArray); DatetimeArray + Period and PeriodArray + Period are rejected.","triggerScenarios":"DatetimeIndex + pd.Period(...) or PeriodIndex + pd.Period(...), dispatched via __add__ line 1326-1327 (only entered when self is timedelta dtype) — for non-timedelta, the call still reaches _add_period through other paths and fails at line 1139.","commonSituations":"Mixing Period and datetime columns in arithmetic; treating a Period like a DateOffset.","solutions":["Convert the Period to a Timedelta via its freq if you meant a duration: pd.tseries.frequencies.to_offset(period.freq) * period.","Add a DateOffset (e.g. pd.DateOffset(months=1)) instead of a Period to a DatetimeIndex.","For PeriodIndex, shift via idx + n (integer) or idx.shift(n) instead of adding a Period.","Verify self.dtype.kind == 'm' before adding a Period."],"exampleFix":"// before\nout = datetime_idx + pd.Period('2020-01', 'M')  # TypeError\n// after\nout = datetime_idx + pd.DateOffset(months=1)","handlingStrategy":"type-guard","validationCode":"if idx.dtype.kind != 'm':\n    # cannot add Period; convert to DateOffset\n    other = pd.DateOffset(months=1)\nout = idx + other","typeGuard":"def accepts_period_addition(idx) -> bool:\n    return getattr(idx.dtype, 'kind', None) == 'm'","tryCatchPattern":"try:\n    out = idx + period\nexcept TypeError as e:\n    if 'cannot add Period' in str(e):\n        out = idx + pd.DateOffset(months=period.n)\n    else:\n        raise","preventionTips":["Use DateOffset, not Period, when adding to a DatetimeIndex.","For PeriodIndex use integer shifts (idx + n).","Check dtype.kind == 'm' before adding a Period."],"tags":["addition","period","datetime","type-mismatch"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}