Skip to content

add match to bare pyteset.raises() - arrays #33010

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
Mar 30, 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
4 changes: 2 additions & 2 deletions pandas/tests/arithmetic/test_timedelta64.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,9 +650,9 @@ def test_tdi_add_overflow(self):
# See GH#14068
# preliminary test scalar analogue of vectorized tests below
# TODO: Make raised error message more informative and test
with pytest.raises(OutOfBoundsDatetime):
with pytest.raises(OutOfBoundsDatetime, match="10155196800000000000"):
pd.to_timedelta(106580, "D") + Timestamp("2000")
with pytest.raises(OutOfBoundsDatetime):
with pytest.raises(OutOfBoundsDatetime, match="10155196800000000000"):
Timestamp("2000") + pd.to_timedelta(106580, "D")

_NaT = int(pd.NaT) + 1
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/interval/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_overlaps_interval_container(self, constructor, other_constructor):
# TODO: modify this test when implemented
interval_container = constructor.from_breaks(range(5))
other_container = other_constructor.from_breaks(range(5))
with pytest.raises(NotImplementedError):
with pytest.raises(NotImplementedError, match="^$"):
interval_container.overlaps(other_container)

def test_overlaps_na(self, constructor, start_shift):
Expand Down
67 changes: 44 additions & 23 deletions pandas/tests/arrays/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ def test_take(self):

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

@pytest.mark.parametrize("fill_value", [2, 2.0, pd.Timestamp.now().time])
def test_take_fill_raises(self, fill_value):
data = np.arange(10, dtype="i8") * 24 * 3600 * 10 ** 9

arr = self.array_cls._simple_new(data, freq="D")

msg = f"'fill_value' should be a {self.dtype}. Got '{fill_value}'"
with pytest.raises(ValueError, match=msg):
arr.take([0, 1], allow_fill=True, fill_value=fill_value)

def test_take_fill(self):
data = np.arange(10, dtype="i8") * 24 * 3600 * 10 ** 9

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

with pytest.raises(ValueError):
arr.take([0, 1], allow_fill=True, fill_value=2)

with pytest.raises(ValueError):
arr.take([0, 1], allow_fill=True, fill_value=2.0)

with pytest.raises(ValueError):
arr.take([0, 1], allow_fill=True, fill_value=pd.Timestamp.now().time)

def test_concat_same_type(self):
data = np.arange(10, dtype="i8") * 24 * 3600 * 10 ** 9

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

with pytest.raises(ValueError):
msg = f"'value' should be a {self.dtype.__name__}."
with pytest.raises(ValueError, match=msg):
arr._unbox_scalar("foo")

def test_check_compatible_with(self):
Expand Down Expand Up @@ -261,6 +263,7 @@ def test_shift_fill_int_deprecated(self):
class TestDatetimeArray(SharedTests):
index_cls = pd.DatetimeIndex
array_cls = DatetimeArray
dtype = pd.Timestamp

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

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

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

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

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

def test_concat_same_type_invalid(self, datetime_index):
# different timezones
Expand Down Expand Up @@ -520,6 +528,7 @@ def test_strftime_nat(self):
class TestTimedeltaArray(SharedTests):
index_cls = pd.TimedeltaIndex
array_cls = TimedeltaArray
dtype = pd.Timedelta

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

now = pd.Timestamp.now()
with pytest.raises(ValueError):
value = now
msg = f"'fill_value' should be a {self.dtype}. Got '{value}'."
with pytest.raises(ValueError, match=msg):
# fill_value Timestamp invalid
arr.take([0, 1], allow_fill=True, fill_value=now)
arr.take([0, 1], allow_fill=True, fill_value=value)

with pytest.raises(ValueError):
value = now.to_period("D")
msg = f"'fill_value' should be a {self.dtype}. Got '{value}'."
with pytest.raises(ValueError, match=msg):
# fill_value Period invalid
arr.take([0, 1], allow_fill=True, fill_value=now.to_period("D"))
arr.take([0, 1], allow_fill=True, fill_value=value)


class TestPeriodArray(SharedTests):
index_cls = pd.PeriodIndex
array_cls = PeriodArray
dtype = pd.Period

def test_from_pi(self, period_index):
pi = period_index
Expand Down Expand Up @@ -665,10 +679,11 @@ def test_to_timestamp(self, how, period_index):
def test_to_timestamp_out_of_bounds(self):
# GH#19643 previously overflowed silently
pi = pd.period_range("1500", freq="Y", periods=3)
with pytest.raises(OutOfBoundsDatetime):
msg = "Out of bounds nanosecond timestamp: 1500-01-01 00:00:00"
with pytest.raises(OutOfBoundsDatetime, match=msg):
pi.to_timestamp()

with pytest.raises(OutOfBoundsDatetime):
with pytest.raises(OutOfBoundsDatetime, match=msg):
pi._data.to_timestamp()

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

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

result = np.asarray(arr, dtype="S20")
Expand Down Expand Up @@ -774,8 +790,13 @@ def test_casting_nat_setitem_array(array, casting_nats):
ids=lambda x: type(x).__name__,
)
def test_invalid_nat_setitem_array(array, non_casting_nats):
msg = (
"'value' should be a '(Timestamp|Timedelta|Period)', 'NaT', or array of those. "
"Got '(timedelta64|datetime64|int)' instead."
)

for nat in non_casting_nats:
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
array[0] = nat


Expand Down