Skip to content
Open
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
37 changes: 29 additions & 8 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -13920,18 +13920,36 @@ def to_period(
setattr(new_obj, axis_name, new_ax)
return new_obj

def isin(self, values: Series | DataFrame | Sequence | Mapping) -> DataFrame:
def isin(
self,
values: Series | DataFrame | Sequence | Mapping,
ignore_index: bool = False,
) -> DataFrame:
"""
Whether each element in the DataFrame is contained in values.

Parameters
----------
values : iterable, Series, DataFrame or dict
The result will only be true at a location if all the
labels match. If `values` is a Series, that's the index. If
`values` is a dict, the keys must be the column names,
which must match. If `values` is a DataFrame,
then both the index and column labels must match.
labels match.
- If `values` is a Series, the index labels must match.
- If `values` is a dict, the keys must be column names,
which must match.
- If `values` is a DataFrame:
* When ``ignore_index=False`` (default), both the index
and column labels must match, and comparison is done
elementwise.
* When ``ignore_index=True``, only column labels must
match. Each element in the DataFrame is compared
against the set of values in the corresponding column
of ``values``, ignoring row index alignment.

ignore_index : bool, default False
*Only valid when `values` is a DataFrame.*
If True, ignore index alignment and simply check
if each value in each column occurs in the same
column of `values`.

Returns
-------
Expand Down Expand Up @@ -14012,9 +14030,12 @@ def isin(self, values: Series | DataFrame | Sequence | Mapping) -> DataFrame:
raise ValueError("cannot compute isin with a duplicate axis.")
result = self.eq(values.reindex_like(self), axis="index")
elif isinstance(values, DataFrame):
if not (values.columns.is_unique and values.index.is_unique):
raise ValueError("cannot compute isin with a duplicate axis.")
result = self.eq(values.reindex_like(self))
if ignore_index:
result = self.isin(values.to_dict("list"))
else:
if not (values.columns.is_unique and values.index.is_unique):
raise ValueError("cannot compute isin with a duplicate axis.")
result = self.eq(values.reindex_like(self))
else:
if not is_list_like(values):
raise TypeError(
Expand Down
Loading