pandas-dev/pandas · error · TypeError

' ' with PeriodDtype is no longer supported. Use (obj !=…

Error message

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

What it means

Raised in _groupby_op for PeriodDtype when how is 'any' or 'all' (GH#34479). Prior to the fix these fell through to a numeric path; the message now redirects users to a boolean test against a zero-ordinal Period, because 'any'/'all' over Periods are conceptually truthiness checks, not reductions the groupby engine should perform directly.

Solutions

  1. Replace s.groupby(g).any() with (s != pd.Period(ordinal=0, freq=s.dtype.freq)).groupby(g).any().
  2. If you only need 'are there non-NaT values', use s.notna().groupby(g).any() instead.
  3. Drop the Period dtype before the call if the truthiness semantics are not what you want.

Example fix

// before
s = pd.Series(pd.PeriodIndex(['2020-01','2020-02',None], freq='M'))
s.groupby([0,0,0]).any()  # TypeError: 'any' with PeriodDtype is no longer supported

// after
(s != pd.Period(ordinal=0, freq='M')).groupby([0,0,0]).any()
Defensive patterns

Strategy: validation

Validate before calling

def period_any(s, group):
    if isinstance(s.dtype, pd.PeriodDtype):
        return (s != pd.Period(ordinal=0, freq=s.dtype.freq)).groupby(group).any()
    return s.groupby(group).any()

Type guard

def needs_period_any_redirect(dtype, how) -> bool:
    return isinstance(dtype, pd.PeriodDtype) and how in {"any","all"}

Try / catch

try:
    out = s.groupby(g).any()
except TypeError as e:
    if "PeriodDtype is no longer supported" in str(e):
        out = (s != pd.Period(ordinal=0, freq=s.dtype.freq)).groupby(g).any()
    else:
        raise

Prevention

When it happens

Trigger: groupby(...).any() or .all() on a Series/Index with PeriodDtype; the same call routed through resample/rolling when the underlying values are Periods.

Common situations: Reusing a generic 'are there any non-empty values' reduction (.any()) on a Period column after a schema change; pipelines that call .any()/.all() polymorphically across mixed-dtype columns; pandas 1.1+ migrations where old behavior quietly succeeded.

Related errors


AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11). Data as JSON: /api/errors/88c29af1c6c45089. Report an issue: GitHub.

Appendix: source

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

        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)

        res_values = op._cython_op_ndim_compat(
            npvalues,

View on GitHub (pinned to 3b7651241d)