Skip to content

Commit b2f6b70

Browse files
authored
BUG: Avoid duplicates in DatetimeIndex.intersection (#38196)
1 parent 8a15ae9 commit b2f6b70

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

pandas/core/indexes/datetimelike.py

+2
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,8 @@ def intersection(self, other, sort=False):
689689
other, _ = self._convert_can_do_setop(other)
690690

691691
if self.equals(other):
692+
if self.has_duplicates:
693+
return self.unique()._get_reconciled_name_object(other)
692694
return self._get_reconciled_name_object(other)
693695

694696
return self._intersection(other, sort=sort)

pandas/tests/indexes/datetimes/test_setops.py

+14
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,20 @@ def test_month_range_union_tz_dateutil(self, sort):
510510

511511
early_dr.union(late_dr, sort=sort)
512512

513+
@pytest.mark.parametrize("sort", [False, None])
514+
def test_intersection_duplicates(self, sort):
515+
# GH#38196
516+
idx1 = Index(
517+
[
518+
pd.Timestamp("2019-12-13"),
519+
pd.Timestamp("2019-12-12"),
520+
pd.Timestamp("2019-12-12"),
521+
]
522+
)
523+
result = idx1.intersection(idx1, sort=sort)
524+
expected = Index([pd.Timestamp("2019-12-13"), pd.Timestamp("2019-12-12")])
525+
tm.assert_index_equal(result, expected)
526+
513527

514528
class TestCustomDatetimeIndex:
515529
def setup_method(self, method):

0 commit comments

Comments
 (0)