pandas-dev/pandas · error · TypeError
Array with ndim > 2 is not supported.
Error message
Array with ndim > 2 is not supported.
What it means
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.
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.
Example fix
# before arr_3d = np.random.randn(3, 4, 5) pd.DataFrame(arr_3d).rank() # after — reshape to 2-D first arr_2d = arr_3d.reshape(arr_3d.shape[0], -1) pd.DataFrame(arr_2d).rank()
Defensive patterns
Strategy: validation
Validate before calling
def safe_rank(arr):
arr = np.asarray(arr)
if arr.ndim > 2:
raise ValueError(f"rank supports max 2-D arrays, got ndim={arr.ndim}")
return pd.DataFrame(arr).rank() Type guard
def is_rankable_ndim(arr) -> bool:
return np.asarray(arr).ndim <= 2 Try / catch
try:
result = pd.DataFrame(arr).rank()
except TypeError as e:
if "ndim > 2" in str(e):
arr = np.asarray(arr).reshape(-1)
result = pd.Series(arr).rank()
else:
raise Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- bins argument only works with numeric data.
- can only perform ops with 1-d structures
- Column length mismatch
- 'data' must have a single column, not
- requires a Series, Index, ExtensionArray, np.ndarray or…
AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11).
Data as JSON: /api/errors/d8af9961b9723792.
Report an issue: GitHub.
Appendix: 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 3b7651241d)