diff --git a/pandas/_libs/tslibs/period.pyx b/pandas/_libs/tslibs/period.pyx index 878dbcfd99842..a64bf03b5c166 100644 --- a/pandas/_libs/tslibs/period.pyx +++ b/pandas/_libs/tslibs/period.pyx @@ -1569,16 +1569,7 @@ cdef class _Period: return PyObject_RichCompareBool(self.ordinal, other.ordinal, op) elif other is NaT: return _nat_scalar_rules[op] - # index/series like - elif hasattr(other, '_typ'): - return NotImplemented - else: - if op == Py_EQ: - return NotImplemented - elif op == Py_NE: - return NotImplemented - raise TypeError(f"Cannot compare type {type(self).__name__} " - f"with type {type(other).__name__}") + return NotImplemented def __hash__(self): return hash((self.ordinal, self.freqstr)) @@ -1654,13 +1645,7 @@ cdef class _Period: raise IncompatibleFrequency(msg) # GH 23915 - mul by base freq since __add__ is agnostic of n return (self.ordinal - other.ordinal) * self.freq.base - elif getattr(other, '_typ', None) == 'periodindex': - # GH#21314 PeriodIndex - Period returns an object-index - # of DateOffset objects, for which we cannot use __neg__ - # directly, so we have to apply it pointwise - return other.__sub__(self).map(lambda x: -x) - else: # pragma: no cover - return NotImplemented + return NotImplemented elif is_period_object(other): if self is NaT: return NaT diff --git a/pandas/tests/arithmetic/test_period.py b/pandas/tests/arithmetic/test_period.py index 0e8bec331ea3f..3b3f5cb90bb31 100644 --- a/pandas/tests/arithmetic/test_period.py +++ b/pandas/tests/arithmetic/test_period.py @@ -153,9 +153,11 @@ def test_eq_integer_disallowed(self, other): result = idx == other tm.assert_numpy_array_equal(result, expected) - msg = ( - r"(:?Invalid comparison between dtype=period\[D\] and .*)" - r"|(:?Cannot compare type Period with type .*)" + msg = "|".join( + [ + "not supported between instances of 'Period' and 'int'", + r"Invalid comparison between dtype=period\[D\] and ", + ] ) with pytest.raises(TypeError, match=msg): idx < other diff --git a/pandas/tests/scalar/period/test_period.py b/pandas/tests/scalar/period/test_period.py index 620fc1c006d93..01b61da099481 100644 --- a/pandas/tests/scalar/period/test_period.py +++ b/pandas/tests/scalar/period/test_period.py @@ -992,7 +992,8 @@ def test_comparison_invalid_type(self): assert not jan == 1 assert jan != 1 - msg = "Cannot compare type Period with type int" + int_or_per = "'(Period|int)'" + msg = f"not supported between instances of {int_or_per} and {int_or_per}" for left, right in [(jan, 1), (1, jan)]: with pytest.raises(TypeError, match=msg):