Skip to content

CLN: avoid _typ checks in Period ops #34007

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

Merged
merged 2 commits into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 2 additions & 17 deletions pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions pandas/tests/arithmetic/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/scalar/period/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down