Skip to content

Commit f08984e

Browse files
authored
CLN: avoid _typ checks in Period ops (#34007)
1 parent 0698dd8 commit f08984e

File tree

3 files changed

+9
-21
lines changed

3 files changed

+9
-21
lines changed

pandas/_libs/tslibs/period.pyx

+2-17
Original file line numberDiff line numberDiff line change
@@ -1568,16 +1568,7 @@ cdef class _Period:
15681568
return PyObject_RichCompareBool(self.ordinal, other.ordinal, op)
15691569
elif other is NaT:
15701570
return _nat_scalar_rules[op]
1571-
# index/series like
1572-
elif hasattr(other, '_typ'):
1573-
return NotImplemented
1574-
else:
1575-
if op == Py_EQ:
1576-
return NotImplemented
1577-
elif op == Py_NE:
1578-
return NotImplemented
1579-
raise TypeError(f"Cannot compare type {type(self).__name__} "
1580-
f"with type {type(other).__name__}")
1571+
return NotImplemented
15811572

15821573
def __hash__(self):
15831574
return hash((self.ordinal, self.freqstr))
@@ -1653,13 +1644,7 @@ cdef class _Period:
16531644
raise IncompatibleFrequency(msg)
16541645
# GH 23915 - mul by base freq since __add__ is agnostic of n
16551646
return (self.ordinal - other.ordinal) * self.freq.base
1656-
elif getattr(other, '_typ', None) == 'periodindex':
1657-
# GH#21314 PeriodIndex - Period returns an object-index
1658-
# of DateOffset objects, for which we cannot use __neg__
1659-
# directly, so we have to apply it pointwise
1660-
return other.__sub__(self).map(lambda x: -x)
1661-
else: # pragma: no cover
1662-
return NotImplemented
1647+
return NotImplemented
16631648
elif is_period_object(other):
16641649
if self is NaT:
16651650
return NaT

pandas/tests/arithmetic/test_period.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,11 @@ def test_eq_integer_disallowed(self, other):
153153
result = idx == other
154154

155155
tm.assert_numpy_array_equal(result, expected)
156-
msg = (
157-
r"(:?Invalid comparison between dtype=period\[D\] and .*)"
158-
r"|(:?Cannot compare type Period with type .*)"
156+
msg = "|".join(
157+
[
158+
"not supported between instances of 'Period' and 'int'",
159+
r"Invalid comparison between dtype=period\[D\] and ",
160+
]
159161
)
160162
with pytest.raises(TypeError, match=msg):
161163
idx < other

pandas/tests/scalar/period/test_period.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,8 @@ def test_comparison_invalid_type(self):
992992
assert not jan == 1
993993
assert jan != 1
994994

995-
msg = "Cannot compare type Period with type int"
995+
int_or_per = "'(Period|int)'"
996+
msg = f"not supported between instances of {int_or_per} and {int_or_per}"
996997
for left, right in [(jan, 1), (1, jan)]:
997998

998999
with pytest.raises(TypeError, match=msg):

0 commit comments

Comments
 (0)