{"record":{"id":"0bec0750e682ce35","repo":"pola-rs/polars","slug":"only-call-is-implemented-not-method","errorCode":null,"errorMessage":"Only call is implemented not {method}","messagePattern":"Only call is implemented not (.+?)","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/expr/expr.py","lineNumber":472,"sourceCode":"        other_expr = parse_into_expression(other)\n        return wrap_expr(other_expr.xor_(self._pyexpr))\n\n    def __getstate__(self) -> bytes:\n        return self._pyexpr.__getstate__()\n\n    def __setstate__(self, state: bytes) -> None:\n        # Initialize with a dummy\n        tmp = F.lit(0)._pyexpr\n        tmp.__setstate__(state)\n        self._pyexpr = tmp\n\n    def __array_ufunc__(\n        self, ufunc: Callable[..., Any], method: str_, *inputs: Any, **kwargs: Any\n    ) -> Expr:\n        \"\"\"Numpy universal functions.\"\"\"\n        if method != \"__call__\":\n            msg = f\"Only call is implemented not {method}\"\n            raise NotImplementedError(msg)\n        # Numpy/Scipy ufuncs have signature None but numba signatures always exists.\n        is_custom_ufunc = getattr(ufunc, \"signature\") is not None  # noqa: B009\n        if is_custom_ufunc is True:\n            msg = (\n                \"Native numpy ufuncs are dispatched using `map_batches(ufunc, is_elementwise=True)` which \"\n                \"is safe for native Numpy and Scipy ufuncs but custom ufuncs in a group_by \"\n                \"context won't be properly grouped. Custom ufuncs are dispatched with is_elementwise=False. \"\n                f\"If {ufunc.__name__} needs elementwise then please use map_batches directly.\"\n            )\n            warnings.warn(\n                msg,\n                CustomUFuncWarning,\n                stacklevel=find_stacklevel(),\n            )\n        if len(inputs) == 1 and len(kwargs) == 0:\n            # if there is only 1 input then it must be an Expr for this func to\n            # have been called. If there are no kwargs then call map_batches\n            # directly on the ufunc","sourceCodeStart":454,"sourceCodeEnd":490,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/expr/expr.py#L454-L490","documentation":"Expr implements NumPy's ufunc protocol (via __array_ufunc__) only for plain calls, where method == '__call__'. NumPy ufunc methods — reduce, accumulate, outer, reduceat, at — route through the same protocol with a different method string and raise NotImplementedError. Elementwise usage like np.sqrt(expr) works; np.add.reduce(expr) does not.","triggerScenarios":"np.add.reduce(pl.col('a')); np.multiply.accumulate(pl.col('a')); np.subtract.outer(e1, e2); any ufunc-method syntax applied to an Expr.","commonSituations":"Porting numpy reduction idioms (sum via np.add.reduce) to polars expressions; scipy/numba code that calls ufunc methods; np.vectorize-style wrappers.","solutions":["Use native polars equivalents: .sum(), .cum_sum(), .dot() instead of reduce/accumulate/outer","Call the ufunc directly for elementwise math: np.sqrt(expr) (dispatched via __call__)","If a numpy reduction is truly required, wrap it: expr.map_batches(lambda s: np.add.reduce(s))"],"exampleFix":"# before\nout = np.add.reduce(pl.col('a'))  # NotImplementedError\n\n# after\nout = pl.col('a').sum()\n# or elementwise: out = np.sqrt(pl.col('a'))","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    out = np.add.reduce(expr)\nexcept NotImplementedError:\n    out = expr.sum()  # native reduction fallback","preventionTips":["Use native .sum()/.cum_sum()/.dot() for reductions and outer products","Call ufuncs directly for elementwise math (method '__call__' is the only supported path)","Never use ufunc .reduce/.accumulate/.outer/.at syntax on Expr objects"],"tags":["numpy","ufunc","expr","not-implemented"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}