pandas-dev/pandas · error · TypeError
Array with ndim > 2 is not supported.
Error message
Array with ndim > 2 is not supported.
What it means
Raised by pandas.core.algorithms.rank when the input array has more than 2 dimensions. rank only implements 1-D (rank_1d) and 2-D (rank_2d) paths; a 3-D+ array (e.g. a stacked tensor) has no supported ranking axis mapping.
Source
Thrown at pandas/core/algorithms.py:1254
ties_method=method,
ascending=ascending,
na_option=na_option,
pct=pct,
mask=mask,
)
elif values.ndim == 2:
assert mask is None
ranks = algos.rank_2d(
values,
axis=axis,
is_datetimelike=is_datetimelike,
ties_method=method,
ascending=ascending,
na_option=na_option,
pct=pct,
)
else:
raise TypeError("Array with ndim > 2 is not supported.")
return ranks
def is_monotonic(values: ArrayLike) -> tuple[bool, bool, bool]:
"""
Determine whether values are monotonic increasing/decreasing.
Parameters
----------
values : np.ndarray or ExtensionArray
Returns
-------
tuple[bool, bool, bool]
(is_monotonic_increasing, is_monotonic_decreasing, is_strict_monotonic)
RaisesView on GitHub (pinned to 71959b8cb9)
Solutions
- Reduce to 1-D or 2-D before ranking (reshape, stack, or loop over the extra dimension).
- Rank a 2-D slice (arr[:, :, 0]) and iterate over the third axis.
- Use DataFrame.rank on a proper 2-D frame instead of a 3-D array.
Example fix
# before
arr = np.arange(24).reshape(2, 3, 4)
pd.core.algorithms.rank(arr)
# after
ranks = np.empty_like(arr, dtype=float)
for k in range(arr.shape[2]):
ranks[:, :, k] = pd.core.algorithms.rank(arr[:, :, k]) Defensive patterns
Strategy: validation
Validate before calling
import numpy as np, pandas as pd
def rank_any(arr, **kw):
arr = np.asarray(arr)
if arr.ndim > 2:
out = np.empty(arr.shape, dtype=float)
for idx in np.ndindex(arr.shape[2:]):
full = (slice(None), slice(None)) + idx
out[full] = pd.core.algorithms.rank(arr[full], **kw)
return out
return pd.core.algorithms.rank(arr, **kw) Type guard
import numpy as np
def is_rankable(arr) -> bool:
return np.asarray(arr).ndim <= 2 Prevention
- Reduce ndim to <=2 before calling rank.
- Rank 2-D slices and iterate the extra axes.
- Use DataFrame.rank on proper 2-D frames.
When it happens
Trigger: Calling rank on a 3-D numpy array, or constructing a DataFrame/array whose underlying values are 3-D and routing it through the rank algorithm; reshaping data into ndim>2 before ranking.
Common situations: Multi-indexed/panel-like data flattened into a 3-D ndarray; passing raw np.ndarray of shape (n, m, k) to rank; custom ExtensionArray whose _values is 3-D.
Related errors
- Need to pass bool-like values
- values must be a 1D list-like
- mask must be a 1D list-like
- No such keys(s): {pat!r}
- {k} is not a valid identifier
AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07).
Data as JSON: /api/errors/d8af9961b9723792.
Report an issue: GitHub.