{"record":{"id":"a0eff1096f884352","repo":"pandas-dev/pandas","slug":"numpy-operations-are-not-valid-with-groupby-use","errorCode":null,"errorMessage":"numpy operations are not valid with groupby. Use .groupby(...).{name}() instead","messagePattern":"numpy operations are not valid with groupby\\. Use \\.groupby\\(\\.\\.\\.\\)\\.(.+?)\\(\\) instead","errorType":"exception","errorClass":"UnsupportedFunctionCall","httpStatus":null,"severity":"error","filePath":"pandas/compat/numpy/function.py","lineNumber":340,"sourceCode":"\ndef validate_groupby_func(\n    name: str,\n    args: tuple[Any, ...],\n    kwargs: dict[str, Any],\n    allowed: list[str] | None = None,\n) -> None:\n    \"\"\"\n    'args' and 'kwargs' should be empty, except for allowed kwargs because all\n    of their necessary parameters are explicitly listed in the function\n    signature\n    \"\"\"\n    if allowed is None:\n        allowed = []\n\n    extra_kwargs = set(kwargs) - set(allowed)\n\n    if len(args) + len(extra_kwargs) > 0:\n        raise UnsupportedFunctionCall(\n            \"numpy operations are not valid with groupby. \"\n            f\"Use .groupby(...).{name}() instead\"\n        )\n\n\ndef validate_minmax_axis(axis: AxisInt | None, ndim: int = 1) -> None:\n    \"\"\"\n    Ensure that the axis argument passed to min, max, argmin, or argmax is zero\n    or None, as otherwise it will be incorrectly ignored.\n\n    Parameters\n    ----------\n    axis : int or None\n    ndim : int, default 1\n\n    Raises\n    ------\n    ValueError","sourceCodeStart":322,"sourceCodeEnd":358,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/compat/numpy/function.py#L322-L358","documentation":"Raised by `validate_args_and_kwargs` (in pandas.compat.numpy.function) as an `UnsupportedFunctionCall` when a numpy-style ufunc call is made on a groupby object with positional args or unexpected kwargs. pandas blocks these because groupby dispatching should go through the named pandas method (`.sum()`, `.mean()`, etc.) rather than `np.add`/`np.multiply`-style calls.","triggerScenarios":"`np.sum(df.groupby('x'))`; calling `df.groupby('x').transform(np.add, 1)`; passing extra positional args to a numpy function applied through groupby; using `df.groupby('x').apply(np.mean)` with extra args.","commonSituations":"Porting plain-numpy code to grouped operations; reading old tutorials that recommend `np.sum` on grouped objects; trying to pass extra ufunc parameters (like `where=`) through groupby.","solutions":["Use the equivalent named groupby method: `df.groupby('x').sum()` instead of `np.sum(df.groupby('x'))`.","For custom functions, define a python callable that takes the group and call `.apply`/`.transform` with it.","Pass scalar parameters by closure or partial, not as ufunc args."],"exampleFix":"// before\nnp.add(df.groupby('x'), 1)\n\n// after\ndef add_one(g):\n    return g + 1\ndf.groupby('x').apply(add_one)","handlingStrategy":"validation","validationCode":"def is_pure_groupby_call(args, kwargs, allowed):\n    return len(args) == 0 and set(kwargs).issubset(set(allowed))","typeGuard":null,"tryCatchPattern":"try:\n    df.groupby('x').agg(func)\nexcept Exception as e:\n    if 'numpy operations are not valid with groupby' in str(e):\n        df.groupby('x').apply(func)","preventionTips":["Use named groupby methods (.sum, .mean) instead of np.<func>.","Wrap custom logic in a python callable for apply/transform."],"tags":["groupby","numpy-compat","unsupported-call"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}