Skip to content

Commit 980ab6b

Browse files
authored
Test messages test period (#31824)
1 parent f04d913 commit 980ab6b

File tree

1 file changed

+55
-29
lines changed

1 file changed

+55
-29
lines changed

pandas/tests/arithmetic/test_period.py

+55-29
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,17 @@ def test_eq_integer_disallowed(self, other):
153153
result = idx == other
154154

155155
tm.assert_numpy_array_equal(result, expected)
156-
157-
with pytest.raises(TypeError):
156+
msg = (
157+
r"(:?Invalid comparison between dtype=period\[D\] and .*)"
158+
r"|(:?Cannot compare type Period with type .*)"
159+
)
160+
with pytest.raises(TypeError, match=msg):
158161
idx < other
159-
with pytest.raises(TypeError):
162+
with pytest.raises(TypeError, match=msg):
160163
idx > other
161-
with pytest.raises(TypeError):
164+
with pytest.raises(TypeError, match=msg):
162165
idx <= other
163-
with pytest.raises(TypeError):
166+
with pytest.raises(TypeError, match=msg):
164167
idx >= other
165168

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

593-
with pytest.raises(TypeError):
597+
with pytest.raises(TypeError, match=msg):
594598
rng += other
595599

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

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

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

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

703-
with pytest.raises(TypeError):
709+
msg = (
710+
r"(:?cannot add PeriodArray and .*)"
711+
r"|(:?cannot subtract .* from (:?a\s)?.*)"
712+
r"|(:?unsupported operand type\(s\) for \+: .* and .*)"
713+
)
714+
with pytest.raises(TypeError, match=msg):
704715
rng + other
705-
with pytest.raises(TypeError):
716+
with pytest.raises(TypeError, match=msg):
706717
other + rng
707-
with pytest.raises(TypeError):
718+
with pytest.raises(TypeError, match=msg):
708719
rng - other
709-
with pytest.raises(TypeError):
720+
with pytest.raises(TypeError, match=msg):
710721
other - rng
711722

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

720-
with pytest.raises(IncompatibleFrequency):
731+
msg = r"Input has different freq=None from PeriodArray\(freq=Q-DEC\)"
732+
with pytest.raises(IncompatibleFrequency, match=msg):
721733
rng + tdarr
722-
with pytest.raises(IncompatibleFrequency):
734+
with pytest.raises(IncompatibleFrequency, match=msg):
723735
tdarr + rng
724736

725-
with pytest.raises(IncompatibleFrequency):
737+
with pytest.raises(IncompatibleFrequency, match=msg):
726738
rng - tdarr
727-
with pytest.raises(TypeError):
739+
msg = r"cannot subtract PeriodArray from timedelta64\[ns\]"
740+
with pytest.raises(TypeError, match=msg):
728741
tdarr - rng
729742

730743
def test_pi_add_sub_td64_array_tick(self):
@@ -751,10 +764,11 @@ def test_pi_add_sub_td64_array_tick(self):
751764
result = rng - tdarr
752765
tm.assert_index_equal(result, expected)
753766

754-
with pytest.raises(TypeError):
767+
msg = r"cannot subtract .* from .*"
768+
with pytest.raises(TypeError, match=msg):
755769
tdarr - rng
756770

757-
with pytest.raises(TypeError):
771+
with pytest.raises(TypeError, match=msg):
758772
tdi - rng
759773

760774
# -----------------------------------------------------------------
@@ -783,10 +797,11 @@ def test_pi_add_offset_array(self, box):
783797
unanchored = np.array([pd.offsets.Hour(n=1), pd.offsets.Minute(n=-2)])
784798
# addition/subtraction ops with incompatible offsets should issue
785799
# a PerformanceWarning and _then_ raise a TypeError.
786-
with pytest.raises(IncompatibleFrequency):
800+
msg = r"Input cannot be converted to Period\(freq=Q-DEC\)"
801+
with pytest.raises(IncompatibleFrequency, match=msg):
787802
with tm.assert_produces_warning(PerformanceWarning):
788803
pi + unanchored
789-
with pytest.raises(IncompatibleFrequency):
804+
with pytest.raises(IncompatibleFrequency, match=msg):
790805
with tm.assert_produces_warning(PerformanceWarning):
791806
unanchored + pi
792807

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

812827
# addition/subtraction ops with anchored offsets should issue
813828
# a PerformanceWarning and _then_ raise a TypeError.
814-
with pytest.raises(IncompatibleFrequency):
829+
msg = r"Input has different freq=-1M from Period\(freq=Q-DEC\)"
830+
with pytest.raises(IncompatibleFrequency, match=msg):
815831
with tm.assert_produces_warning(PerformanceWarning):
816832
pi - anchored
817-
with pytest.raises(IncompatibleFrequency):
833+
with pytest.raises(IncompatibleFrequency, match=msg):
818834
with tm.assert_produces_warning(PerformanceWarning):
819835
anchored - pi
820836

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

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

930947
# ---------------------------------------------------------------
@@ -952,7 +969,11 @@ def test_pi_add_timedeltalike_minute_gt1(self, three_days):
952969
result = rng - other
953970
tm.assert_index_equal(result, expected)
954971

955-
with pytest.raises(TypeError):
972+
msg = (
973+
r"(:?bad operand type for unary -: 'PeriodArray')"
974+
r"|(:?cannot subtract PeriodArray from timedelta64\[[hD]\])"
975+
)
976+
with pytest.raises(TypeError, match=msg):
956977
other - rng
957978

958979
@pytest.mark.parametrize("freqstr", ["5ns", "5us", "5ms", "5s", "5T", "5h", "5d"])
@@ -974,8 +995,11 @@ def test_pi_add_timedeltalike_tick_gt1(self, three_days, freqstr):
974995
expected = pd.period_range(rng[0] - other, periods=6, freq=freqstr)
975996
result = rng - other
976997
tm.assert_index_equal(result, expected)
977-
978-
with pytest.raises(TypeError):
998+
msg = (
999+
r"(:?bad operand type for unary -: 'PeriodArray')"
1000+
r"|(:?cannot subtract PeriodArray from timedelta64\[[hD]\])"
1001+
)
1002+
with pytest.raises(TypeError, match=msg):
9791003
other - rng
9801004

9811005
def test_pi_add_iadd_timedeltalike_daily(self, three_days):
@@ -1110,7 +1134,8 @@ def test_parr_add_sub_td64_nat(self, box_with_array, transpose):
11101134
tm.assert_equal(result, expected)
11111135
result = obj - other
11121136
tm.assert_equal(result, expected)
1113-
with pytest.raises(TypeError):
1137+
msg = r"cannot subtract .* from .*"
1138+
with pytest.raises(TypeError, match=msg):
11141139
other - obj
11151140

11161141
@pytest.mark.parametrize(
@@ -1133,7 +1158,8 @@ def test_parr_add_sub_tdt64_nat_array(self, box_with_array, other):
11331158
tm.assert_equal(result, expected)
11341159
result = obj - other
11351160
tm.assert_equal(result, expected)
1136-
with pytest.raises(TypeError):
1161+
msg = r"cannot subtract .* from .*"
1162+
with pytest.raises(TypeError, match=msg):
11371163
other - obj
11381164

11391165
# ---------------------------------------------------------------

0 commit comments

Comments
 (0)