pandas-dev/pandas · error · ValueError

Value must be 1-D array-like or scalar

Error message

Value must be 1-D array-like or scalar, {type(value).__name__} is not supported

What it means

Raised by IndexOpsMixin.searchsorted when `value` is a DataFrame. searchsorted requires a 1-D array-like or scalar to find insertion points in a sorted array; a 2-D DataFrame has no defined 1-D ordering. The error names the offending type so users see 'DataFrame' explicitly. Other 2-D inputs may fail later with a NumPy error; only the DataFrame case is pre-checked.

Solutions

  1. Pass a single column: ser.searchsorted(df['col']).
  2. Pass a scalar or 1-D list/array of search values.
  3. Apply per-column with a comprehension if you need results for many columns.

Example fix

// before
pos = ser.searchsorted(df)
// after
pos = ser.searchsorted(df['key'])
Defensive patterns

Strategy: type-guard

Validate before calling

import pandas as pd
if isinstance(value, pd.DataFrame):
    raise TypeError('pass a 1-D column or scalar, not a DataFrame')
pos = ser.searchsorted(value)

Type guard

def is_dataframe(x) -> bool:
    return isinstance(x, pd.DataFrame)

Prevention

When it happens

Trigger: ser.searchsorted(df); ser.searchsorted(df['col']) works (1-D) but passing the whole df raises; passing a 2-D ndarray may not hit this branch but is also unsupported.

Common situations: Forgetting to select a column before searchsorted; passing a DataFrame built from a lookup table thinking searchsorted broadcasts.

Related errors


AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11). Data as JSON: /api/errors/e47d285dd33e809f. Report an issue: GitHub.

Appendix: 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 3b7651241d)