Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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):
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.

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 your previous feedback on my PR . I have addressed your suggestions and made the improvements accordingly. I would love to get your thoughts on the updated changes. Thanks!

# 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")
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([])
tm.assert_index_equal(result2, expected2)
Loading