diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 7e9a918179714..f163d394be722 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -409,9 +409,9 @@ Other - Bug in :func:`assert_almost_equal` now throwing assertion error for two unequal sets (:issue:`51727`) - Bug in :func:`assert_frame_equal` checks category dtypes even when asked not to check index type (:issue:`52126`) - Bug in :meth:`DataFrame.reindex` with a ``fill_value`` that should be inferred with a :class:`ExtensionDtype` incorrectly inferring ``object`` dtype (:issue:`52586`) +- Bug in :meth:`Series.isin` when ``values`` is of type ``Dataframe`` throw a ``TypeError`` (:issue:`43460`) - Bug in :meth:`Series.map` when giving a callable to an empty series, the returned series had ``object`` dtype. It now keeps the original dtype (:issue:`52384`) - Bug in :meth:`Series.memory_usage` when ``deep=True`` throw an error with Series of objects and the returned value is incorrect, as it does not take into account GC corrections (:issue:`51858`) -- .. ***DO NOT USE THIS SECTION*** diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index 67b7dc0ac709d..d21e2c9552fd5 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -63,6 +63,7 @@ PandasDtype, ) from pandas.core.dtypes.generic import ( + ABCDataFrame, ABCDatetimeArray, ABCExtensionArray, ABCIndex, @@ -465,6 +466,12 @@ def isin(comps: AnyArrayLike, values: AnyArrayLike) -> npt.NDArray[np.bool_]: f"to isin(), you passed a [{type(values).__name__}]" ) + if isinstance(values, ABCDataFrame): + raise TypeError( + "only list-like objects are allowed to be passed " + f"to isin(), you passed a [{type(values).__name__}]" + ) + if not isinstance(values, (ABCIndex, ABCSeries, ABCExtensionArray, np.ndarray)): orig_values = list(values) values = _ensure_arraylike(orig_values) diff --git a/pandas/core/series.py b/pandas/core/series.py index adb16c2f2dd55..65628cb9f2541 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -5050,7 +5050,7 @@ def isin(self, values) -> Series: Raises ------ TypeError - * If `values` is a string + * If `values` is a string or a dataframe See Also -------- diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index e2c5ed2ea92b6..27f0295b50d27 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -2687,7 +2687,9 @@ def _get_numeric_subset_default(self): # Returns a boolean mask indicating where `self.data` has numerical columns. # Choosing a mask as opposed to the column names also works for # boolean column labels (GH47838). - return self.data.columns.isin(self.data.select_dtypes(include=np.number)) + return self.data.columns.isin( + self.data.select_dtypes(include=np.number).columns + ) @doc( name="background", diff --git a/pandas/tests/test_algos.py b/pandas/tests/test_algos.py index 58bdf3666caf4..b53a7d7292d2c 100644 --- a/pandas/tests/test_algos.py +++ b/pandas/tests/test_algos.py @@ -876,14 +876,16 @@ class TestIsin: def test_invalid(self): msg = ( r"only list-like objects are allowed to be passed to isin\(\), " - r"you passed a \[int\]" + r"you passed a" ) - with pytest.raises(TypeError, match=msg): + with pytest.raises(TypeError, match=f"{msg} \\[int\\]"): algos.isin(1, 1) - with pytest.raises(TypeError, match=msg): + with pytest.raises(TypeError, match=f"{msg} \\[int\\]"): algos.isin(1, [1]) - with pytest.raises(TypeError, match=msg): + with pytest.raises(TypeError, match=f"{msg} \\[int\\]"): algos.isin([1], 1) + with pytest.raises(TypeError, match=f"{msg} \\[DataFrame\\]"): + algos.isin([1], DataFrame({"Column": [1]})) def test_basic(self): result = algos.isin([1, 2], [1])