diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 1ca513e8f5e6a..4f61cc51e9f7e 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -740,6 +740,8 @@ Metadata Other ^^^^^ +- Bug in :meth:`Series.searchsorted` inconsistent behavior when accepting :class:`DataFrame` as parameter ``value`` (:issue:`49620`) +- .. ***DO NOT USE THIS SECTION*** diff --git a/pandas/core/base.py b/pandas/core/base.py index 3d06c1830cc53..9957535020563 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -1270,6 +1270,13 @@ def searchsorted( sorter: NumpySorter = None, ) -> npt.NDArray[np.intp] | np.intp: + 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 diff --git a/pandas/tests/series/methods/test_searchsorted.py b/pandas/tests/series/methods/test_searchsorted.py index 5a7eb3f8cfc97..239496052b99b 100644 --- a/pandas/tests/series/methods/test_searchsorted.py +++ b/pandas/tests/series/methods/test_searchsorted.py @@ -1,5 +1,7 @@ import numpy as np +import pytest +import pandas as pd from pandas import ( Series, Timestamp, @@ -65,3 +67,11 @@ def test_searchsorted_sorter(self): res = ser.searchsorted([0, 3], sorter=np.argsort(ser)) exp = np.array([0, 2], dtype=np.intp) tm.assert_numpy_array_equal(res, exp) + + def test_searchsorted_dataframe_fail(self): + # GH#49620 + ser = Series([1, 2, 3, 4, 5]) + vals = pd.DataFrame([[1, 2], [3, 4]]) + msg = "Value must be 1-D array-like or scalar, DataFrame is not supported" + with pytest.raises(ValueError, match=msg): + ser.searchsorted(vals)