Skip to content

fix isin method for series #52746

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

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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: 1 addition & 1 deletion doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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***

Expand Down
7 changes: 7 additions & 0 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
PandasDtype,
)
from pandas.core.dtypes.generic import (
ABCDataFrame,
ABCDatetimeArray,
ABCExtensionArray,
ABCIndex,
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------
Expand Down
4 changes: 3 additions & 1 deletion pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 6 additions & 4 deletions pandas/tests/test_algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down