Skip to content

Commit f1a7363

Browse files
committed
Refactor PeriodIndex.convert_can_do_setop to improve exception handling
1 parent bdf98c4 commit f1a7363

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

pandas/core/indexes/period.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -535,18 +535,14 @@ def shift(self, periods: int = 1, freq=None) -> Self:
535535
return self + periods
536536

537537
def _convert_can_do_setop(self, other):
538-
try:
539-
if isinstance(other, Index):
540-
# Handle empty cases
541-
if other.empty:
542-
return super()._convert_can_do_setop(other)
543-
544-
# Convert non-PeriodIndex to PeriodIndex
545-
if not isinstance(other, PeriodIndex):
546-
other = PeriodIndex(other, freq=self.freq)
538+
if not isinstance(other, Index) or isinstance(other, PeriodIndex):
539+
return super()._convert_can_do_setop(other)
547540

541+
# Convert non-PeriodIndex to PeriodIndex
542+
try:
543+
other = PeriodIndex(other, freq=self.freq)
548544
except (TypeError, ValueError):
549-
return super()._convert_can_do_setop(other)
545+
pass
550546

551547
return super()._convert_can_do_setop(other)
552548

0 commit comments

Comments
 (0)