pandas-dev/pandas · error · TypeError
Cannot add or subtract timedelta64[ns] dtype from {self.dtyp
Error message
Cannot add or subtract timedelta64[ns] dtype from {self.dtype} What it means
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.
Source
Thrown at pandas/core/arrays/period.py:1258
else:
td = np.asarray(Timedelta(other).asm8)
return self._add_timedelta_arraylike(td)
def _add_timedelta_arraylike(
self, other: TimedeltaArray | npt.NDArray[np.timedelta64]
) -> Self:
"""
Parameters
----------
other : TimedeltaArray or ndarray[timedelta64]
Returns
-------
PeriodArray
"""
if not self.dtype._is_tick_like():
# We cannot add timedelta-like to non-tick PeriodArray
raise TypeError(
f"Cannot add or subtract timedelta64[ns] dtype from {self.dtype}"
)
dtype = np.dtype(f"m8[{self.dtype._td64_unit}]")
# Similar to _check_timedeltalike_freq_compat, but we raise with a
# more specific exception message if necessary.
try:
delta = astype_overflowsafe(
np.asarray(other), dtype=dtype, copy=False, round_ok=False
)
except ValueError as err:
# e.g. if we have minutes freq and try to add 30s
# "Cannot losslessly convert units"
raise IncompatibleFrequency(
"Cannot add/subtract timedelta-like from PeriodArray that is "
"not an integer multiple of the PeriodArray's freq."
) from errView on GitHub (pinned to 71959b8cb9)
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.
Example fix
# before
pa = pd.period_range('2020-01','2020-03', freq='M')._data
pa + pd.Timedelta(days=1)
# after
pa.asfreq('D') + pd.Timedelta(days=1)
# or shift by whole periods
pa + 1 # shifts one month Defensive patterns
Strategy: type-guard
Validate before calling
import pandas as pd
from pandas._libs.tslibs.offsets import Tick
def can_add_timedelta(pa) -> bool:
return isinstance(pa.freq, (Tick,)) or pa.freq.rule_code == 'D' Type guard
from pandas._libs.tslibs.offsets import Tick
def is_tick_freq(pa) -> bool:
return pa.dtype._is_tick_like() Try / catch
try:
out = pa + td
except TypeError:
out = pa.asfreq('D') + td Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Cannot add/subtract timedelta-like from PeriodArray that is
- dtype is not specified and cannot be inferred
- Not supported to convert PeriodArray to array with different
- specified freq and dtype are different
- cannot subtract {type(self).__name__} from {other.dtype}
AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07).
Data as JSON: /api/errors/ebf3801ff4760837.
Report an issue: GitHub.