{"record":{"id":"d8af9961b9723792","repo":"pandas-dev/pandas","slug":"array-with-ndim-2-is-not-supported","errorCode":null,"errorMessage":"Array with ndim > 2 is not supported.","messagePattern":"Array with ndim > 2 is not supported\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/algorithms.py","lineNumber":1254,"sourceCode":"            ties_method=method,\n            ascending=ascending,\n            na_option=na_option,\n            pct=pct,\n            mask=mask,\n        )\n    elif values.ndim == 2:\n        assert mask is None\n        ranks = algos.rank_2d(\n            values,\n            axis=axis,\n            is_datetimelike=is_datetimelike,\n            ties_method=method,\n            ascending=ascending,\n            na_option=na_option,\n            pct=pct,\n        )\n    else:\n        raise TypeError(\"Array with ndim > 2 is not supported.\")\n\n    return ranks\n\n\ndef is_monotonic(values: ArrayLike) -> tuple[bool, bool, bool]:\n    \"\"\"\n    Determine whether values are monotonic increasing/decreasing.\n\n    Parameters\n    ----------\n    values : np.ndarray or ExtensionArray\n\n    Returns\n    -------\n    tuple[bool, bool, bool]\n        (is_monotonic_increasing, is_monotonic_decreasing, is_strict_monotonic)\n\n    Raises","sourceCodeStart":1236,"sourceCodeEnd":1272,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/algorithms.py#L1236-L1272","documentation":"The rank() algorithm supports only 1-D and 2-D arrays. For 1-D input it calls rank_1d; for 2-D it calls rank_2d with an axis parameter. Any array with three or more dimensions falls through to this TypeError. This is a fundamental limitation — the ranking logic has no implementation for higher-dimensional data.","triggerScenarios":"Calling pd.Series.rank() or DataFrame.rank() on data that has been reshaped to 3+ dimensions. Passing a 3-D numpy array to pandas.core.algorithms.rank directly. Attempting to rank a panel-like or multi-dimensional numpy array without first flattening or selecting a 2-D slice.","commonSituations":"Reshaping data with np.reshape or xarray-to-numpy conversion that produces 3-D arrays, then passing them to pandas ranking functions. Working with image data or tensor representations. Building multi-index DataFrames from higher-dimensional arrays and calling rank before reducing dimensions.","solutions":["Reshape the array to 1-D or 2-D before ranking: arr.reshape(-1) or arr.reshape(arr.shape[0], -1).","Apply rank() to individual Series or DataFrame columns rather than the raw high-dimensional array.","If you have panel data, use a MultiIndex DataFrame (2-D) instead of a 3-D numpy array."],"exampleFix":"# before\narr_3d = np.random.randn(3, 4, 5)\npd.DataFrame(arr_3d).rank()\n\n# after — reshape to 2-D first\narr_2d = arr_3d.reshape(arr_3d.shape[0], -1)\npd.DataFrame(arr_2d).rank()","handlingStrategy":"validation","validationCode":"def safe_rank(arr):\n    arr = np.asarray(arr)\n    if arr.ndim > 2:\n        raise ValueError(f\"rank supports max 2-D arrays, got ndim={arr.ndim}\")\n    return pd.DataFrame(arr).rank()","typeGuard":"def is_rankable_ndim(arr) -> bool:\n    return np.asarray(arr).ndim <= 2","tryCatchPattern":"try:\n    result = pd.DataFrame(arr).rank()\nexcept TypeError as e:\n    if \"ndim > 2\" in str(e):\n        arr = np.asarray(arr).reshape(-1)\n        result = pd.Series(arr).rank()\n    else:\n        raise","preventionTips":["Reshape high-dimensional arrays to 2-D before passing to pandas ranking.","Use np.asarray(arr).ndim to validate dimensionality before calling rank.","Prefer MultiIndex DataFrames over raw 3-D numpy arrays for panel data."],"tags":["pandas","rank","ndim","shape","typeerror"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}