Skip to content

Bug: Fix Inconsistent Behavior for Series.searchsorted #49706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
ab4e851
Bug: Fix inconsistent behavior in searchsorted (#49620)
yqyqyq-W Nov 15, 2022
8e33f8e
Merge branch 'pandas-dev:main' into main
yqyqyq-W Nov 15, 2022
60d05d6
Merge branch 'pandas-dev:main' into main
yqyqyq-W Nov 15, 2022
0b1b450
Update v2.0.0.rst
yqyqyq-W Nov 15, 2022
f043fb5
Merge branch 'main' of https://github.com/yqyqyq-W/pandas
yqyqyq-W Nov 15, 2022
edd6099
Revert "Update v2.0.0.rst"
yqyqyq-W Nov 15, 2022
340de9a
Update whatsnew
yqyqyq-W Nov 15, 2022
06337f6
Update v2.0.0.rst
yqyqyq-W Nov 15, 2022
090bd79
Update base.py
yqyqyq-W Nov 15, 2022
56b3fba
Revert "Update base.py"
yqyqyq-W Nov 15, 2022
12db40d
Bug: Fix inconsistent behavior in searchsorted (#49620)
yqyqyq-W Nov 15, 2022
34ecabc
Update whatsnew
yqyqyq-W Nov 15, 2022
b2053d8
Merge branch 'pandas-dev:main' into main
yqyqyq-W Nov 15, 2022
023a7c7
Bug: Fix inconsistent behavior in searchsorted (#49620)
yqyqyq-W Nov 16, 2022
9037cc9
Add test for failure on DataFrame
yqyqyq-W Nov 16, 2022
72f809c
Merge branch 'main' of https://github.com/yqyqyq-W/pandas
yqyqyq-W Nov 16, 2022
063fddb
Merge branch 'pandas-dev:main' into main
yqyqyq-W Nov 16, 2022
2a70a16
Add test for failure on DataFrame
yqyqyq-W Nov 16, 2022
bdd9a26
Add test for failure on DataFrame
yqyqyq-W Nov 16, 2022
f83189c
Add test for failure on DataFrame
yqyqyq-W Nov 16, 2022
2f85075
Bug: Fix inconsistent behavior in searchsorted (#49620)
yqyqyq-W Nov 21, 2022
7558c3d
Add test for failure on DataFrame
yqyqyq-W Nov 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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***

Expand Down
7 changes: 7 additions & 0 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/series/methods/test_searchsorted.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import numpy as np
import pytest

import pandas as pd
from pandas import (
Series,
Timestamp,
Expand Down Expand Up @@ -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)