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 20 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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 array-like or scalar, "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add "1D" here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added 1-D. In fact, n-D object except DataFrame works well with this function. As long as we don't call pd.array(n-d DataFrame).

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
9 changes: 9 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,10 @@ 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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you de-capitalize "DataFrame" here, and add a comment # GH#49620 on the nex tline

ser = Series([1, 2, 3, 4, 5])
vals = pd.DataFrame([[1, 2], [3, 4]])
msg = "Value must be array-like or scalar, DataFrame is not supported"
with pytest.raises(ValueError, match=msg):
ser.searchsorted(vals)