pandas-dev/pandas · error · TypeError

'{how}' with PeriodDtype is no longer supported. Use (obj !=

Error message

'{how}' with PeriodDtype is no longer supported. Use (obj != pd.Period(0, freq)).{how}() instead.

What it means

Raised by _groupby_op when grouping a PeriodDtype column with how in {'any','all'}. Mirrors error 257 for Period dtypes: the implicit truthiness comparison was removed (GH#34479) and the user must materialise the boolean mask explicitly via comparison against pd.Period(0, freq).

Source

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

        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 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,

View on GitHub (pinned to 71959b8cb9)

Solutions

  1. Materialise the mask first: (df['p'] != pd.Period(0, df['p'].dt.freq)).groupby(df['key']).any().
  2. Test for non-NaT instead: df['p'].notna().groupby(df['key']).all().
  3. Drop the Period column from any/all aggregations.
  4. Pin pandas version if you depend on the legacy implicit truthiness, while planning migration.

Example fix

// before
df.groupby('key')['period_col'].any()  # TypeError: 'any' with PeriodDtype no longer supported
// after
(df['period_col'].notna()).groupby(df['key']).any()
Defensive patterns

Strategy: type-guard

Validate before calling

from pandas.api.types import is_period_dtype
if is_period_dtype(col.dtype) and how in {'any','all'}:
    series = col.notna()
else:
    series = col
out = series.groupby(df['key']).agg(how)

Type guard

def needs_explicit_mask(col, how: str) -> bool:
    from pandas.api.types import is_period_dtype
    return is_period_dtype(col.dtype) and how in {'any','all'}

Try / catch

try:
    out = df.groupby('key')['p'].any()
except TypeError as e:
    if 'no longer supported' in str(e) and 'PeriodDtype' in str(e):
        out = df['p'].notna().groupby(df['key']).any()
    else:
        raise

Prevention

When it happens

Trigger: df.groupby(key)[period_col].any() or .all(); reached via the branch at line 1636-1641.

Common situations: Post-upgrade behavior change; generic 'reduce every column' code paths; mixing boolean reductions across heterogeneous dtypes.

Related errors


AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07). Data as JSON: /api/errors/6886a8ac4bcde4b3. Report an issue: GitHub.