{"record":{"id":"a7663c5c7ce63cd7","repo":"pandas-dev/pandas","slug":"period-type-does-not-support-how-operations","errorCode":null,"errorMessage":"Period type does not support {how} operations","messagePattern":"Period type does not support (.+?) operations","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/datetimelike.py","lineNumber":1635,"sourceCode":"        ids: npt.NDArray[np.intp],\n        **kwargs,\n    ):\n        dtype = self.dtype\n        if dtype.kind == \"M\":\n            # Adding/multiplying datetimes is not valid\n            if how in [\"sum\", \"prod\", \"cumsum\", \"cumprod\", \"var\", \"skew\", \"kurt\"]:\n                raise TypeError(f\"datetime64 type does not support operation '{how}'\")\n            if how in [\"any\", \"all\"]:\n                # 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(0, 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)","sourceCodeStart":1617,"sourceCodeEnd":1653,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/datetimelike.py#L1617-L1653","documentation":"Raised by _groupby_op when grouping a PeriodDtype column with how in {sum/prod/cumsum/cumprod/var/skew/kurt}. As with datetime64 (error 256) these reductions are not meaningful on absolute Period values and are rejected.","triggerScenarios":"df.groupby(key)[period_col].sum(), .prod(), .cumsum(), .var(), .skew(), .kurt(); reached via the branch at line 1632-1635.","commonSituations":"Generic aggregation pipelines run over Period columns; reporting code that summarises every column.","solutions":["Convert the Period column to timestamp before reducing: df['p'].dt.to_timestamp(how='start').groupby(key).mean().","Use supported reductions: .min/.max/.count/.median.","Aggregate the ordinal: df['p'].view('i8').groupby(key).sum().","Exclude Period columns from sum/prod-style aggregations."],"exampleFix":"// before\ndf.groupby('key')['period_col'].sum()  # TypeError: Period type does not support sum operations\n// after\ndf.groupby('key')['period_col'].agg(['min','max','count'])","handlingStrategy":"type-guard","validationCode":"from pandas.api.types import is_period_dtype\nBAD = {'sum','prod','cumsum','cumprod','var','skew','kurt'}\nif is_period_dtype(col.dtype) and how in BAD:\n    how = 'min'  # or skip the column","typeGuard":"def supports_groupby_op(col, how: str) -> bool:\n    from pandas.api.types import is_period_dtype\n    BAD = {'sum','prod','cumsum','cumprod','var','skew','kurt'}\n    return not (is_period_dtype(col.dtype) and how in BAD)","tryCatchPattern":"try:\n    out = df.groupby('key')['p'].agg(how)\nexcept TypeError as e:\n    if 'Period type does not support' in str(e):\n        out = df.groupby('key')['p'].agg(['min','max','count'])\n    else:\n        raise","preventionTips":["Exclude Period columns from sum/prod/cumsum/var/skew/kurt aggregations.","Use min/max/median/count for Period reductions, or convert to timestamp.","Aggregate ordinals explicitly if a numeric result is required."],"tags":["groupby","reduction","period","unsupported-op"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}