Skip to content

ENH: Period comparisons with mismatched freq use py3 behavior #39274

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 3 commits into from
Jan 20, 2021
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ I/O

Period
^^^^^^

- Comparisons of :class:`Period` objects or :class:`Index`, :class:`Series`, or :class:`DataFrame` with mismatched ``PeriodDtype`` now behave like other mismatched-type comparisons, returning ``False`` for equals, ``True`` for not-equal, and raising ``TypeError`` for inequality checks (:issue:`??`)
-
-

Expand Down
4 changes: 4 additions & 0 deletions pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1541,6 +1541,10 @@ cdef class _Period(PeriodMixin):
def __richcmp__(self, other, op):
if is_period_object(other):
if other.freq != self.freq:
if op == Py_EQ:
return False
elif op == Py_NE:
return True
msg = DIFFERENT_FREQ.format(cls=type(self).__name__,
own_freq=self.freqstr,
other_freq=other.freqstr)
Expand Down
7 changes: 4 additions & 3 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from pandas._libs import algos, lib
from pandas._libs.tslibs import (
BaseOffset,
IncompatibleFrequency,
NaT,
NaTType,
Period,
Expand Down Expand Up @@ -441,7 +442,7 @@ def _validate_comparison_value(self, other):
try:
# GH#18435 strings get a pass from tzawareness compat
other = self._scalar_from_string(other)
except ValueError:
except (ValueError, IncompatibleFrequency):
# failed to parse as Timestamp/Timedelta/Period
raise InvalidComparison(other)

Expand All @@ -451,7 +452,7 @@ def _validate_comparison_value(self, other):
other = self._scalar_type(other) # type: ignore[call-arg]
try:
self._check_compatible_with(other)
except TypeError as err:
except (TypeError, IncompatibleFrequency) as err:
# e.g. tzawareness mismatch
raise InvalidComparison(other) from err

Expand All @@ -465,7 +466,7 @@ def _validate_comparison_value(self, other):
try:
other = self._validate_listlike(other, allow_object=True)
self._check_compatible_with(other)
except TypeError as err:
except (TypeError, IncompatibleFrequency) as err:
if is_object_dtype(getattr(other, "dtype", None)):
# We will have to operate element-wise
pass
Expand Down
31 changes: 16 additions & 15 deletions pandas/tests/arithmetic/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,38 +272,38 @@ def test_parr_cmp_pi(self, freq, box_with_array):
tm.assert_equal(base <= idx, exp)

@pytest.mark.parametrize("freq", ["M", "2M", "3M"])
def test_parr_cmp_pi_mismatched_freq_raises(self, freq, box_with_array):
def test_parr_cmp_pi_mismatched_freq(self, freq, box_with_array):
# GH#13200
# different base freq
base = PeriodIndex(["2011-01", "2011-02", "2011-03", "2011-04"], freq=freq)
base = tm.box_expected(base, box_with_array)

msg = "Input has different freq=A-DEC from "
with pytest.raises(IncompatibleFrequency, match=msg):
msg = rf"Invalid comparison between dtype=period\[{freq}\] and Period"
with pytest.raises(TypeError, match=msg):
base <= Period("2011", freq="A")

with pytest.raises(IncompatibleFrequency, match=msg):
with pytest.raises(TypeError, match=msg):
Period("2011", freq="A") >= base

# TODO: Could parametrize over boxes for idx?
idx = PeriodIndex(["2011", "2012", "2013", "2014"], freq="A")
rev_msg = r"Input has different freq=(M|2M|3M) from PeriodArray\(freq=A-DEC\)"
rev_msg = r"Invalid comparison between dtype=period\[A-DEC\] and PeriodArray"
idx_msg = rev_msg if box_with_array in [tm.to_array, pd.array] else msg
with pytest.raises(IncompatibleFrequency, match=idx_msg):
with pytest.raises(TypeError, match=idx_msg):
base <= idx

# Different frequency
msg = "Input has different freq=4M from "
with pytest.raises(IncompatibleFrequency, match=msg):
msg = rf"Invalid comparison between dtype=period\[{freq}\] and Period"
with pytest.raises(TypeError, match=msg):
base <= Period("2011", freq="4M")

with pytest.raises(IncompatibleFrequency, match=msg):
with pytest.raises(TypeError, match=msg):
Period("2011", freq="4M") >= base

idx = PeriodIndex(["2011", "2012", "2013", "2014"], freq="4M")
rev_msg = r"Input has different freq=(M|2M|3M) from PeriodArray\(freq=4M\)"
rev_msg = r"Invalid comparison between dtype=period\[4M\] and PeriodArray"
idx_msg = rev_msg if box_with_array in [tm.to_array, pd.array] else msg
with pytest.raises(IncompatibleFrequency, match=idx_msg):
with pytest.raises(TypeError, match=idx_msg):
base <= idx

@pytest.mark.parametrize("freq", ["M", "2M", "3M"])
Expand Down Expand Up @@ -354,12 +354,13 @@ def test_pi_cmp_nat_mismatched_freq_raises(self, freq):
idx1 = PeriodIndex(["2011-01", "2011-02", "NaT", "2011-05"], freq=freq)

diff = PeriodIndex(["2011-02", "2011-01", "2011-04", "NaT"], freq="4M")
msg = "Input has different freq=4M from Period(Array|Index)"
with pytest.raises(IncompatibleFrequency, match=msg):
msg = rf"Invalid comparison between dtype=period\[{freq}\] and PeriodArray"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prob should either rename the tests slightly or separate out the == case (and test != as well)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will rename for now. i think we may be able to re-use a helper from the dt64 tests, will take a look before long

with pytest.raises(TypeError, match=msg):
idx1 > diff

with pytest.raises(IncompatibleFrequency, match=msg):
idx1 == diff
result = idx1 == diff
expected = np.array([False, False, False, False], dtype=bool)
tm.assert_numpy_array_equal(result, expected)

# TODO: De-duplicate with test_pi_cmp_nat
@pytest.mark.parametrize("dtype", [object, None])
Expand Down
10 changes: 3 additions & 7 deletions pandas/tests/scalar/period/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ def test_construction(self):
i4 = Period("2005", freq="M")
i5 = Period("2005", freq="m")

msg = r"Input has different freq=M from Period\(freq=A-DEC\)"
with pytest.raises(IncompatibleFrequency, match=msg):
i1 != i4
assert i1 != i4
assert i4 == i5

i1 = Period.now("Q")
Expand Down Expand Up @@ -1071,11 +1069,9 @@ def test_comparison_mismatched_freq(self):
jan = Period("2000-01", "M")
day = Period("2012-01-01", "D")

assert not jan == day
assert jan != day
msg = r"Input has different freq=D from Period\(freq=M\)"
with pytest.raises(IncompatibleFrequency, match=msg):
jan == day
with pytest.raises(IncompatibleFrequency, match=msg):
jan != day
with pytest.raises(IncompatibleFrequency, match=msg):
jan < day
with pytest.raises(IncompatibleFrequency, match=msg):
Expand Down