{"record":{"id":"e12c6ae25024981f","repo":"pandas-dev/pandas","slug":"cannot-apply-ufunc-ufunc-to-mixed-dataframe-and","errorCode":null,"errorMessage":"Cannot apply ufunc {ufunc} to mixed DataFrame and Series inputs.","messagePattern":"Cannot apply ufunc (.+?) to mixed DataFrame and Series inputs\\.","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"pandas/core/arraylike.py","lineNumber":336,"sourceCode":"            return NotImplemented\n\n    # align all the inputs.\n    types = tuple(type(x) for x in inputs)\n    alignable = [\n        x for x, t in zip(inputs, types, strict=True) if issubclass(t, NDFrame)\n    ]\n\n    if len(alignable) > 1:\n        # This triggers alignment.\n        # At the moment, there aren't any ufuncs with more than two inputs\n        # so this ends up just being x1.index | x2.index, but we write\n        # it to handle *args.\n        set_types = set(types)\n        if len(set_types) > 1 and {DataFrame, Series}.issubset(set_types):\n            # We currently don't handle ufunc(DataFrame, Series)\n            # well. Previously this raised an internal ValueError. We might\n            # support it someday, so raise a NotImplementedError.\n            raise NotImplementedError(\n                f\"Cannot apply ufunc {ufunc} to mixed DataFrame and Series inputs.\"\n            )\n        axes = self.axes\n        for obj in alignable[1:]:\n            # this relies on the fact that we aren't handling mixed\n            # series / frame ufuncs.\n            for i, (ax1, ax2) in enumerate(zip(axes, obj.axes, strict=True)):\n                axes[i] = ax1.union(ax2)\n\n        reconstruct_axes = dict(zip(self._AXIS_ORDERS, axes, strict=True))\n        inputs = tuple(\n            x.reindex(**reconstruct_axes) if issubclass(t, NDFrame) else x\n            for x, t in zip(inputs, types, strict=True)\n        )\n    else:\n        reconstruct_axes = dict(zip(self._AXIS_ORDERS, self.axes, strict=True))\n\n    if self.ndim == 1:","sourceCodeStart":318,"sourceCodeEnd":354,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arraylike.py#L318-L354","documentation":"NotImplementedError raised in the arraylike ufunc dispatch when a numpy ufunc is called with mixed DataFrame and Series inputs. pandas would previously raise an internal ValueError during alignment; now it explicitly states the combination is unsupported pending a future implementation.","triggerScenarios":"np.add(df, series); np.multiply(df, series); any np.<ufunc>(DataFrame, Series) call.","commonSituations":"Vectorising math with numpy ufuncs and passing a Series (e.g. a row of weights) alongside a DataFrame.","solutions":["Convert the Series to a DataFrame matching the desired axis: np.add(df, s.to_frame()).","Use pandas arithmetic operators which handle alignment: df + s (broadcasts across columns) or df.add(s, axis=...).","Align manually: reindex the Series to the DataFrame index/columns and pass it as a 2-D object."],"exampleFix":"# before\nnp.add(df, s)  # df is DataFrame, s is Series\n# after\ndf + s  # pandas operator handles alignment","handlingStrategy":"fallback","validationCode":"import numpy as np, pandas as pd\ndef safe_ufunc(ufunc, df, s):\n    if isinstance(df, pd.DataFrame) and isinstance(s, pd.Series):\n        return df + s.to_frame() if ufunc is np.add else ufunc(df, s.to_frame())\n    return ufunc(df, s)","typeGuard":"def is_mixed_df_series(a, b) -> bool:\n    import pandas as pd\n    return {type(a), type(b)} == {pd.DataFrame, pd.Series}","tryCatchPattern":"try:\n    np.add(df, s)\nexcept NotImplementedError as e:\n    if 'mixed DataFrame and Series' in str(e):\n        df + s  # pandas operator handles alignment\n    else:\n        raise","preventionTips":["Prefer pandas arithmetic operators over numpy ufuncs when mixing DataFrame and Series.","Convert Series to a one-column DataFrame before passing to numpy ufuncs."],"tags":["numpy","ufunc","dataframe","series","alignment"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}