pandas-dev/pandas · error · TypeError

timedelta64 type does not support

Error message

timedelta64 type does not support {how} operations

What it means

Raised in _groupby_op on the timedelta path when how is one of prod, cumprod, skew, kurt, var. Timedeltas support addition and summing, but multiplication (prod/cumprod) and higher moments (var/skew/kurt) are not defined over duration quantities, so pandas rejects them at the groupby dispatch layer.

Solutions

  1. If you want numeric moments, convert to a numeric unit first: s.astype('int64').groupby(g).var() and reinterpret as a timedelta where meaningful.
  2. For sums use .sum()/.cumsum(), which ARE supported for timedeltas.
  3. Drop the timedelta dtype (e.g. .dt.total_seconds()) before applying prod/var/skew/kurt.

Example fix

// before
s = pd.Series(pd.to_timedelta(['1 day','2 days']))
s.prod()  # TypeError: timedelta64 type does not support prod operations

// after
s.dt.total_seconds().prod()  # numeric product on seconds
Defensive patterns

Strategy: validation

Validate before calling

TD_UNSUPPORTED = {"prod","cumprod","skew","kurt","var"}
def assert_td_reduction(s, how):
    if pd.api.types.is_timedelta64_dtype(s) and how in TD_UNSUPPORTED:
        raise ValueError(f"timedelta64 does not support {how}; cast to seconds first")

Type guard

def is_td_unsupported_reduction(dtype, how) -> bool:
    return getattr(dtype, "kind", None) == "m" and how in {"prod","cumprod","skew","kurt","var"}

Try / catch

try:
    out = s.groupby(g).prod()
except TypeError as e:
    if "timedelta64 type does not support" in str(e):
        out = s.dt.total_seconds().groupby(g).prod()
    else:
        raise

Prevention

When it happens

Trigger: groupby(...).prod()/.cumprod()/.var()/.skew()/.kurt() on a timedelta64 Series or TimedeltaIndex; rolling/resample aggregations that map onto those how strings for timedelta values.

Common situations: Treating durations as raw integers and assuming .var() works; generic 'describe all numeric columns' pipelines that hit a timedelta column; migrating from object-stored durations to timedelta64 dtype.

Related errors


AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11). Data as JSON: /api/errors/dc1cdb49dcfaf2b1. Report an issue: GitHub.

Appendix: source

Thrown at pandas/core/arrays/datetimelike.py:1653

                # GH#34479
                raise TypeError(
                    f"'{how}' with datetime64 dtypes is no longer supported. "
                    f"Use (obj != pd.Timestamp(0)).{how}() instead."
                )

        elif isinstance(dtype, PeriodDtype):
            # Adding/multiplying Periods is not valid
            if how in ["sum", "prod", "cumsum", "cumprod", "var", "skew", "kurt"]:
                raise TypeError(f"Period type does not support {how} operations")
            if how in ["any", "all"]:
                # GH#34479
                raise TypeError(
                    f"'{how}' with PeriodDtype is no longer supported. "
                    f"Use (obj != pd.Period(ordinal=0, freq=freq)).{how}() instead."
                )
        # timedeltas we can add but not multiply
        elif how in ["prod", "cumprod", "skew", "kurt", "var"]:
            raise TypeError(f"timedelta64 type does not support {how} operations")

        # All of the functions implemented here are ordinal, so we can
        #  operate on the tz-naive equivalents
        npvalues = self._ndarray.view("M8[ns]")

        from pandas.core.groupby.ops import WrappedCythonOp

        kind = WrappedCythonOp.get_kind_from_how(how)
        op = WrappedCythonOp(how=how, kind=kind, has_dropped_na=has_dropped_na)

        res_values = op._cython_op_ndim_compat(
            npvalues,
            min_count=min_count,
            ngroups=ngroups,
            comp_ids=ids,
            mask=None,
            **kwargs,
        )

View on GitHub (pinned to 3b7651241d)