Skip to content

Commit d83f3c4

Browse files
committed
Fix bug in index.union with duplicates and not a subset of each other
1 parent 1d5102c commit d83f3c4

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

pandas/core/indexes/base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2979,8 +2979,8 @@ def _union(self, other: Index, sort):
29792979
value_list.extend([x for x in rvals if x not in value_set])
29802980
return Index(value_list)._values # do type inference here
29812981

2982-
elif not other.is_unique and not self.is_unique:
2983-
# self and other both have duplicates
2982+
elif not other.is_unique:
2983+
# other has duplicates
29842984

29852985
# error: Argument 1 to "union_with_duplicates" has incompatible type
29862986
# "Union[ExtensionArray, ndarray]"; expected "ndarray"
@@ -2989,7 +2989,7 @@ def _union(self, other: Index, sort):
29892989
result = algos.union_with_duplicates(lvals, rvals) # type: ignore[arg-type]
29902990
return _maybe_try_sort(result, sort)
29912991

2992-
# Either other or self is not unique
2992+
# Self may have duplicates
29932993
# find indexes of things in "other" that are not in "self"
29942994
if self.is_unique:
29952995
indexer = self.get_indexer(other)

pandas/tests/indexes/test_setops.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,29 @@ def test_union_nan_in_both(dup):
579579
tm.assert_index_equal(result, expected)
580580

581581

582+
@pytest.mark.parametrize(
583+
"cls",
584+
[
585+
Int64Index,
586+
Float64Index,
587+
DatetimeIndex,
588+
TimedeltaIndex,
589+
lambda x: Index(x, dtype=object),
590+
],
591+
)
592+
def test_union_with_duplicate_index_not_subset_and_non_monotonic(cls):
593+
# GH#36289
594+
a = cls([1, 0, 0, 2])
595+
b = cls([0, 1])
596+
expected = cls([0, 0, 1, 2])
597+
598+
result = a.union(b)
599+
tm.assert_index_equal(result, expected)
600+
601+
result = a.union(b)
602+
tm.assert_index_equal(result, expected)
603+
604+
582605
class TestSetOpsUnsorted:
583606
# These may eventually belong in a dtype-specific test_setops, or
584607
# parametrized over a more general fixture

0 commit comments

Comments
 (0)