{"record":{"id":"ebf3801ff4760837","repo":"pandas-dev/pandas","slug":"cannot-add-or-subtract-timedelta64-ns-dtype-from","errorCode":null,"errorMessage":"Cannot add or subtract timedelta64[ns] dtype from {self.dtype}","messagePattern":"Cannot add or subtract timedelta64\\[ns\\] dtype from (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/period.py","lineNumber":1258,"sourceCode":"        else:\n            td = np.asarray(Timedelta(other).asm8)\n        return self._add_timedelta_arraylike(td)\n\n    def _add_timedelta_arraylike(\n        self, other: TimedeltaArray | npt.NDArray[np.timedelta64]\n    ) -> Self:\n        \"\"\"\n        Parameters\n        ----------\n        other : TimedeltaArray or ndarray[timedelta64]\n\n        Returns\n        -------\n        PeriodArray\n        \"\"\"\n        if not self.dtype._is_tick_like():\n            # We cannot add timedelta-like to non-tick PeriodArray\n            raise TypeError(\n                f\"Cannot add or subtract timedelta64[ns] dtype from {self.dtype}\"\n            )\n\n        dtype = np.dtype(f\"m8[{self.dtype._td64_unit}]\")\n\n        # Similar to _check_timedeltalike_freq_compat, but we raise with a\n        #  more specific exception message if necessary.\n        try:\n            delta = astype_overflowsafe(\n                np.asarray(other), dtype=dtype, copy=False, round_ok=False\n            )\n        except ValueError as err:\n            # e.g. if we have minutes freq and try to add 30s\n            # \"Cannot losslessly convert units\"\n            raise IncompatibleFrequency(\n                \"Cannot add/subtract timedelta-like from PeriodArray that is \"\n                \"not an integer multiple of the PeriodArray's freq.\"\n            ) from err","sourceCodeStart":1240,"sourceCodeEnd":1276,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/period.py#L1240-L1276","documentation":"Raised by PeriodArray._add_timedelta_arraylike when self.dtype._is_tick_like() is False. Adding/subtracting a timedelta only makes sense for period arrays whose freq is a Tick (ns, us, ms, s, min, h, D); non-tick freqs (M, Q, Y, W) have variable-length periods, so timedelta arithmetic is undefined and rejected.","triggerScenarios":"period_range(..., freq='M') + pd.Timedelta(days=1); a Series of period[M] plus a timedelta; vectorized period[Tick] arithmetic where the freq slipped to a non-tick.","commonSituations":"Monthly/quarterly/annual period columns where users try timedelta math. Mixing datetime arithmetic idioms with period data. Aggregations that change the freq to month then apply offset shifts.","solutions":["Use asfreq to a tick freq first, then add: pa.asfreq('D') + timedelta.","Add Period-freq multiples: pa + n * pa.freq (integer multiples of the period).","Convert to timestamps: pa.to_timestamp() + timedelta for wall-clock arithmetic."],"exampleFix":"# before\npa = pd.period_range('2020-01','2020-03', freq='M')._data\npa + pd.Timedelta(days=1)\n# after\npa.asfreq('D') + pd.Timedelta(days=1)\n# or shift by whole periods\npa + 1  # shifts one month","handlingStrategy":"type-guard","validationCode":"import pandas as pd\nfrom pandas._libs.tslibs.offsets import Tick\n\ndef can_add_timedelta(pa) -> bool:\n    return isinstance(pa.freq, (Tick,)) or pa.freq.rule_code == 'D'","typeGuard":"from pandas._libs.tslibs.offsets import Tick\n\ndef is_tick_freq(pa) -> bool:\n    return pa.dtype._is_tick_like()","tryCatchPattern":"try:\n    out = pa + td\nexcept TypeError:\n    out = pa.asfreq('D') + td","preventionTips":["Check pa.dtype._is_tick_like() before timedelta arithmetic.","Use integer-multiple shifts (pa + n) for non-tick period arrays.","Convert to timestamps for wall-clock timedelta math."],"tags":["period","arithmetic","timedelta","freq","pandas-arrays"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}