{"record":{"id":"c5d974aad93ec3c0","repo":"pandas-dev/pandas","slug":"the-index-columns-must-be-unique-when-raw-false-an","errorCode":null,"errorMessage":"The index/columns must be unique when raw=False and engine='numba'","messagePattern":"The index/columns must be unique when raw=False and engine='numba'","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"pandas/core/apply.py","lineNumber":1311,"sourceCode":"\n        results = {}\n\n        for i, v in enumerate(series_gen):\n            results[i] = self.func(v, *self.args, **self.kwargs)\n            if isinstance(results[i], ABCSeries):\n                # If we have a view on v, we need to make a copy because\n                #  series_generator will swap out the underlying data\n                results[i] = results[i].copy(deep=False)\n\n        return results, res_index\n\n    def apply_series_numba(self):\n        if self.engine_kwargs.get(\"parallel\", False):\n            raise NotImplementedError(\n                \"Parallel apply is not supported when raw=False and engine='numba'\"\n            )\n        if not self.obj.index.is_unique or not self.columns.is_unique:\n            raise NotImplementedError(\n                \"The index/columns must be unique when raw=False and engine='numba'\"\n            )\n        self.validate_values_for_numba()\n        results = self.apply_with_numba()\n        return results, self.result_index\n\n    def wrap_results(self, results: ResType, res_index: Index) -> DataFrame | Series:\n        from pandas import Series\n\n        # see if we can infer the results\n        if len(results) > 0 and 0 in results and is_sequence(results[0]):\n            return self.wrap_results_for_axis(results, res_index)\n\n        # dict of scalars\n\n        # the default dtype of an empty Series is `object`, but this\n        # code can be hit by df.mean() where the result should have dtype\n        # float64 even if it's an empty Series.","sourceCodeStart":1293,"sourceCodeEnd":1329,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/apply.py#L1293-L1329","documentation":"Raised by apply_series_numba when engine='numba' with raw=False and the underlying DataFrame/Series has non-unique index labels or duplicate column names. The numba path builds its result by positional assignment and then realigns by labels, which is impossible when labels repeat, so pandas refuses instead of producing silently misaligned output.","triggerScenarios":"df.apply(func, engine='numba') where df.index.has_duplicates or df.columns.has_duplicates; same on a Series whose index has duplicates.","commonSituations":"Applying numba engine on pivoted/aggregated frames that retained duplicate index entries, or on a Series produced by .value_counts() / groupby sums that you forgot to reset.","solutions":["Deduplicate the index before applying: df = df.reset_index(drop=True) (or df[~df.index.duplicated(keep='first')]).","Remove duplicate columns: df = df.loc[:, ~df.columns.duplicated()].","Fall back to the default engine: df.apply(func) (no engine='numba')."],"exampleFix":"# before\ndf.apply(func, engine='numba')  # df.index has duplicates\n# after\ndf.reset_index(drop=True).apply(func, engine='numba')","handlingStrategy":"validation","validationCode":"def ensure_unique_for_numba(df):\n    if df.index.has_duplicates:\n        df = df.reset_index(drop=True)\n    if getattr(df.columns, 'has_duplicates', False):\n        df = df.loc[:, ~df.columns.duplicated()]\n    return df\n\ndf = ensure_unique_for_numba(df)\ndf.apply(func, engine='numba')","typeGuard":null,"tryCatchPattern":"try:\n    df.apply(func, engine='numba')\nexcept NotImplementedError as e:\n    if 'must be unique' in str(e):\n        df.reset_index(drop=True).apply(func, engine='numba')\n    else:\n        raise","preventionTips":["Before numba apply, assert df.index.is_unique and df.columns.is_unique.","Reset index after any groupby/agg that may produce duplicate labels."],"tags":["numba","apply","duplicate-labels","engine"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}