Skip to content

BUG: fix PeriodIndex.difference producing incorrect results #60992

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

Closed
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`)
Copy link
Member

Choose a reason for hiding this comment

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

are the affected Index objects object-dtype?

- 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):
if (
not isinstance(other, Index)
or other.empty
or isinstance(other, PeriodIndex)
):
return super()._convert_can_do_setop(other)

# Convert non-PeriodIndex to PeriodIndex
try:
other = PeriodIndex(other, freq=self.freq)
Copy link
Member

Choose a reason for hiding this comment

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

i dont think we should be doing this casting implicitly. the user should do it explicitly if they want it.

except (TypeError, ValueError):
pass

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")
Copy link
Member

Choose a reason for hiding this comment

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

i think this is wrong and the result in main is correct.

tm.assert_index_equal(result1, expected1)

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