pandas-dev/pandas · error · TypeError
datetime64 type does not support operation '{how}'
Error message
datetime64 type does not support operation '{how}' What it means
Raised by _groupby_op when the grouped array is datetime64 and the requested how is one of sum/prod/cumsum/cumprod/var/skew/kurt. Summing or multiplying absolute timestamps is not meaningful, so pandas rejects these reductions on groupby dispatch.
Source
Thrown at pandas/core/arrays/datetimelike.py:1624
# ------------------------------------------------------------------
# GroupBy Methods
def _groupby_op(
self,
*,
how: str,
has_dropped_na: bool,
min_count: int,
ngroups: int,
ids: npt.NDArray[np.intp],
**kwargs,
):
dtype = self.dtype
if dtype.kind == "M":
# Adding/multiplying datetimes is not valid
if how in ["sum", "prod", "cumsum", "cumprod", "var", "skew", "kurt"]:
raise TypeError(f"datetime64 type does not support operation '{how}'")
if how in ["any", "all"]:
# 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(0, freq)).{how}() instead."
)
# timedeltas we can add but not multiplyView on GitHub (pinned to 71959b8cb9)
Solutions
- Aggregate a numeric column instead, or convert the datetime column: use .min()/.max()/.median()/.count() which are supported.
- Compute a duration by subtracting a baseline first: (df['ts'] - df['ts'].min()).groupby(key).sum().
- Drop datetime columns before calling generic .sum()/.prod() reductions.
- Use .agg() with a function allow-list that excludes sum/prod for datetime dtypes.
Example fix
// before
df.groupby('key')['timestamp'].sum() # TypeError: datetime64 type does not support operation 'sum'
// after
df.groupby('key')['timestamp'].agg(['min','max','count']) Defensive patterns
Strategy: type-guard
Validate before calling
from pandas.api.types import is_datetime64_any_dtype
BAD = {'sum','prod','cumsum','cumprod','var','skew','kurt'}
if is_datetime64_any_dtype(col.dtype) and how in BAD:
how = 'min' # or skip the column Type guard
def supports_groupby_op(col, how: str) -> bool:
from pandas.api.types import is_datetime64_any_dtype
BAD = {'sum','prod','cumsum','cumprod','var','skew','kurt'}
return not (is_datetime64_any_dtype(col.dtype) and how in BAD) Try / catch
try:
out = df.groupby('key')['ts'].agg(how)
except TypeError as e:
if 'datetime64 type does not support' in str(e):
out = df.groupby('key')['ts'].agg(['min','max','count'])
else:
raise Prevention
- Exclude datetime columns from sum/prod/cumsum/var/skew/kurt aggregations.
- Use min/max/median/count for datetime reductions.
- Subtract a baseline first if a duration sum is needed.
When it happens
Trigger: df.groupby(key)[ts_col].sum(), .prod(), .cumsum(), .var(), .skew(), .kurt() on a datetime64 column; reached via the branch at line 1621-1624.
Common situations: df.describe() or df.groupby().agg(['sum','mean']) applied blindly to datetime columns; pipelines that aggregate every numeric-looking column including timestamps.
Related errors
- Period type does not support {how} operations
- Accumulation {name} not supported for {type(self)}
- '{how}' with datetime64 dtypes is no longer supported. Use (
- timedelta64 type does not support {how} operations
- 'std' and 'sem' are not valid for PeriodDtype
AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07).
Data as JSON: /api/errors/8998255087089b9b.
Report an issue: GitHub.