pandas-dev/pandas · error · TypeError

'{how}' with datetime64 dtypes is no longer supported. Use (

Error message

'{how}' with datetime64 dtypes is no longer supported. Use (obj != pd.Timestamp(0)).{how}() instead.

What it means

Raised by _groupby_op when grouping a datetime64 column with how in {'any','all'}. Since GH#34479 these boolean reductions on datetime64 are no longer supported because the implicit 'datetime != 0' comparison was confusing; the message tells you to compute the boolean mask explicitly.

Source

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

    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 multiply
        elif how in ["prod", "cumprod", "skew", "kurt", "var"]:
            raise TypeError(f"timedelta64 type does not support {how} operations")

View on GitHub (pinned to 71959b8cb9)

Solutions

  1. Materialize the boolean mask first: (df['ts'] != pd.Timestamp(0)).groupby(key).any().
  2. Or test for non-NaT directly: df['ts'].notna().groupby(key).all().
  3. Drop the datetime column from any/all aggregations.
  4. Pin the pandas version if you rely on legacy behavior, and schedule a migration.

Example fix

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

Strategy: type-guard

Validate before calling

from pandas.api.types import is_datetime64_any_dtype
if is_datetime64_any_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_datetime64_any_dtype
    return is_datetime64_any_dtype(col.dtype) and how in {'any','all'}

Try / catch

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

Prevention

When it happens

Trigger: df.groupby(key)[ts_col].any() or .all() on a datetime64 column; reached via the branch at line 1625-1630.

Common situations: Behavioral change upgrading across pandas versions that dropped implicit datetime truthiness; generic 'aggregate everything' code that runs any/all over all columns.

Related errors


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