{"record":{"id":"dc1cdb49dcfaf2b1","repo":"pandas-dev/pandas","slug":"timedelta64-type-does-not-support-how-operations","errorCode":null,"errorMessage":"timedelta64 type does not support {how} operations","messagePattern":"timedelta64 type does not support (.+?) operations","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/datetimelike.py","lineNumber":1653,"sourceCode":"                # GH#34479\n                raise TypeError(\n                    f\"'{how}' with datetime64 dtypes is no longer supported. \"\n                    f\"Use (obj != pd.Timestamp(0)).{how}() instead.\"\n                )\n\n        elif isinstance(dtype, PeriodDtype):\n            # Adding/multiplying Periods is not valid\n            if how in [\"sum\", \"prod\", \"cumsum\", \"cumprod\", \"var\", \"skew\", \"kurt\"]:\n                raise TypeError(f\"Period type does not support {how} operations\")\n            if how in [\"any\", \"all\"]:\n                # GH#34479\n                raise TypeError(\n                    f\"'{how}' with PeriodDtype is no longer supported. \"\n                    f\"Use (obj != pd.Period(ordinal=0, freq=freq)).{how}() instead.\"\n                )\n        # timedeltas we can add but not multiply\n        elif how in [\"prod\", \"cumprod\", \"skew\", \"kurt\", \"var\"]:\n            raise TypeError(f\"timedelta64 type does not support {how} operations\")\n\n        # All of the functions implemented here are ordinal, so we can\n        #  operate on the tz-naive equivalents\n        npvalues = self._ndarray.view(\"M8[ns]\")\n\n        from pandas.core.groupby.ops import WrappedCythonOp\n\n        kind = WrappedCythonOp.get_kind_from_how(how)\n        op = WrappedCythonOp(how=how, kind=kind, has_dropped_na=has_dropped_na)\n\n        res_values = op._cython_op_ndim_compat(\n            npvalues,\n            min_count=min_count,\n            ngroups=ngroups,\n            comp_ids=ids,\n            mask=None,\n            **kwargs,\n        )","sourceCodeStart":1635,"sourceCodeEnd":1671,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/datetimelike.py#L1635-L1671","documentation":"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.","triggerScenarios":"groupby(...).prod()/.cumprod()/.var()/.skew()/.kurt() on a timedelta64 Series or TimedeltaIndex; rolling/resample aggregations that map onto those how strings for timedelta values.","commonSituations":"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.","solutions":["If you want numeric moments, convert to a numeric unit first: s.astype('int64').groupby(g).var() and reinterpret as a timedelta where meaningful.","For sums use .sum()/.cumsum(), which ARE supported for timedeltas.","Drop the timedelta dtype (e.g. .dt.total_seconds()) before applying prod/var/skew/kurt."],"exampleFix":"// before\ns = pd.Series(pd.to_timedelta(['1 day','2 days']))\ns.prod()  # TypeError: timedelta64 type does not support prod operations\n\n// after\ns.dt.total_seconds().prod()  # numeric product on seconds","handlingStrategy":"validation","validationCode":"TD_UNSUPPORTED = {\"prod\",\"cumprod\",\"skew\",\"kurt\",\"var\"}\ndef assert_td_reduction(s, how):\n    if pd.api.types.is_timedelta64_dtype(s) and how in TD_UNSUPPORTED:\n        raise ValueError(f\"timedelta64 does not support {how}; cast to seconds first\")","typeGuard":"def is_td_unsupported_reduction(dtype, how) -> bool:\n    return getattr(dtype, \"kind\", None) == \"m\" and how in {\"prod\",\"cumprod\",\"skew\",\"kurt\",\"var\"}","tryCatchPattern":"try:\n    out = s.groupby(g).prod()\nexcept TypeError as e:\n    if \"timedelta64 type does not support\" in str(e):\n        out = s.dt.total_seconds().groupby(g).prod()\n    else:\n        raise","preventionTips":["Convert durations to a numeric unit (total_seconds) before multiplicative/stat reductions.","Keep a registry of which reductions are valid per dtype-kind in shared utils.","Add dtype-aware test fixtures so timedelta columns surface these errors early."],"tags":["pandas","timedelta","groupby","dtype","reduction"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}