Skip to content

Commit 560cd29

Browse files
committed
feat(): ignore index logic for df.isin()
1 parent 6583c1d commit 560cd29

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
@@ -14051,18 +14051,36 @@ def to_period(
1405114051
setattr(new_obj, axis_name, new_ax)
1405214052
return new_obj
1405314053

14054-
def isin(self, values: Series | DataFrame | Sequence | Mapping) -> DataFrame:
14054+
def isin(
14055+
self,
14056+
values: Series | DataFrame | Sequence | Mapping,
14057+
ignore_index: bool = False,
14058+
) -> DataFrame:
1405514059
"""
1405614060
Whether each element in the DataFrame is contained in values.
1405714061
1405814062
Parameters
1405914063
----------
1406014064
values : iterable, Series, DataFrame or dict
1406114065
The result will only be true at a location if all the
14062-
labels match. If `values` is a Series, that's the index. If
14063-
`values` is a dict, the keys must be the column names,
14064-
which must match. If `values` is a DataFrame,
14065-
then both the index and column labels must match.
14066+
labels match.
14067+
- If `values` is a Series, the index labels must match.
14068+
- If `values` is a dict, the keys must be column names,
14069+
which must match.
14070+
- If `values` is a DataFrame:
14071+
* When ``ignore_index=False`` (default), both the index
14072+
and column labels must match, and comparison is done
14073+
elementwise.
14074+
* When ``ignore_index=True``, only column labels must
14075+
match. Each element in the DataFrame is compared
14076+
against the set of values in the corresponding column
14077+
of ``values``, ignoring row index alignment.
14078+
14079+
ignore_index : bool, default False
14080+
*Only valid when `values` is a DataFrame.*
14081+
If True, ignore index alignment and simply check
14082+
if each value in each column occurs in the same
14083+
column of `values`.
1406614084
1406714085
Returns
1406814086
-------
@@ -14143,9 +14161,12 @@ def isin(self, values: Series | DataFrame | Sequence | Mapping) -> DataFrame:
1414314161
raise ValueError("cannot compute isin with a duplicate axis.")
1414414162
result = self.eq(values.reindex_like(self), axis="index")
1414514163
elif isinstance(values, DataFrame):
14146-
if not (values.columns.is_unique and values.index.is_unique):
14147-
raise ValueError("cannot compute isin with a duplicate axis.")
14148-
result = self.eq(values.reindex_like(self))
14164+
if ignore_index:
14165+
result = self.isin(values.to_dict("list"))
14166+
else:
14167+
if not (values.columns.is_unique and values.index.is_unique):
14168+
raise ValueError("cannot compute isin with a duplicate axis.")
14169+
result = self.eq(values.reindex_like(self))
1414914170
else:
1415014171
if not is_list_like(values):
1415114172
raise TypeError(

0 commit comments

Comments
 (0)