Skip to content

Commit 2f6ed00

Browse files
committed
feat(): ignore index logic for df.isin()
1 parent ce3298b commit 2f6ed00

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
@@ -14093,18 +14093,36 @@ def to_period(
1409314093
setattr(new_obj, axis_name, new_ax)
1409414094
return new_obj
1409514095

14096-
def isin(self, values: Series | DataFrame | Sequence | Mapping) -> DataFrame:
14096+
def isin(
14097+
self,
14098+
values: Series | DataFrame | Sequence | Mapping,
14099+
ignore_index: bool = False,
14100+
) -> DataFrame:
1409714101
"""
1409814102
Whether each element in the DataFrame is contained in values.
1409914103
1410014104
Parameters
1410114105
----------
1410214106
values : iterable, Series, DataFrame or dict
1410314107
The result will only be true at a location if all the
14104-
labels match. If `values` is a Series, that's the index. If
14105-
`values` is a dict, the keys must be the column names,
14106-
which must match. If `values` is a DataFrame,
14107-
then both the index and column labels must match.
14108+
labels match.
14109+
- If `values` is a Series, the index labels must match.
14110+
- If `values` is a dict, the keys must be column names,
14111+
which must match.
14112+
- If `values` is a DataFrame:
14113+
* When ``ignore_index=False`` (default), both the index
14114+
and column labels must match, and comparison is done
14115+
elementwise.
14116+
* When ``ignore_index=True``, only column labels must
14117+
match. Each element in the DataFrame is compared
14118+
against the set of values in the corresponding column
14119+
of ``values``, ignoring row index alignment.
14120+
14121+
ignore_index : bool, default False
14122+
*Only valid when `values` is a DataFrame.*
14123+
If True, ignore index alignment and simply check
14124+
if each value in each column occurs in the same
14125+
column of `values`.
1410814126
1410914127
Returns
1411014128
-------
@@ -14185,9 +14203,12 @@ def isin(self, values: Series | DataFrame | Sequence | Mapping) -> DataFrame:
1418514203
raise ValueError("cannot compute isin with a duplicate axis.")
1418614204
result = self.eq(values.reindex_like(self), axis="index")
1418714205
elif isinstance(values, DataFrame):
14188-
if not (values.columns.is_unique and values.index.is_unique):
14189-
raise ValueError("cannot compute isin with a duplicate axis.")
14190-
result = self.eq(values.reindex_like(self))
14206+
if ignore_index:
14207+
result = self.isin(values.to_dict("list"))
14208+
else:
14209+
if not (values.columns.is_unique and values.index.is_unique):
14210+
raise ValueError("cannot compute isin with a duplicate axis.")
14211+
result = self.eq(values.reindex_like(self))
1419114212
else:
1419214213
if not is_list_like(values):
1419314214
raise TypeError(

0 commit comments

Comments
 (0)