Skip to content

Commit 914c390

Browse files
authored
add match to bare pyteset.raises() - arrays (pandas-dev#33010)
1 parent 0d46144 commit 914c390

File tree

3 files changed

+47
-26
lines changed

3 files changed

+47
-26
lines changed

pandas/tests/arithmetic/test_timedelta64.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -650,9 +650,9 @@ def test_tdi_add_overflow(self):
650650
# See GH#14068
651651
# preliminary test scalar analogue of vectorized tests below
652652
# TODO: Make raised error message more informative and test
653-
with pytest.raises(OutOfBoundsDatetime):
653+
with pytest.raises(OutOfBoundsDatetime, match="10155196800000000000"):
654654
pd.to_timedelta(106580, "D") + Timestamp("2000")
655-
with pytest.raises(OutOfBoundsDatetime):
655+
with pytest.raises(OutOfBoundsDatetime, match="10155196800000000000"):
656656
Timestamp("2000") + pd.to_timedelta(106580, "D")
657657

658658
_NaT = int(pd.NaT) + 1

pandas/tests/arrays/interval/test_ops.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def test_overlaps_interval_container(self, constructor, other_constructor):
5757
# TODO: modify this test when implemented
5858
interval_container = constructor.from_breaks(range(5))
5959
other_container = other_constructor.from_breaks(range(5))
60-
with pytest.raises(NotImplementedError):
60+
with pytest.raises(NotImplementedError, match="^$"):
6161
interval_container.overlaps(other_container)
6262

6363
def test_overlaps_na(self, constructor, start_shift):

pandas/tests/arrays/test_datetimelike.py

+44-23
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ def test_take(self):
9494

9595
tm.assert_index_equal(self.index_cls(result), expected)
9696

97+
@pytest.mark.parametrize("fill_value", [2, 2.0, pd.Timestamp.now().time])
98+
def test_take_fill_raises(self, fill_value):
99+
data = np.arange(10, dtype="i8") * 24 * 3600 * 10 ** 9
100+
101+
arr = self.array_cls._simple_new(data, freq="D")
102+
103+
msg = f"'fill_value' should be a {self.dtype}. Got '{fill_value}'"
104+
with pytest.raises(ValueError, match=msg):
105+
arr.take([0, 1], allow_fill=True, fill_value=fill_value)
106+
97107
def test_take_fill(self):
98108
data = np.arange(10, dtype="i8") * 24 * 3600 * 10 ** 9
99109

@@ -108,15 +118,6 @@ def test_take_fill(self):
108118
result = arr.take([-1, 1], allow_fill=True, fill_value=pd.NaT)
109119
assert result[0] is pd.NaT
110120

111-
with pytest.raises(ValueError):
112-
arr.take([0, 1], allow_fill=True, fill_value=2)
113-
114-
with pytest.raises(ValueError):
115-
arr.take([0, 1], allow_fill=True, fill_value=2.0)
116-
117-
with pytest.raises(ValueError):
118-
arr.take([0, 1], allow_fill=True, fill_value=pd.Timestamp.now().time)
119-
120121
def test_concat_same_type(self):
121122
data = np.arange(10, dtype="i8") * 24 * 3600 * 10 ** 9
122123

@@ -139,7 +140,8 @@ def test_unbox_scalar(self):
139140
result = arr._unbox_scalar(pd.NaT)
140141
assert isinstance(result, int)
141142

142-
with pytest.raises(ValueError):
143+
msg = f"'value' should be a {self.dtype.__name__}."
144+
with pytest.raises(ValueError, match=msg):
143145
arr._unbox_scalar("foo")
144146

145147
def test_check_compatible_with(self):
@@ -261,6 +263,7 @@ def test_shift_fill_int_deprecated(self):
261263
class TestDatetimeArray(SharedTests):
262264
index_cls = pd.DatetimeIndex
263265
array_cls = DatetimeArray
266+
dtype = pd.Timestamp
264267

265268
def test_round(self, tz_naive_fixture):
266269
# GH#24064
@@ -452,23 +455,28 @@ def test_take_fill_valid(self, datetime_index, tz_naive_fixture):
452455
result = arr.take([-1, 1], allow_fill=True, fill_value=now)
453456
assert result[0] == now
454457

455-
with pytest.raises(ValueError):
458+
msg = f"'fill_value' should be a {self.dtype}. Got '0 days 00:00:00'."
459+
with pytest.raises(ValueError, match=msg):
456460
# fill_value Timedelta invalid
457461
arr.take([-1, 1], allow_fill=True, fill_value=now - now)
458462

459-
with pytest.raises(ValueError):
463+
msg = f"'fill_value' should be a {self.dtype}. Got '2014Q1'."
464+
with pytest.raises(ValueError, match=msg):
460465
# fill_value Period invalid
461466
arr.take([-1, 1], allow_fill=True, fill_value=pd.Period("2014Q1"))
462467

463468
tz = None if dti.tz is not None else "US/Eastern"
464469
now = pd.Timestamp.now().tz_localize(tz)
465-
with pytest.raises(TypeError):
470+
msg = "Cannot compare tz-naive and tz-aware datetime-like objects"
471+
with pytest.raises(TypeError, match=msg):
466472
# Timestamp with mismatched tz-awareness
467473
arr.take([-1, 1], allow_fill=True, fill_value=now)
468474

469-
with pytest.raises(ValueError):
475+
value = pd.NaT.value
476+
msg = f"'fill_value' should be a {self.dtype}. Got '{value}'."
477+
with pytest.raises(ValueError, match=msg):
470478
# require NaT, not iNaT, as it could be confused with an integer
471-
arr.take([-1, 1], allow_fill=True, fill_value=pd.NaT.value)
479+
arr.take([-1, 1], allow_fill=True, fill_value=value)
472480

473481
def test_concat_same_type_invalid(self, datetime_index):
474482
# different timezones
@@ -520,6 +528,7 @@ def test_strftime_nat(self):
520528
class TestTimedeltaArray(SharedTests):
521529
index_cls = pd.TimedeltaIndex
522530
array_cls = TimedeltaArray
531+
dtype = pd.Timedelta
523532

524533
def test_from_tdi(self):
525534
tdi = pd.TimedeltaIndex(["1 Day", "3 Hours"])
@@ -618,18 +627,23 @@ def test_take_fill_valid(self, timedelta_index):
618627
assert result[0] == td1
619628

620629
now = pd.Timestamp.now()
621-
with pytest.raises(ValueError):
630+
value = now
631+
msg = f"'fill_value' should be a {self.dtype}. Got '{value}'."
632+
with pytest.raises(ValueError, match=msg):
622633
# fill_value Timestamp invalid
623-
arr.take([0, 1], allow_fill=True, fill_value=now)
634+
arr.take([0, 1], allow_fill=True, fill_value=value)
624635

625-
with pytest.raises(ValueError):
636+
value = now.to_period("D")
637+
msg = f"'fill_value' should be a {self.dtype}. Got '{value}'."
638+
with pytest.raises(ValueError, match=msg):
626639
# fill_value Period invalid
627-
arr.take([0, 1], allow_fill=True, fill_value=now.to_period("D"))
640+
arr.take([0, 1], allow_fill=True, fill_value=value)
628641

629642

630643
class TestPeriodArray(SharedTests):
631644
index_cls = pd.PeriodIndex
632645
array_cls = PeriodArray
646+
dtype = pd.Period
633647

634648
def test_from_pi(self, period_index):
635649
pi = period_index
@@ -665,10 +679,11 @@ def test_to_timestamp(self, how, period_index):
665679
def test_to_timestamp_out_of_bounds(self):
666680
# GH#19643 previously overflowed silently
667681
pi = pd.period_range("1500", freq="Y", periods=3)
668-
with pytest.raises(OutOfBoundsDatetime):
682+
msg = "Out of bounds nanosecond timestamp: 1500-01-01 00:00:00"
683+
with pytest.raises(OutOfBoundsDatetime, match=msg):
669684
pi.to_timestamp()
670685

671-
with pytest.raises(OutOfBoundsDatetime):
686+
with pytest.raises(OutOfBoundsDatetime, match=msg):
672687
pi._data.to_timestamp()
673688

674689
@pytest.mark.parametrize("propname", PeriodArray._bool_ops)
@@ -708,7 +723,8 @@ def test_array_interface(self, period_index):
708723
tm.assert_numpy_array_equal(result, arr.asi8)
709724

710725
# to other dtypes
711-
with pytest.raises(TypeError):
726+
msg = r"float\(\) argument must be a string or a number, not 'Period'"
727+
with pytest.raises(TypeError, match=msg):
712728
np.asarray(arr, dtype="float64")
713729

714730
result = np.asarray(arr, dtype="S20")
@@ -774,8 +790,13 @@ def test_casting_nat_setitem_array(array, casting_nats):
774790
ids=lambda x: type(x).__name__,
775791
)
776792
def test_invalid_nat_setitem_array(array, non_casting_nats):
793+
msg = (
794+
"'value' should be a '(Timestamp|Timedelta|Period)', 'NaT', or array of those. "
795+
"Got '(timedelta64|datetime64|int)' instead."
796+
)
797+
777798
for nat in non_casting_nats:
778-
with pytest.raises(TypeError):
799+
with pytest.raises(TypeError, match=msg):
779800
array[0] = nat
780801

781802

0 commit comments

Comments
 (0)