Skip to content
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

BUG: fix PeriodIndex.difference producing incorrect results #60992

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ Indexing
- Bug in :meth:`DataFrame.__getitem__` returning modified columns when called with ``slice`` in Python 3.12 (:issue:`57500`)
- Bug in :meth:`DataFrame.from_records` throwing a ``ValueError`` when passed an empty list in ``index`` (:issue:`58594`)
- Bug in :meth:`MultiIndex.insert` when a new value inserted to a datetime-like level gets cast to ``NaT`` and fails indexing (:issue:`60388`)
- Bug in :meth:`PeriodIndex.difference` producing incorrect results when operating between :class:`PeriodIndex` and :class:`Index` objects (:issue:`58971`)
- Bug in printing :attr:`Index.names` and :attr:`MultiIndex.levels` would not escape single quotes (:issue:`60190`)

Missing
Expand Down
16 changes: 16 additions & 0 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,22 @@ def shift(self, periods: int = 1, freq=None) -> Self:
)
return self + periods

def _convert_can_do_setop(self, other):
try:
if isinstance(other, Index):
Copy link
Member

Choose a reason for hiding this comment

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

A lot of this can be outside a try/except

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jbrockmendel Thank you for the review, but before submitting the revision, I would like to confirm with you whether you think this modification direction is correct.

# Handle empty cases
if other.empty:
return super()._convert_can_do_setop(other)

# Convert non-PeriodIndex to PeriodIndex
if not isinstance(other, PeriodIndex):
other = PeriodIndex(other, freq=self.freq)

except (TypeError, ValueError):
return super()._convert_can_do_setop(other)

return super()._convert_can_do_setop(other)


def period_range(
start=None,
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/indexes/period/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,16 @@ def test_union_duplicates(self):
freq="D",
)
tm.assert_index_equal(result, expected)

def test_difference_periodindex_with_index(self):
# GH#58971
index1 = period_range("2022-01", periods=5, freq="M")
index2 = pd.Index(["2022-02", "2022-03"])

result1 = index1.difference(index2)
expected1 = PeriodIndex(["2022-01", "2022-04", "2022-05"], freq="M")
tm.assert_index_equal(result1, expected1)

result2 = index2.difference(index1)
expected2 = pd.Index([])
tm.assert_index_equal(result2, expected2)
Loading