From ab4e85175aef98e79818a858e24bd15efabefff6 Mon Sep 17 00:00:00 2001 From: 4441only <94888665+yqyqyq-W@users.noreply.github.com> Date: Mon, 14 Nov 2022 23:44:39 -0500 Subject: [PATCH 01/16] Bug: Fix inconsistent behavior in searchsorted (#49620) Raise error when DataFrame is passed to searchsorted --- pandas/core/base.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pandas/core/base.py b/pandas/core/base.py index 3d06c1830cc53..f05ce8e4ecb5d 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -20,6 +20,7 @@ import numpy as np +import pandas as pd from pandas._libs import lib from pandas._typing import ( Axis, @@ -1270,6 +1271,13 @@ def searchsorted( sorter: NumpySorter = None, ) -> npt.NDArray[np.intp] | np.intp: + if isinstance(value, pd.DataFrame): + msg = ( + "Value must be 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 From 0b1b450d0993203b78a660c286b0bb568a620a7f Mon Sep 17 00:00:00 2001 From: 4441only <94888665+yqyqyq-W@users.noreply.github.com> Date: Tue, 15 Nov 2022 00:40:01 -0500 Subject: [PATCH 02/16] Update v2.0.0.rst --- doc/source/whatsnew/v2.0.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 032bcf09244e5..2ddfe2b491273 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -739,6 +739,7 @@ Metadata Other ^^^^^ +- Bug in :meth:`Series.searchsorted` inconsistent behavior when accepting `DataFrame` as parameter `value` (:issue:`49620`) .. ***DO NOT USE THIS SECTION*** From edd6099f7e6b6ebff4f6632fe60525dd87694d22 Mon Sep 17 00:00:00 2001 From: 4441only <94888665+yqyqyq-W@users.noreply.github.com> Date: Tue, 15 Nov 2022 00:45:33 -0500 Subject: [PATCH 03/16] Revert "Update v2.0.0.rst" This reverts commit 0b1b450d0993203b78a660c286b0bb568a620a7f. --- doc/source/whatsnew/v2.0.0.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 2ddfe2b491273..032bcf09244e5 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -739,7 +739,6 @@ Metadata Other ^^^^^ -- Bug in :meth:`Series.searchsorted` inconsistent behavior when accepting `DataFrame` as parameter `value` (:issue:`49620`) .. ***DO NOT USE THIS SECTION*** From 340de9a69a733f63778ba9cdc8a1cee53d184ccd Mon Sep 17 00:00:00 2001 From: 4441only <94888665+yqyqyq-W@users.noreply.github.com> Date: Tue, 15 Nov 2022 00:46:51 -0500 Subject: [PATCH 04/16] Update whatsnew --- doc/source/whatsnew/v2.0.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 032bcf09244e5..59155bfb38046 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -739,7 +739,7 @@ Metadata Other ^^^^^ - +- Bug in :meth:`Series.searchsorted` inconsistent behavior when accepting `DataFrame` as parameter `value` (:issue:`49620`) .. ***DO NOT USE THIS SECTION*** - From 06337f660d5808409c0014e12c4e44a0ae10327a Mon Sep 17 00:00:00 2001 From: 4441only <94888665+yqyqyq-W@users.noreply.github.com> Date: Tue, 15 Nov 2022 01:07:17 -0500 Subject: [PATCH 05/16] Update v2.0.0.rst --- doc/source/whatsnew/v2.0.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 59155bfb38046..650fad8c33e76 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -739,7 +739,7 @@ Metadata Other ^^^^^ -- Bug in :meth:`Series.searchsorted` inconsistent behavior when accepting `DataFrame` as parameter `value` (:issue:`49620`) +- Bug in :meth:`Series.searchsorted` inconsistent behavior when accepting :class:`DataFrame` as parameter ``value`` (:issue:`49620`) .. ***DO NOT USE THIS SECTION*** - From 090bd7928369b1ae70aa7bd9532ae1f6b735b6d3 Mon Sep 17 00:00:00 2001 From: 4441only <94888665+yqyqyq-W@users.noreply.github.com> Date: Tue, 15 Nov 2022 01:31:33 -0500 Subject: [PATCH 06/16] Update base.py --- pandas/core/base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/base.py b/pandas/core/base.py index f05ce8e4ecb5d..a68fccd074f70 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -20,7 +20,6 @@ import numpy as np -import pandas as pd from pandas._libs import lib from pandas._typing import ( Axis, @@ -56,6 +55,7 @@ remove_na_arraylike, ) +from pandas.core.frame import DataFrame from pandas.core import ( algorithms, nanops, @@ -1271,7 +1271,7 @@ def searchsorted( sorter: NumpySorter = None, ) -> npt.NDArray[np.intp] | np.intp: - if isinstance(value, pd.DataFrame): + if isinstance(value, DataFrame): msg = ( "Value must be array-like or scalar, " f"{type(value).__name__} is not supported" From 56b3fba6658ee388723aeaae8c729c98dd7ccb12 Mon Sep 17 00:00:00 2001 From: 4441only <94888665+yqyqyq-W@users.noreply.github.com> Date: Tue, 15 Nov 2022 01:32:33 -0500 Subject: [PATCH 07/16] Revert "Update base.py" This reverts commit 090bd7928369b1ae70aa7bd9532ae1f6b735b6d3. --- pandas/core/base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/base.py b/pandas/core/base.py index a68fccd074f70..f05ce8e4ecb5d 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -20,6 +20,7 @@ import numpy as np +import pandas as pd from pandas._libs import lib from pandas._typing import ( Axis, @@ -55,7 +56,6 @@ remove_na_arraylike, ) -from pandas.core.frame import DataFrame from pandas.core import ( algorithms, nanops, @@ -1271,7 +1271,7 @@ def searchsorted( sorter: NumpySorter = None, ) -> npt.NDArray[np.intp] | np.intp: - if isinstance(value, DataFrame): + if isinstance(value, pd.DataFrame): msg = ( "Value must be array-like or scalar, " f"{type(value).__name__} is not supported" From 12db40ddc22702501da75005ae75a4889e49fed2 Mon Sep 17 00:00:00 2001 From: 4441only <94888665+yqyqyq-W@users.noreply.github.com> Date: Tue, 15 Nov 2022 01:35:37 -0500 Subject: [PATCH 08/16] Bug: Fix inconsistent behavior in searchsorted (#49620) Raise error when DataFrame is passed to searchsorted --- pandas/core/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/base.py b/pandas/core/base.py index f05ce8e4ecb5d..71062f32099e7 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -20,7 +20,6 @@ import numpy as np -import pandas as pd from pandas._libs import lib from pandas._typing import ( Axis, @@ -56,6 +55,7 @@ remove_na_arraylike, ) +import pandas as pd from pandas.core import ( algorithms, nanops, From 34ecabcaa1439a9c58337d9157b88892a68b72fc Mon Sep 17 00:00:00 2001 From: 4441only <94888665+yqyqyq-W@users.noreply.github.com> Date: Tue, 15 Nov 2022 11:11:10 -0500 Subject: [PATCH 09/16] Update whatsnew --- doc/source/whatsnew/v2.0.0.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 650fad8c33e76..da1c8caed1940 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*** - From 023a7c7465187a9caaa5aaead3cce7bd3c2aba84 Mon Sep 17 00:00:00 2001 From: 4441only <94888665+yqyqyq-W@users.noreply.github.com> Date: Tue, 15 Nov 2022 20:36:36 -0500 Subject: [PATCH 10/16] Bug: Fix inconsistent behavior in searchsorted (#49620) Raise error when DataFrame is passed to searchsorted --- pandas/core/base.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/core/base.py b/pandas/core/base.py index 71062f32099e7..1ba9e56106a45 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -55,7 +55,6 @@ remove_na_arraylike, ) -import pandas as pd from pandas.core import ( algorithms, nanops, @@ -1271,7 +1270,7 @@ def searchsorted( sorter: NumpySorter = None, ) -> npt.NDArray[np.intp] | np.intp: - if isinstance(value, pd.DataFrame): + if isinstance(value, ABCDataFrame): msg = ( "Value must be array-like or scalar, " f"{type(value).__name__} is not supported" From 9037cc983bfb04bf955d0e391f8c58638dd0e526 Mon Sep 17 00:00:00 2001 From: 4441only <94888665+yqyqyq-W@users.noreply.github.com> Date: Tue, 15 Nov 2022 20:37:00 -0500 Subject: [PATCH 11/16] Add test for failure on DataFrame --- pandas/tests/series/methods/test_searchsorted.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pandas/tests/series/methods/test_searchsorted.py b/pandas/tests/series/methods/test_searchsorted.py index 5a7eb3f8cfc97..4c902361b3649 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,13 @@ 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): + ser = Series([1, 2, 3, 4]) + 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) From 2a70a16a011f0e78c1ef23971d1a06f8ac34ba29 Mon Sep 17 00:00:00 2001 From: 4441only <94888665+yqyqyq-W@users.noreply.github.com> Date: Wed, 16 Nov 2022 00:54:14 -0500 Subject: [PATCH 12/16] Add test for failure on DataFrame --- pandas/tests/series/methods/test_searchsorted.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/series/methods/test_searchsorted.py b/pandas/tests/series/methods/test_searchsorted.py index 4c902361b3649..f518c29f4ecd0 100644 --- a/pandas/tests/series/methods/test_searchsorted.py +++ b/pandas/tests/series/methods/test_searchsorted.py @@ -69,7 +69,7 @@ def test_searchsorted_sorter(self): tm.assert_numpy_array_equal(res, exp) def test_searchsorted_Dataframe_fail(self): - ser = Series([1, 2, 3, 4]) + ser = Series([1, 2, 3, 4, 5]) vals = pd.DataFrame([[1, 2], [3, 4]]) msg = ( "Value must be array-like or scalar, " From bdd9a266f17403c22fcdafd9208e793b66fbd1b7 Mon Sep 17 00:00:00 2001 From: 4441only <94888665+yqyqyq-W@users.noreply.github.com> Date: Wed, 16 Nov 2022 01:16:49 -0500 Subject: [PATCH 13/16] Add test for failure on DataFrame --- pandas/tests/series/methods/test_searchsorted.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pandas/tests/series/methods/test_searchsorted.py b/pandas/tests/series/methods/test_searchsorted.py index f518c29f4ecd0..3b439376a709b 100644 --- a/pandas/tests/series/methods/test_searchsorted.py +++ b/pandas/tests/series/methods/test_searchsorted.py @@ -71,9 +71,6 @@ def test_searchsorted_sorter(self): def test_searchsorted_Dataframe_fail(self): 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" - ) + msg = "Value must be array-like or scalar, " "DataFrame is not supported" with pytest.raises(ValueError, match=msg): ser.searchsorted(vals) From f83189ca2345c2cd2320dcc8cfdc3cd2deb5aef8 Mon Sep 17 00:00:00 2001 From: 4441only <94888665+yqyqyq-W@users.noreply.github.com> Date: Wed, 16 Nov 2022 10:52:27 -0500 Subject: [PATCH 14/16] Add test for failure on DataFrame --- pandas/tests/series/methods/test_searchsorted.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/series/methods/test_searchsorted.py b/pandas/tests/series/methods/test_searchsorted.py index 3b439376a709b..e17d926bfdb3f 100644 --- a/pandas/tests/series/methods/test_searchsorted.py +++ b/pandas/tests/series/methods/test_searchsorted.py @@ -71,6 +71,6 @@ def test_searchsorted_sorter(self): def test_searchsorted_Dataframe_fail(self): 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" + msg = "Value must be array-like or scalar, DataFrame is not supported" with pytest.raises(ValueError, match=msg): ser.searchsorted(vals) From 2f8507569d4e24b1a8e5d57ed8bcbaca0e359cbe Mon Sep 17 00:00:00 2001 From: 4441only <94888665+yqyqyq-W@users.noreply.github.com> Date: Sun, 20 Nov 2022 22:53:33 -0500 Subject: [PATCH 15/16] Bug: Fix inconsistent behavior in searchsorted (#49620) --- pandas/core/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/base.py b/pandas/core/base.py index 1ba9e56106a45..9957535020563 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -1272,7 +1272,7 @@ def searchsorted( if isinstance(value, ABCDataFrame): msg = ( - "Value must be array-like or scalar, " + "Value must be 1-D array-like or scalar, " f"{type(value).__name__} is not supported" ) raise ValueError(msg) From 7558c3d5d6ff325a6f089307feb1ec280a1e1b95 Mon Sep 17 00:00:00 2001 From: 4441only <94888665+yqyqyq-W@users.noreply.github.com> Date: Sun, 20 Nov 2022 22:53:46 -0500 Subject: [PATCH 16/16] Add test for failure on DataFrame --- pandas/tests/series/methods/test_searchsorted.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pandas/tests/series/methods/test_searchsorted.py b/pandas/tests/series/methods/test_searchsorted.py index e17d926bfdb3f..239496052b99b 100644 --- a/pandas/tests/series/methods/test_searchsorted.py +++ b/pandas/tests/series/methods/test_searchsorted.py @@ -68,9 +68,10 @@ def test_searchsorted_sorter(self): exp = np.array([0, 2], dtype=np.intp) tm.assert_numpy_array_equal(res, exp) - def test_searchsorted_Dataframe_fail(self): + 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 array-like or scalar, DataFrame is not supported" + msg = "Value must be 1-D array-like or scalar, DataFrame is not supported" with pytest.raises(ValueError, match=msg): ser.searchsorted(vals)