{"record":{"id":"de7be14cd65ac312","repo":"pandas-dev/pandas","slug":"axis-axis-is-out-of-bounds-for-array-of-dimensio","errorCode":null,"errorMessage":"axis {axis} is out of bounds for array of dimension {first.ndim}","messagePattern":"axis (.+?) is out of bounds for array of dimension (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/categorical.py","lineNumber":2692,"sourceCode":"        codes = self.codes.copy()\n        mask = self.isna()\n        if func == np.minimum.accumulate:\n            codes[mask] = np.iinfo(codes.dtype.type).max\n        # no need to change codes for maximum because codes[mask] is already -1\n        if not skipna:\n            mask = np.maximum.accumulate(mask)\n\n        codes = func(codes)\n        codes[mask] = -1\n        return self._simple_new(codes, dtype=self._dtype)\n\n    @classmethod\n    def _concat_same_type(cls, to_concat: Sequence[Self], axis: AxisInt = 0) -> Self:\n        from pandas.core.dtypes.concat import union_categoricals\n\n        first = to_concat[0]\n        if axis >= first.ndim:\n            raise ValueError(\n                f\"axis {axis} is out of bounds for array of dimension {first.ndim}\"\n            )\n\n        if axis == 1:\n            # Flatten, concatenate then reshape\n            if not all(x.ndim == 2 for x in to_concat):\n                raise ValueError\n\n            # pass correctly-shaped to union_categoricals\n            tc_flat = []\n            for obj in to_concat:\n                tc_flat.extend([obj[:, i] for i in range(obj.shape[1])])\n\n            res_flat = cls._concat_same_type(tc_flat, axis=0)\n\n            result = res_flat.reshape(len(first), -1, order=\"F\")\n            return result\n","sourceCodeStart":2674,"sourceCodeEnd":2710,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/categorical.py#L2674-L2710","documentation":"Raised by Categorical._concat_same_type when axis is greater than or equal to the number of dimensions of the first array (first.ndim). A 1-D categorical only has axis 0, so axis=1 is out of bounds. This mirrors numpy's axis-bounds semantics for the concat path that builds the union of categoricals.","triggerScenarios":"Calling pd.concat([cat1, cat2], axis=1) where the inputs are 1-D Categorical arrays routed through _concat_same_type; or an internal caller passing an axis that exceeds ndim. CategoricalIndex column-wise concat is the typical surface.","commonSituations":"Building a DataFrame from two Categorical Series with axis=1 (column-wise) where pandas falls into the same-type categorical concat path; or passing user-supplied axis values to groupby/concat internals.","solutions":["Use axis=0 (the only valid axis for 1-D categoricals) and reshape afterward.","Construct the DataFrame via pd.DataFrame({...}) instead of pd.concat(axis=1) for column-wise assembly of categorical Series.","Verify ndim of inputs and pass an axis strictly less than ndim."],"exampleFix":"// before\npd.concat([pd.Categorical(['a']), pd.Categorical(['b'])], axis=1)  # ValueError\n\n// after\npd.DataFrame({'x': pd.Categorical(['a']), 'y': pd.Categorical(['b'])})","handlingStrategy":"validation","validationCode":"def safe_concat(parts, axis):\n    first = parts[0]\n    if axis >= first.ndim:\n        raise ValueError(f'axis {axis} out of bounds for ndim {first.ndim}; use DataFrame ctor')\n    return pd.concat(parts, axis=axis)","typeGuard":"from typing import Any\n\ndef axis_in_bounds(obj: Any, axis: int) -> bool:\n    return 0 <= axis < getattr(obj, 'ndim', 1)","tryCatchPattern":"try:\n    pd.concat(parts, axis=axis)\nexcept ValueError as e:\n    if 'out of bounds for array of dimension' in str(e):\n        result = pd.concat(parts, axis=0)\n    else:\n        raise","preventionTips":["Use axis=0 for 1-D categoricals; build DataFrames for column-wise assembly.","Check ndim before forwarding an axis parameter."],"tags":["categorical","concat","axis"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}