{"record":{"id":"9affb2181372517e","repo":"pola-rs/polars","slug":"can-t-pass-a-series-with-missing-data-to-a-general","errorCode":null,"errorMessage":"can't pass a Series with missing data to a generalized ufunc, as it might give unexpected results. See https://docs.pola.rs/user-guide/expressions/missing-data/ for suggestions on how to remove or fill in missing data.","messagePattern":"can't pass a Series with missing data to a generalized ufunc, as it might give unexpected results\\. See https://docs\\.pola\\.rs/user-guide/expressions/missing-data/ for suggestions on how to remove or fill in missing data\\.","errorType":"exception","errorClass":"ComputeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/series/series.py","lineNumber":1568,"sourceCode":"                    dtype_char_minimum = dtype_ufunc\n                    break\n\n            # Override minimum dtype if requested.\n            dtype_char = (\n                np.dtype(kwargs.pop(\"dtype\")).char\n                if \"dtype\" in kwargs\n                else dtype_char_minimum\n            )\n\n            # Only generalized ufuncs have a signature set:\n            is_generalized_ufunc = bool(ufunc.signature)\n\n            if is_generalized_ufunc:\n                # Generalized ufuncs will operate on the whole array, so\n                # missing data can corrupt the results.\n                if self.has_nulls():\n                    msg = \"can't pass a Series with missing data to a generalized ufunc, as it might give unexpected results. See https://docs.pola.rs/user-guide/expressions/missing-data/ for suggestions on how to remove or fill in missing data.\"\n                    raise ComputeError(msg)\n                # If the input and output are the same size, e.g. \"(n)->(n)\" we\n                # can allocate ourselves and save a copy. If they're different,\n                # we let the ufunc do the allocation, since only it knows the\n                # output size.\n                assert ufunc.signature is not None  # pacify MyPy\n                ufunc_input, ufunc_output = ufunc.signature.split(\"->\")\n                if ufunc_output == \"()\":\n                    # If the result a scalar, just let the function do its\n                    # thing, no need for any song and dance involving\n                    # allocation:\n                    return ufunc(*args, dtype=dtype_char, **kwargs)\n                else:\n                    allocate_output = ufunc_input == ufunc_output\n            else:\n                allocate_output = True\n\n            f = get_ffi_func(\"apply_ufunc_<>\", numpy_char_code_to_dtype(dtype_char), s)\n","sourceCodeStart":1550,"sourceCodeEnd":1586,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/series/series.py#L1550-L1586","documentation":"Generalized numpy ufuncs (gufuncs, e.g. with signature '(n)->(n)') operate over the entire array, so nulls in the input Series would corrupt or propagate incorrectly through the whole computation. Polars refuses to pass a Series containing nulls to such a ufunc and raises ComputeError.","triggerScenarios":"Applying a generalized ufunc via np.apply/ufunc dispatch (Series.__array_ufunc__ with is_generalized_ufunc=True) while self.has_nulls() is True, e.g. np.matmul-style gufuncs or np.vectorize with signature containing '->' shapes.","commonSituations":"Data loaded from files with missing values passed to a gufunc; forgetting to drop_nulls()/fill_null() after joins or parsing.","solutions":["Remove nulls first: srs.drop_nulls()","Fill nulls: srs.fill_null(0) (or an appropriate value)","Filter to non-null rows before calling the ufunc"],"exampleFix":"// before\nnp.gufunc_like(srs)\n// after\nnp.gufunc_like(srs.drop_nulls())","handlingStrategy":"validation","validationCode":"if srs.has_nulls():\n    srs = srs.drop_nulls()  # or fill_null(...)","typeGuard":null,"tryCatchPattern":"try:\n    out = gufunc(srs)\nexcept pl.exceptions.ComputeError:\n    out = gufunc(srs.drop_nulls())","preventionTips":["Check has_nulls() before generalized ufuncs","Run drop_nulls()/fill_null() right after loading or joining data","Document null-handling policy for numeric pipelines"],"tags":["numpy","polars","nulls","ufunc"],"backgroundTag":"null-argument","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}