pandas-dev/pandas · error · ValueError
Value must be 1-D array-like or scalar, {type(value).__name_
Error message
Value must be 1-D array-like or scalar, {type(value).__name__} is not supported What it means
Raised by IndexOpsMixin.searchsorted (pandas/core/base.py:1653) when the `value` argument is a 2-D object such as a DataFrame. searchsorted performs a binary search on a sorted 1-D array-like, so a rectangular DataFrame has no well-defined insertion point and is explicitly rejected before any lookup is attempted. Only 1-D array-likes (list, Series, Index, 1-D ndarray) or scalars are accepted.
Source
Thrown at pandas/core/base.py:1653
If the values are not monotonically sorted, wrong locations
may be returned:
>>> ser = pd.Series([2, 1, 3])
>>> ser
0 2
1 1
2 3
dtype: int64
>>> ser.searchsorted(1) # doctest: +SKIP
0 # wrong result, correct would be 1
"""
if isinstance(value, ABCDataFrame):
msg = (
"Value must be 1-D array-like or scalar, "
f"{type(value).__name__} is not supported"
)
raise ValueError(msg)
values = self._values
if not isinstance(values, np.ndarray):
# Going through EA.searchsorted directly improves performance GH#38083
return values.searchsorted(value, side=side, sorter=sorter)
return algorithms.searchsorted(
values,
value,
side=side,
sorter=sorter,
)
def drop_duplicates(self, *, keep: DropKeep = "first") -> Self:
duplicated = self._duplicated(keep=keep)
# error: Value of type "IndexOpsMixin" is not indexable
return self[~duplicated] # type: ignore[index]
View on GitHub (pinned to 71959b8cb9)
Solutions
- Change the passed value to a 1-D structure: use `df['col']` (Series) or `df['col'].values` (1-D ndarray) instead of `df[['col']]`.
- If you genuinely have multiple keys to locate, call searchsorted once per column in a loop or list comprehension, or use `Index.get_indexer` for vectorized lookups.
- If you have a single value per row, squeeze the frame first: `df['col']` or `df.squeeze('columns')`.
Example fix
# before idx.searchsorted(df[['date']]) # after idx.searchsorted(df['date'])
Defensive patterns
Strategy: validation
Validate before calling
import pandas as pd
def safe_searchsorted(index, value):
if isinstance(value, pd.DataFrame):
raise TypeError("searchsorted requires 1-D array-like or scalar, got DataFrame")
return index.searchsorted(value) Type guard
import pandas as pd
def is_searchsortable(value) -> bool:
return not isinstance(value, pd.DataFrame) and (
pd.api.types.is_scalar(value) or getattr(value, 'ndim', 1) == 1
) Try / catch
try:
pos = idx.searchsorted(value)
except ValueError as e:
if 'not supported' in str(e):
value = value.squeeze() if hasattr(value, 'squeeze') else value
pos = idx.searchsorted(value)
else:
raise Prevention
- Prefer single-bracket column selection df['col'] over df[['col']] before searchsorted.
- Add a dimension check in helper functions that feed searchsorted.
- Use Index.get_indexer for vectorized multi-key lookups.
When it happens
Trigger: Calling `idx.searchsorted(df)` or `ser.searchsorted(df)` where `df` is a pandas DataFrame. Also triggered indirectly when a function computes a searchsorted value and accidentally passes a DataFrame column-selection that returns a DataFrame (e.g. `df[['col']]` instead of `df['col']`).
Common situations: Selecting with double brackets `df[['col']]` (returns DataFrame) instead of single `df['col']` (returns Series) and feeding it into searchsorted. Constructing a target from `pd.concat(..., axis=1)` and forgetting to squeeze to one dimension.
Related errors
- Named aggregation is not supported when {axis=}.
- by_row={by_row} not allowed
- Cannot apply ufunc {ufunc} to mixed DataFrame and Series inp
- key must be an int or slice, got {type(key).__name__}
- name_or_index must be an int, str, bytes, pyarrow.compute.Ex
AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07).
Data as JSON: /api/errors/e47d285dd33e809f.
Report an issue: GitHub.