Skip to content
Merged
Changes from 3 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
8 changes: 6 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2858,13 +2858,17 @@ def _intersection(self, other, sort=False):
indexer = algos.unique1d(Index(rvals).get_indexer_non_unique(lvals)[0])
indexer = indexer[indexer != -1]

result = other.take(indexer).unique()._values
result = other.take(indexer)
if not result.is_unique:
result = result.unique()._values
else:
result = result._values
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

im surprised this makes a difference. Index.unique should already be doing this check internally i guess

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jreback suggested to check for uniqueness before calling unique() to save a bit of time.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually @jbrockmendel makes a very good point here. Let's repurpose this PR to actually do a uniquess check internal to unique (of course this may only make sense for > 1000 elements or something).


if sort is None:
result = algos.safe_sort(result)

# Intersection has to be unique
assert algos.unique(result).shape == result.shape
assert Index(result).is_unique

return result

Expand Down