{"record":{"id":"adaa2b340fa25bb0","repo":"pandas-dev/pandas","slug":"axis-axis-out-of-bounds","errorCode":null,"errorMessage":"axis(={axis}) out of bounds","messagePattern":"axis\\(=(.+?)\\) out of bounds","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/sparse/array.py","lineNumber":1698,"sourceCode":"\n        When performing the cumulative summation, any non-NA/null values will\n        be skipped. The resulting SparseArray will preserve the locations of\n        NaN values, but the fill value will be `np.nan` regardless.\n\n        Parameters\n        ----------\n        axis : int or None\n            Axis over which to perform the cumulative summation. If None,\n            perform cumulative summation over flattened array.\n\n        Returns\n        -------\n        cumsum : SparseArray\n        \"\"\"\n        nv.validate_cumsum(args, kwargs)\n\n        if axis is not None and axis >= self.ndim:  # Mimic ndarray behaviour.\n            raise ValueError(f\"axis(={axis}) out of bounds\")\n\n        if not self._null_fill_value:\n            return SparseArray(self.to_dense(), fill_value=np.nan).cumsum()\n\n        return SparseArray(\n            self.sp_values.cumsum(),\n            sparse_index=self.sp_index,\n            fill_value=self.fill_value,\n        )\n\n    def mean(self, axis: Axis = 0, *args, skipna: bool = True, **kwargs):\n        \"\"\"\n        Mean of non-NA/null values.\n\n        Parameters\n        ----------\n        axis : int, default 0\n            Not Used. NumPy compatibility.","sourceCodeStart":1680,"sourceCodeEnd":1716,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/sparse/array.py#L1680-L1716","documentation":"Raised by SparseArray.cumsum when `axis` is not None and axis >= self.ndim (which is 1 for a 1-D SparseArray). It mimics ndarray.cumsum's bounds check so that passing axis=1 on a 1-D sparse array surfaces a clear error rather than silently being ignored or mis-shaping the result.","triggerScenarios":"sparse_arr.cumsum(axis=1), df.cummax(axis=1) on a sparse-backed DataFrame column broadcast with an axis arg, or code that assumes 2-D semantics.","commonSituations":"Generic axis-handling code written for DataFrames applied to a Series/SparseArray, or copy-paste from a 2-D reduction into a 1-D cumsum call.","solutions":["Pass axis=0 (or None) for a 1-D SparseArray: sparse_arr.cumsum(axis=0).","Drop the axis argument entirely since 1-D cumsum ignores it when valid.","If you genuinely need 2-D, operate on a DataFrame with sparse dtype and ensure the column count justifies axis=1."],"exampleFix":"// before\nout = sparse_arr.cumsum(axis=1)  # raises 'axis(=1) out of bounds'\n\n// after\nout = sparse_arr.cumsum(axis=0)","handlingStrategy":"validation","validationCode":"def cumsum_safe(arr, axis=0):\n    if axis is not None and axis >= arr.ndim:\n        raise ValueError(f'axis(={axis}) out of bounds for ndim={arr.ndim}')\n    return arr.cumsum(axis=axis)","typeGuard":"def axis_is_valid(arr, axis) -> bool:\n    return axis is None or -arr.ndim <= axis < arr.ndim","tryCatchPattern":"try:\n    out = arr.cumsum(axis=axis)\nexcept ValueError as e:\n    if 'out of bounds' in str(e):\n        out = arr.cumsum(axis=0)\n    else:\n        raise","preventionTips":["Pass axis=0 (or None) for 1-D SparseArray cumsum","Avoid copy-pasting 2-D reduction axis arguments into 1-D calls","Validate axis against arr.ndim at API boundaries"],"tags":["sparse","cumsum","axis","bounds"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}