pandas-dev/pandas · error · TypeError
Period type does not support
Error message
Period type does not support {how} operations What it means
Raised inside DatetimeLikeArrayMixin._groupby_op when the underlying dtype is a PeriodDtype and the requested reduction 'how' is one of sum, prod, cumsum, cumprod, var, skew, or kurt. Periods represent ordinal positions on a fixed calendar grid; summing or multiplying them (or computing moments like variance/skew) has no defined meaning, so pandas refuses rather than producing a misleading numeric result.
Solutions
- Cast the Period column to a timestamp with .dt.to_timestamp() before the aggregation if you actually want time arithmetic.
- If you need a count-like reduction, use size() or count() instead of sum().
- Convert the Period to its ordinal integer via .astype('int64') (view of the underlying ordinal) if you intentionally want raw ordinal arithmetic.
- Drop the Period dtype (e.g. .astype(str) or .dt.to_timestamp()) before applying sum/prod/etc.
Example fix
// before s = pd.Series(pd.PeriodIndex(['2020-01','2020-02'], freq='M'), name='p') s.sum() # TypeError: Period type does not support sum operations // after s.dt.to_timestamp().sum() # or s.groupby(key).size() for counts
Defensive patterns
Strategy: validation
Validate before calling
def safe_period_groupby(s, how):
if pd.api.types.is_period_dtype(s):
if how in {"sum","prod","cumsum","cumprod","var","skew","kurt"}:
raise ValueError(f"{how} is unsupported on Period; convert to timestamp first")
return s.groupby(level=0) if isinstance(s, pd.Series) else s Type guard
def is_period_reduction_unsupported(dtype, how) -> bool:
return isinstance(dtype, pd.PeriodDtype) and how in {"sum","prod","cumsum","cumprod","var","skew","kurt"} Try / catch
try:
result = s.groupby(g).sum()
except TypeError as e:
if "Period type does not support" in str(e):
result = s.dt.to_timestamp().groupby(g).sum()
else:
raise Prevention
- Centralize groupby aggregations behind a helper that branches on dtype.kind / PeriodDtype.
- Document Period columns explicitly in schemas so consumers know not to sum them.
- Add unit tests that assert a TypeError for unsupported period reductions.
When it happens
Trigger: Calling groupby(...).sum()/.prod()/.cumsum()/.var()/.skew()/.kurt() (or DataFrame/Series reductions routed through the cython groupby engine) on a Series whose dtype is period[...] or a PeriodIndex; equivalently resample/rolling aggregations that map onto those how values over Period data.
Common situations: Loading monthly/quarterly data as Period columns and then running describe-style aggregations; migrating code from object/integer encodings to Period dtype and forgetting to strip it before summing; aggregating Period columns inside a groupby pipeline.
Related errors
- 'std' and 'sem' are not valid for PeriodDtype
- timedelta64 type does not support
- Cannot perform reduction
- Cannot perform reduction
- dtype is not specified and cannot be inferred
AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11).
Data as JSON: /api/errors/a7663c5c7ce63cd7.
Report an issue: GitHub.
Appendix: source
Thrown at pandas/core/arrays/datetimelike.py:1644
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(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)View on GitHub (pinned to 3b7651241d)