Skip to content

Commit 68fdacd

Browse files
tom-alcornjreback
authored andcommitted
BUG: Bug in interesection of non-monotonic non-unique indexes (GH8362)
1 parent b8d680d commit 68fdacd

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

doc/source/v0.15.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -985,3 +985,4 @@ Bug Fixes
985985
- Bug in OLS where running with "cluster" and "nw_lags" parameters did not work correctly, but also did not throw an error
986986
(:issue:`5884').
987987
- Bug in ``DataFrame.dropna`` that interpreted non-existent columns in the subset argument as the 'last column' (:issue:`8303`)
988+
- Bug in Index.intersection on non-monotonic non-unique indexes (:issue:`8362`).

pandas/core/index.py

+1
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,7 @@ def intersection(self, other):
12791279
except:
12801280
# duplicates
12811281
indexer = self.get_indexer_non_unique(other.values)[0].unique()
1282+
indexer = indexer[indexer != -1]
12821283

12831284
taken = self.take(indexer)
12841285
if self.name != other.name:

pandas/tests/test_index.py

+7
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,13 @@ def test_intersection(self):
532532
result3 = idx1.intersection(idx3)
533533
self.assertTrue(tm.equalContents(result3, expected3))
534534
self.assertEqual(result3.name, expected3.name)
535+
536+
# non-monotonic non-unique
537+
idx1 = Index(['A','B','A','C'])
538+
idx2 = Index(['B','D'])
539+
expected = Index(['B'], dtype='object')
540+
result = idx1.intersection(idx2)
541+
self.assertTrue(result.equals(expected))
535542

536543
def test_union(self):
537544
first = self.strIndex[5:20]

0 commit comments

Comments
 (0)