Skip to content

Commit d5000df

Browse files
committed
feat(): ignore index logic for df.isin()
1 parent 5d30c83 commit d5000df

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

pandas/core/frame.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13920,18 +13920,36 @@ def to_period(
1392013920
setattr(new_obj, axis_name, new_ax)
1392113921
return new_obj
1392213922

13923-
def isin(self, values: Series | DataFrame | Sequence | Mapping) -> DataFrame:
13923+
def isin(
13924+
self,
13925+
values: Series | DataFrame | Sequence | Mapping,
13926+
ignore_index: bool = False,
13927+
) -> DataFrame:
1392413928
"""
1392513929
Whether each element in the DataFrame is contained in values.
1392613930
1392713931
Parameters
1392813932
----------
1392913933
values : iterable, Series, DataFrame or dict
1393013934
The result will only be true at a location if all the
13931-
labels match. If `values` is a Series, that's the index. If
13932-
`values` is a dict, the keys must be the column names,
13933-
which must match. If `values` is a DataFrame,
13934-
then both the index and column labels must match.
13935+
labels match.
13936+
- If `values` is a Series, the index labels must match.
13937+
- If `values` is a dict, the keys must be column names,
13938+
which must match.
13939+
- If `values` is a DataFrame:
13940+
* When ``ignore_index=False`` (default), both the index
13941+
and column labels must match, and comparison is done
13942+
elementwise.
13943+
* When ``ignore_index=True``, only column labels must
13944+
match. Each element in the DataFrame is compared
13945+
against the set of values in the corresponding column
13946+
of ``values``, ignoring row index alignment.
13947+
13948+
ignore_index : bool, default False
13949+
*Only valid when `values` is a DataFrame.*
13950+
If True, ignore index alignment and simply check
13951+
if each value in each column occurs in the same
13952+
column of `values`.
1393513953
1393613954
Returns
1393713955
-------
@@ -14012,9 +14030,12 @@ def isin(self, values: Series | DataFrame | Sequence | Mapping) -> DataFrame:
1401214030
raise ValueError("cannot compute isin with a duplicate axis.")
1401314031
result = self.eq(values.reindex_like(self), axis="index")
1401414032
elif isinstance(values, DataFrame):
14015-
if not (values.columns.is_unique and values.index.is_unique):
14016-
raise ValueError("cannot compute isin with a duplicate axis.")
14017-
result = self.eq(values.reindex_like(self))
14033+
if ignore_index:
14034+
result = self.isin(values.to_dict("list"))
14035+
else:
14036+
if not (values.columns.is_unique and values.index.is_unique):
14037+
raise ValueError("cannot compute isin with a duplicate axis.")
14038+
result = self.eq(values.reindex_like(self))
1401814039
else:
1401914040
if not is_list_like(values):
1402014041
raise TypeError(

0 commit comments

Comments
 (0)