-
-
Notifications
You must be signed in to change notification settings - Fork 18.8k
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
Changes from 3 commits
c301882
9f8512b
bdf98c4
f1a7363
7fea8d7
d49cbc0
7322f67
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A lot of this can be outside a try/except There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
There was a problem hiding this comment.
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?