Skip to content

BUG: Avoid duplicates in DatetimeIndex.intersection #38196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,8 +686,11 @@ def intersection(self, other, sort=False):
"""
self._validate_sort_keyword(sort)
self._assert_can_do_setop(other)
other, _ = self._convert_can_do_setop(other)

if self.equals(other):
if self.has_duplicates:
return self.unique()._get_reconciled_name_object(other)
return self._get_reconciled_name_object(other)
Copy link
Member

Choose a reason for hiding this comment

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

should we handle the self.equals(other) but non-unique case here? (like MultIIndex i think)

Copy link
Member Author

Choose a reason for hiding this comment

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

Should be consistent. best would probably be to handle this here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Handled it here now, and called _convert_can_do_setop. Have seen you moved the remaining part to _intersection in #38190, so I will keep this here as it is now that we are consistent


if len(self) == 0:
Expand Down
16 changes: 15 additions & 1 deletion pandas/tests/indexes/datetimes/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ def test_intersection_list(self):
values = [pd.Timestamp("2020-01-01"), pd.Timestamp("2020-02-01")]
idx = DatetimeIndex(values, name="a")
res = idx.intersection(values)
tm.assert_index_equal(res, idx.rename(None))
tm.assert_index_equal(res, idx)

def test_month_range_union_tz_pytz(self, sort):
from pytz import timezone
Expand Down Expand Up @@ -509,6 +509,20 @@ def test_month_range_union_tz_dateutil(self, sort):

early_dr.union(late_dr, sort=sort)

@pytest.mark.parametrize("sort", [False, None])
def test_intersection_duplicates(self, sort):
# GH#38196
idx1 = Index(
[
pd.Timestamp("2019-12-13"),
pd.Timestamp("2019-12-12"),
pd.Timestamp("2019-12-12"),
]
)
result = idx1.intersection(idx1, sort=sort)
expected = Index([pd.Timestamp("2019-12-13"), pd.Timestamp("2019-12-12")])
tm.assert_index_equal(result, expected)


class TestCustomDatetimeIndex:
def setup_method(self, method):
Expand Down