diff --git a/pandas/_libs/period.pyx b/pandas/_libs/period.pyx index 72523a19b9595..5c5c10684551d 100644 --- a/pandas/_libs/period.pyx +++ b/pandas/_libs/period.pyx @@ -693,7 +693,12 @@ cdef class _Period(object): def __richcmp__(self, other, op): if is_period_object(other): - if other.freq != self.freq: + try: + if other.freq != self.freq: + msg = _DIFFERENT_FREQ.format(self.freqstr, other.freqstr) + raise IncompatibleFrequency(msg) + except SystemError: + # See GH#17112 in python3 msg = _DIFFERENT_FREQ.format(self.freqstr, other.freqstr) raise IncompatibleFrequency(msg) return PyObject_RichCompareBool(self.ordinal, other.ordinal, op) diff --git a/pandas/tests/scalar/test_period.py b/pandas/tests/scalar/test_period.py index 28d85c52604d9..2824d2aa266ff 100644 --- a/pandas/tests/scalar/test_period.py +++ b/pandas/tests/scalar/test_period.py @@ -1418,3 +1418,12 @@ def test_period_immutable(): freq = per.freq with pytest.raises(AttributeError): per.freq = 2 * freq + + +def test_comparison_catches_system_error(): + # see GH#17112 in py3 this comparison could raise SystemError instead + # of IncompatibleFrequency + per1 = pd.Period('2014Q1') + per2 = pd.Period('2015', freq='A') + with pytest.raises(period.IncompatibleFrequency): + per1 < per2