Skip to content

Commit

Permalink
STY: use pytest.raises context manager (plotting, reductions, scalar.…
Browse files Browse the repository at this point in the history
…..) (#25483)

* STY: use pytest.raises context manager (plotting, reductions, scalar...)

* revert removed testing in test_timedelta.py

* remove TODO from test_frame.py

* skip py2 ci failure
  • Loading branch information
simonjayhawkins authored and jreback committed Mar 3, 2019
1 parent cc5b73e commit c66028c
Show file tree
Hide file tree
Showing 12 changed files with 180 additions and 81 deletions.
21 changes: 14 additions & 7 deletions pandas/tests/plotting/test_boxplot_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,20 @@ def test_grouped_box_return_type(self):
def test_grouped_box_layout(self):
df = self.hist_df

pytest.raises(ValueError, df.boxplot, column=['weight', 'height'],
by=df.gender, layout=(1, 1))
pytest.raises(ValueError, df.boxplot,
column=['height', 'weight', 'category'],
layout=(2, 1), return_type='dict')
pytest.raises(ValueError, df.boxplot, column=['weight', 'height'],
by=df.gender, layout=(-1, -1))
msg = "Layout of 1x1 must be larger than required size 2"
with pytest.raises(ValueError, match=msg):
df.boxplot(column=['weight', 'height'], by=df.gender,
layout=(1, 1))

msg = "The 'layout' keyword is not supported when 'by' is None"
with pytest.raises(ValueError, match=msg):
df.boxplot(column=['height', 'weight', 'category'],
layout=(2, 1), return_type='dict')

msg = "At least one dimension of layout must be positive"
with pytest.raises(ValueError, match=msg):
df.boxplot(column=['weight', 'height'], by=df.gender,
layout=(-1, -1))

# _check_plot_works adds an ax so catch warning. see GH #13188
with tm.assert_produces_warning(UserWarning):
Expand Down
13 changes: 10 additions & 3 deletions pandas/tests/plotting/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ def test_nonnumeric_exclude(self):
assert len(ax.get_lines()) == 1 # B was plotted
self.plt.close(fig)

pytest.raises(TypeError, df['A'].plot)
msg = "Empty 'DataFrame': no numeric data to plot"
with pytest.raises(TypeError, match=msg):
df['A'].plot()

def test_tsplot_deprecated(self):
from pandas.tseries.plotting import tsplot
Expand Down Expand Up @@ -140,10 +142,15 @@ def f(*args, **kwds):
def test_both_style_and_color(self):

ts = tm.makeTimeSeries()
pytest.raises(ValueError, ts.plot, style='b-', color='#000099')
msg = ("Cannot pass 'style' string with a color symbol and 'color' "
"keyword argument. Please use one or the other or pass 'style'"
" without a color symbol")
with pytest.raises(ValueError, match=msg):
ts.plot(style='b-', color='#000099')

s = ts.reset_index(drop=True)
pytest.raises(ValueError, s.plot, style='b-', color='#000099')
with pytest.raises(ValueError, match=msg):
s.plot(style='b-', color='#000099')

@pytest.mark.slow
def test_high_freq(self):
Expand Down
17 changes: 11 additions & 6 deletions pandas/tests/plotting/test_hist_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,12 +332,17 @@ def test_grouped_hist_legacy2(self):
@pytest.mark.slow
def test_grouped_hist_layout(self):
df = self.hist_df
pytest.raises(ValueError, df.hist, column='weight', by=df.gender,
layout=(1, 1))
pytest.raises(ValueError, df.hist, column='height', by=df.category,
layout=(1, 3))
pytest.raises(ValueError, df.hist, column='height', by=df.category,
layout=(-1, -1))
msg = "Layout of 1x1 must be larger than required size 2"
with pytest.raises(ValueError, match=msg):
df.hist(column='weight', by=df.gender, layout=(1, 1))

msg = "Layout of 1x3 must be larger than required size 4"
with pytest.raises(ValueError, match=msg):
df.hist(column='height', by=df.category, layout=(1, 3))

msg = "At least one dimension of layout must be positive"
with pytest.raises(ValueError, match=msg):
df.hist(column='height', by=df.category, layout=(-1, -1))

with tm.assert_produces_warning(UserWarning):
axes = _check_plot_works(df.hist, column='height', by=df.gender,
Expand Down
14 changes: 10 additions & 4 deletions pandas/tests/plotting/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,20 @@ def test_subplot_titles(self, iris):
assert [p.get_title() for p in plot] == title

# Case len(title) > len(df)
pytest.raises(ValueError, df.plot, subplots=True,
title=title + ["kittens > puppies"])
msg = ("The length of `title` must equal the number of columns if"
" using `title` of type `list` and `subplots=True`")
with pytest.raises(ValueError, match=msg):
df.plot(subplots=True, title=title + ["kittens > puppies"])

# Case len(title) < len(df)
pytest.raises(ValueError, df.plot, subplots=True, title=title[:2])
with pytest.raises(ValueError, match=msg):
df.plot(subplots=True, title=title[:2])

# Case subplots=False and title is of type list
pytest.raises(ValueError, df.plot, subplots=False, title=title)
msg = ("Using `title` of type `list` is not supported unless"
" `subplots=True` is passed")
with pytest.raises(ValueError, match=msg):
df.plot(subplots=False, title=title)

# Case df with 3 numeric columns but layout of (2,2)
plot = df.drop('SepalWidth', axis=1).plot(subplots=True, layout=(2, 2),
Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/reductions/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,9 @@ def test_timedelta_ops(self):

# invalid ops
for op in ['skew', 'kurt', 'sem', 'prod']:
pytest.raises(TypeError, getattr(td, op))
msg = "reduction operation '{}' not allowed for this dtype"
with pytest.raises(TypeError, match=msg.format(op)):
getattr(td, op)()

# GH#10040
# make sure NaT is properly handled by median()
Expand Down
21 changes: 12 additions & 9 deletions pandas/tests/scalar/period/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pandas._libs.tslibs.ccalendar import DAYS, MONTHS
from pandas._libs.tslibs.frequencies import INVALID_FREQ_ERR_MSG
from pandas._libs.tslibs.parsing import DateParseError
from pandas._libs.tslibs.period import IncompatibleFrequency
from pandas._libs.tslibs.timezones import dateutil_gettz, maybe_get_tz
from pandas.compat import iteritems, text_type
from pandas.compat.numpy import np_datetime64_compat
Expand Down Expand Up @@ -35,7 +36,9 @@ def test_construction(self):
i4 = Period('2005', freq='M')
i5 = Period('2005', freq='m')

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

i1 = Period.now('Q')
Expand Down Expand Up @@ -74,9 +77,12 @@ def test_construction(self):
freq='U')
assert i1 == expected

pytest.raises(ValueError, Period, ordinal=200701)
msg = "Must supply freq for ordinal value"
with pytest.raises(ValueError, match=msg):
Period(ordinal=200701)

pytest.raises(ValueError, Period, '2007-1-1', freq='X')
with pytest.raises(ValueError, match="Invalid frequency: X"):
Period('2007-1-1', freq='X')

def test_construction_bday(self):

Expand Down Expand Up @@ -233,10 +239,6 @@ def test_period_constructor_offsets(self):
freq='U')
assert i1 == expected

pytest.raises(ValueError, Period, ordinal=200701)

pytest.raises(ValueError, Period, '2007-1-1', freq='X')

def test_invalid_arguments(self):
with pytest.raises(ValueError):
Period(datetime.now())
Expand Down Expand Up @@ -925,8 +927,9 @@ def test_properties_secondly(self):
class TestPeriodField(object):

def test_get_period_field_array_raises_on_out_of_range(self):
pytest.raises(ValueError, libperiod.get_period_field_arr, -1,
np.empty(1), 0)
msg = "Buffer dtype mismatch, expected 'int64_t' but got 'double'"
with pytest.raises(ValueError, match=msg):
libperiod.get_period_field_arr(-1, np.empty(1), 0)


class TestComparisons(object):
Expand Down
45 changes: 32 additions & 13 deletions pandas/tests/scalar/timedelta/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,13 @@ def check(value):
assert rng.microseconds == 0
assert rng.nanoseconds == 0

pytest.raises(AttributeError, lambda: rng.hours)
pytest.raises(AttributeError, lambda: rng.minutes)
pytest.raises(AttributeError, lambda: rng.milliseconds)
msg = "'Timedelta' object has no attribute '{}'"
with pytest.raises(AttributeError, match=msg.format('hours')):
rng.hours
with pytest.raises(AttributeError, match=msg.format('minutes')):
rng.minutes
with pytest.raises(AttributeError, match=msg.format('milliseconds')):
rng.milliseconds

# GH 10050
check(rng.days)
Expand All @@ -272,9 +276,13 @@ def check(value):
assert rng.seconds == 10 * 3600 + 11 * 60 + 12
assert rng.microseconds == 100 * 1000 + 123
assert rng.nanoseconds == 456
pytest.raises(AttributeError, lambda: rng.hours)
pytest.raises(AttributeError, lambda: rng.minutes)
pytest.raises(AttributeError, lambda: rng.milliseconds)
msg = "'Timedelta' object has no attribute '{}'"
with pytest.raises(AttributeError, match=msg.format('hours')):
rng.hours
with pytest.raises(AttributeError, match=msg.format('minutes')):
rng.minutes
with pytest.raises(AttributeError, match=msg.format('milliseconds')):
rng.milliseconds

# components
tup = pd.to_timedelta(-1, 'us').components
Expand Down Expand Up @@ -449,8 +457,12 @@ def test_round(self):
assert r2 == s2

# invalid
for freq in ['Y', 'M', 'foobar']:
pytest.raises(ValueError, lambda: t1.round(freq))
for freq, msg in [
('Y', '<YearEnd: month=12> is a non-fixed frequency'),
('M', '<MonthEnd> is a non-fixed frequency'),
('foobar', 'Invalid frequency: foobar')]:
with pytest.raises(ValueError, match=msg):
t1.round(freq)

t1 = timedelta_range('1 days', periods=3, freq='1 min 2 s 3 us')
t2 = -1 * t1
Expand Down Expand Up @@ -495,11 +507,15 @@ def test_round(self):
r1 = t1.round(freq)
tm.assert_index_equal(r1, s1)
r2 = t2.round(freq)
tm.assert_index_equal(r2, s2)
tm.assert_index_equal(r2, s2)

# invalid
for freq in ['Y', 'M', 'foobar']:
pytest.raises(ValueError, lambda: t1.round(freq))
for freq, msg in [
('Y', '<YearEnd: month=12> is a non-fixed frequency'),
('M', '<MonthEnd> is a non-fixed frequency'),
('foobar', 'Invalid frequency: foobar')]:
with pytest.raises(ValueError, match=msg):
t1.round(freq)

def test_contains(self):
# Checking for any NaT-like objects
Expand Down Expand Up @@ -609,9 +625,12 @@ def test_overflow(self):
assert np.allclose(result.value / 1000, expected.value / 1000)

# sum
pytest.raises(ValueError, lambda: (s - s.min()).sum())
msg = "overflow in timedelta operation"
with pytest.raises(ValueError, match=msg):
(s - s.min()).sum()
s1 = s[0:10000]
pytest.raises(ValueError, lambda: (s1 - s1.min()).sum())
with pytest.raises(ValueError, match=msg):
(s1 - s1.min()).sum()
s2 = s[0:1000]
result = (s2 - s2.min()).sum()

Expand Down
8 changes: 6 additions & 2 deletions pandas/tests/scalar/timestamp/test_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ def check(value, equal):
check(ts.hour, 9)
check(ts.minute, 6)
check(ts.second, 3)
pytest.raises(AttributeError, lambda: ts.millisecond)
msg = "'Timestamp' object has no attribute 'millisecond'"
with pytest.raises(AttributeError, match=msg):
ts.millisecond
check(ts.microsecond, 100)
check(ts.nanosecond, 1)
check(ts.dayofweek, 6)
Expand All @@ -78,7 +80,9 @@ def check(value, equal):
check(ts.hour, 23)
check(ts.minute, 59)
check(ts.second, 0)
pytest.raises(AttributeError, lambda: ts.millisecond)
msg = "'Timestamp' object has no attribute 'millisecond'"
with pytest.raises(AttributeError, match=msg):
ts.millisecond
check(ts.microsecond, 0)
check(ts.nanosecond, 0)
check(ts.dayofweek, 2)
Expand Down
47 changes: 32 additions & 15 deletions pandas/tests/sparse/frame/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pytest

from pandas._libs.sparse import BlockIndex, IntIndex
from pandas.compat import lrange
from pandas.compat import PY2, lrange
from pandas.errors import PerformanceWarning

import pandas as pd
Expand Down Expand Up @@ -145,8 +145,9 @@ def test_constructor_ndarray(self, float_frame):
tm.assert_sp_frame_equal(sp, float_frame.reindex(columns=['A']))

# raise on level argument
pytest.raises(TypeError, float_frame.reindex, columns=['A'],
level=1)
msg = "Reindex by level not supported for sparse"
with pytest.raises(TypeError, match=msg):
float_frame.reindex(columns=['A'], level=1)

# wrong length index / columns
with pytest.raises(ValueError, match="^Index length"):
Expand Down Expand Up @@ -433,7 +434,8 @@ def test_getitem(self):
exp = sdf.reindex(columns=['a', 'b'])
tm.assert_sp_frame_equal(result, exp)

pytest.raises(Exception, sdf.__getitem__, ['a', 'd'])
with pytest.raises(KeyError, match=r"\['d'\] not in index"):
sdf[['a', 'd']]

def test_iloc(self, float_frame):

Expand Down Expand Up @@ -504,7 +506,9 @@ def test_getitem_overload(self, float_frame):
subframe = float_frame[indexer]

tm.assert_index_equal(subindex, subframe.index)
pytest.raises(Exception, float_frame.__getitem__, indexer[:-1])
msg = "Item wrong length 9 instead of 10"
with pytest.raises(ValueError, match=msg):
float_frame[indexer[:-1]]

def test_setitem(self, float_frame, float_frame_int_kind,
float_frame_dense,
Expand Down Expand Up @@ -551,8 +555,9 @@ def _check_frame(frame, orig):
assert len(frame['I'].sp_values) == N // 2

# insert ndarray wrong size
pytest.raises(Exception, frame.__setitem__, 'foo',
np.random.randn(N - 1))
msg = "Length of values does not match length of index"
with pytest.raises(AssertionError, match=msg):
frame['foo'] = np.random.randn(N - 1)

# scalar value
frame['J'] = 5
Expand Down Expand Up @@ -625,17 +630,22 @@ def test_delitem(self, float_frame):

def test_set_columns(self, float_frame):
float_frame.columns = float_frame.columns
pytest.raises(Exception, setattr, float_frame, 'columns',
float_frame.columns[:-1])
msg = ("Length mismatch: Expected axis has 4 elements, new values have"
" 3 elements")
with pytest.raises(ValueError, match=msg):
float_frame.columns = float_frame.columns[:-1]

def test_set_index(self, float_frame):
float_frame.index = float_frame.index
pytest.raises(Exception, setattr, float_frame, 'index',
float_frame.index[:-1])
msg = ("Length mismatch: Expected axis has 10 elements, new values"
" have 9 elements")
with pytest.raises(ValueError, match=msg):
float_frame.index = float_frame.index[:-1]

def test_ctor_reindex(self):
idx = pd.Index([0, 1, 2, 3])
with pytest.raises(ValueError, match=''):
msg = "Length of passed values is 2, index implies 4"
with pytest.raises(ValueError, match=msg):
pd.SparseDataFrame({"A": [1, 2]}, index=idx)

def test_append(self, float_frame):
Expand Down Expand Up @@ -858,14 +868,18 @@ def test_describe(self, float_frame):
str(float_frame)
desc = float_frame.describe() # noqa

@pytest.mark.skipif(PY2, reason="pytest.raises match regex fails")
def test_join(self, float_frame):
left = float_frame.loc[:, ['A', 'B']]
right = float_frame.loc[:, ['C', 'D']]
joined = left.join(right)
tm.assert_sp_frame_equal(joined, float_frame, exact_indices=False)

right = float_frame.loc[:, ['B', 'D']]
pytest.raises(Exception, left.join, right)
msg = (r"columns overlap but no suffix specified: Index\(\['B'\],"
r" dtype='object'\)")
with pytest.raises(ValueError, match=msg):
left.join(right)

with pytest.raises(ValueError, match='Other Series must have a name'):
float_frame.join(Series(
Expand Down Expand Up @@ -1046,8 +1060,11 @@ def _check(frame):
_check(float_frame_int_kind)

# for now
pytest.raises(Exception, _check, float_frame_fill0)
pytest.raises(Exception, _check, float_frame_fill2)
msg = "This routine assumes NaN fill value"
with pytest.raises(TypeError, match=msg):
_check(float_frame_fill0)
with pytest.raises(TypeError, match=msg):
_check(float_frame_fill2)

def test_transpose(self, float_frame, float_frame_int_kind,
float_frame_dense,
Expand Down
Loading

0 comments on commit c66028c

Please sign in to comment.