Skip to content

Test messages test period #31824

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
Feb 9, 2020
Merged
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
84 changes: 55 additions & 29 deletions pandas/tests/arithmetic/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,17 @@ def test_eq_integer_disallowed(self, other):
result = idx == other

tm.assert_numpy_array_equal(result, expected)

with pytest.raises(TypeError):
msg = (
r"(:?Invalid comparison between dtype=period\[D\] and .*)"
r"|(:?Cannot compare type Period with type .*)"
)
with pytest.raises(TypeError, match=msg):
idx < other
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
idx > other
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
idx <= other
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
idx >= other

def test_pi_cmp_period(self):
Expand Down Expand Up @@ -587,10 +590,11 @@ def test_parr_add_iadd_parr_raises(self, box_with_array):
# a set operation (union). This has since been changed to
# raise a TypeError. See GH#14164 and GH#13077 for historical
# reference.
with pytest.raises(TypeError):
msg = r"unsupported operand type\(s\) for \+: .* and .*"
with pytest.raises(TypeError, match=msg):
rng + other

with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
rng += other

def test_pi_sub_isub_pi(self):
Expand Down Expand Up @@ -625,7 +629,8 @@ def test_parr_sub_pi_mismatched_freq(self, box_with_array):
# TODO: parametrize over boxes for other?

rng = tm.box_expected(rng, box_with_array)
with pytest.raises(IncompatibleFrequency):
msg = r"Input has different freq=[HD] from PeriodArray\(freq=[DH]\)"
with pytest.raises(IncompatibleFrequency, match=msg):
rng - other

@pytest.mark.parametrize("n", [1, 2, 3, 4])
Expand Down Expand Up @@ -677,7 +682,8 @@ def test_parr_add_sub_float_raises(self, op, other, box_with_array):
dti = pd.DatetimeIndex(["2011-01-01", "2011-01-02"], freq="D")
pi = dti.to_period("D")
pi = tm.box_expected(pi, box_with_array)
with pytest.raises(TypeError):
msg = r"unsupported operand type\(s\) for [+-]: .* and .*"
with pytest.raises(TypeError, match=msg):
op(pi, other)

@pytest.mark.parametrize(
Expand All @@ -700,13 +706,18 @@ def test_parr_add_sub_invalid(self, other, box_with_array):
rng = pd.period_range("1/1/2000", freq="D", periods=3)
rng = tm.box_expected(rng, box_with_array)

with pytest.raises(TypeError):
msg = (
r"(:?cannot add PeriodArray and .*)"
r"|(:?cannot subtract .* from (:?a\s)?.*)"
r"|(:?unsupported operand type\(s\) for \+: .* and .*)"
)
with pytest.raises(TypeError, match=msg):
rng + other
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
other + rng
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
rng - other
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
other - rng

# -----------------------------------------------------------------
Expand All @@ -717,14 +728,16 @@ def test_pi_add_sub_td64_array_non_tick_raises(self):
tdi = pd.TimedeltaIndex(["-1 Day", "-1 Day", "-1 Day"])
tdarr = tdi.values

with pytest.raises(IncompatibleFrequency):
msg = r"Input has different freq=None from PeriodArray\(freq=Q-DEC\)"
with pytest.raises(IncompatibleFrequency, match=msg):
rng + tdarr
with pytest.raises(IncompatibleFrequency):
with pytest.raises(IncompatibleFrequency, match=msg):
tdarr + rng

with pytest.raises(IncompatibleFrequency):
with pytest.raises(IncompatibleFrequency, match=msg):
rng - tdarr
with pytest.raises(TypeError):
msg = r"cannot subtract PeriodArray from timedelta64\[ns\]"
with pytest.raises(TypeError, match=msg):
tdarr - rng

def test_pi_add_sub_td64_array_tick(self):
Expand All @@ -751,10 +764,11 @@ def test_pi_add_sub_td64_array_tick(self):
result = rng - tdarr
tm.assert_index_equal(result, expected)

with pytest.raises(TypeError):
msg = r"cannot subtract .* from .*"
with pytest.raises(TypeError, match=msg):
tdarr - rng

with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
tdi - rng

# -----------------------------------------------------------------
Expand Down Expand Up @@ -783,10 +797,11 @@ def test_pi_add_offset_array(self, box):
unanchored = np.array([pd.offsets.Hour(n=1), pd.offsets.Minute(n=-2)])
# addition/subtraction ops with incompatible offsets should issue
# a PerformanceWarning and _then_ raise a TypeError.
with pytest.raises(IncompatibleFrequency):
msg = r"Input cannot be converted to Period\(freq=Q-DEC\)"
with pytest.raises(IncompatibleFrequency, match=msg):
with tm.assert_produces_warning(PerformanceWarning):
pi + unanchored
with pytest.raises(IncompatibleFrequency):
with pytest.raises(IncompatibleFrequency, match=msg):
with tm.assert_produces_warning(PerformanceWarning):
unanchored + pi

Expand All @@ -811,10 +826,11 @@ def test_pi_sub_offset_array(self, box):

# addition/subtraction ops with anchored offsets should issue
# a PerformanceWarning and _then_ raise a TypeError.
with pytest.raises(IncompatibleFrequency):
msg = r"Input has different freq=-1M from Period\(freq=Q-DEC\)"
with pytest.raises(IncompatibleFrequency, match=msg):
with tm.assert_produces_warning(PerformanceWarning):
pi - anchored
with pytest.raises(IncompatibleFrequency):
with pytest.raises(IncompatibleFrequency, match=msg):
with tm.assert_produces_warning(PerformanceWarning):
anchored - pi

Expand Down Expand Up @@ -924,7 +940,8 @@ def test_pi_sub_intarray(self, int_holder):
expected = pd.PeriodIndex([pd.Period("2014Q1"), pd.Period("NaT")])
tm.assert_index_equal(result, expected)

with pytest.raises(TypeError):
msg = r"bad operand type for unary -: 'PeriodArray'"
with pytest.raises(TypeError, match=msg):
other - pi

# ---------------------------------------------------------------
Expand Down Expand Up @@ -952,7 +969,11 @@ def test_pi_add_timedeltalike_minute_gt1(self, three_days):
result = rng - other
tm.assert_index_equal(result, expected)

with pytest.raises(TypeError):
msg = (
r"(:?bad operand type for unary -: 'PeriodArray')"
r"|(:?cannot subtract PeriodArray from timedelta64\[[hD]\])"
)
with pytest.raises(TypeError, match=msg):
other - rng

@pytest.mark.parametrize("freqstr", ["5ns", "5us", "5ms", "5s", "5T", "5h", "5d"])
Expand All @@ -974,8 +995,11 @@ def test_pi_add_timedeltalike_tick_gt1(self, three_days, freqstr):
expected = pd.period_range(rng[0] - other, periods=6, freq=freqstr)
result = rng - other
tm.assert_index_equal(result, expected)

with pytest.raises(TypeError):
msg = (
r"(:?bad operand type for unary -: 'PeriodArray')"
r"|(:?cannot subtract PeriodArray from timedelta64\[[hD]\])"
)
with pytest.raises(TypeError, match=msg):
other - rng

def test_pi_add_iadd_timedeltalike_daily(self, three_days):
Expand Down Expand Up @@ -1110,7 +1134,8 @@ def test_parr_add_sub_td64_nat(self, box_with_array, transpose):
tm.assert_equal(result, expected)
result = obj - other
tm.assert_equal(result, expected)
with pytest.raises(TypeError):
msg = r"cannot subtract .* from .*"
with pytest.raises(TypeError, match=msg):
other - obj

@pytest.mark.parametrize(
Expand All @@ -1133,7 +1158,8 @@ def test_parr_add_sub_tdt64_nat_array(self, box_with_array, other):
tm.assert_equal(result, expected)
result = obj - other
tm.assert_equal(result, expected)
with pytest.raises(TypeError):
msg = r"cannot subtract .* from .*"
with pytest.raises(TypeError, match=msg):
other - obj

# ---------------------------------------------------------------
Expand Down