Skip to content

Commit bdd51ec

Browse files
committed
TST: Capture warnings in _check_plot_works
1 parent 106b15a commit bdd51ec

File tree

3 files changed

+85
-69
lines changed

3 files changed

+85
-69
lines changed

ci/requirements-3.4.run

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ beautiful-soup
1111
scipy
1212
numexpr
1313
pytables
14-
matplotlib=1.3.1
1514
lxml
1615
sqlalchemy
1716
bottleneck

pandas/tests/test_graphics.py

+57-44
Original file line numberDiff line numberDiff line change
@@ -800,8 +800,8 @@ def test_hist_legacy(self):
800800
_check_plot_works(self.ts.hist)
801801
_check_plot_works(self.ts.hist, grid=False)
802802
_check_plot_works(self.ts.hist, figsize=(8, 10))
803-
_check_plot_works(self.ts.hist, by=self.ts.index.month)
804-
_check_plot_works(self.ts.hist, by=self.ts.index.month, bins=5)
803+
_check_plot_works(self.ts.hist, filterwarnings='ignore', by=self.ts.index.month)
804+
_check_plot_works(self.ts.hist, filterwarnings='ignore', by=self.ts.index.month, bins=5)
805805

806806
fig, ax = self.plt.subplots(1, 1)
807807
_check_plot_works(self.ts.hist, ax=ax)
@@ -835,25 +835,32 @@ def test_hist_layout(self):
835835
def test_hist_layout_with_by(self):
836836
df = self.hist_df
837837

838-
axes = _check_plot_works(df.height.hist, by=df.gender, layout=(2, 1))
838+
axes = _check_plot_works(df.height.hist, filterwarnings='ignore',
839+
by=df.gender, layout=(2, 1))
839840
self._check_axes_shape(axes, axes_num=2, layout=(2, 1))
840841

841-
axes = _check_plot_works(df.height.hist, by=df.gender, layout=(3, -1))
842+
axes = _check_plot_works(df.height.hist, filterwarnings='ignore',
843+
by=df.gender, layout=(3, -1))
842844
self._check_axes_shape(axes, axes_num=2, layout=(3, 1))
843845

844-
axes = _check_plot_works(df.height.hist, by=df.category, layout=(4, 1))
846+
axes = _check_plot_works(df.height.hist, filterwarnings='ignore',
847+
by=df.category, layout=(4, 1))
845848
self._check_axes_shape(axes, axes_num=4, layout=(4, 1))
846849

847-
axes = _check_plot_works(df.height.hist, by=df.category, layout=(2, -1))
850+
axes = _check_plot_works(df.height.hist, filterwarnings='ignore',
851+
by=df.category, layout=(2, -1))
848852
self._check_axes_shape(axes, axes_num=4, layout=(2, 2))
849853

850-
axes = _check_plot_works(df.height.hist, by=df.category, layout=(3, -1))
854+
axes = _check_plot_works(df.height.hist, filterwarnings='ignore',
855+
by=df.category, layout=(3, -1))
851856
self._check_axes_shape(axes, axes_num=4, layout=(3, 2))
852857

853-
axes = _check_plot_works(df.height.hist, by=df.category, layout=(-1, 4))
858+
axes = _check_plot_works(df.height.hist, filterwarnings='ignore',
859+
by=df.category, layout=(-1, 4))
854860
self._check_axes_shape(axes, axes_num=4, layout=(1, 4))
855861

856-
axes = _check_plot_works(df.height.hist, by=df.classroom, layout=(2, 2))
862+
axes = _check_plot_works(df.height.hist, filterwarnings='ignore',
863+
by=df.classroom, layout=(2, 2))
857864
self._check_axes_shape(axes, axes_num=3, layout=(2, 2))
858865

859866
axes = df.height.hist(by=df.category, layout=(4, 2), figsize=(12, 7))
@@ -1247,14 +1254,16 @@ def setUp(self):
12471254
@slow
12481255
def test_plot(self):
12491256
df = self.tdf
1250-
_check_plot_works(df.plot, grid=False)
1251-
axes = _check_plot_works(df.plot, subplots=True)
1257+
_check_plot_works(df.plot, filterwarnings='ignore', grid=False)
1258+
axes = _check_plot_works(df.plot, filterwarnings='ignore', subplots=True)
12521259
self._check_axes_shape(axes, axes_num=4, layout=(4, 1))
12531260

1254-
axes = _check_plot_works(df.plot, subplots=True, layout=(-1, 2))
1261+
axes = _check_plot_works(df.plot, filterwarnings='ignore',
1262+
subplots=True, layout=(-1, 2))
12551263
self._check_axes_shape(axes, axes_num=4, layout=(2, 2))
12561264

1257-
axes = _check_plot_works(df.plot, subplots=True, use_index=False)
1265+
axes = _check_plot_works(df.plot, filterwarnings='ignore',
1266+
subplots=True, use_index=False)
12581267
self._check_axes_shape(axes, axes_num=4, layout=(4, 1))
12591268

12601269
df = DataFrame({'x': [1, 2], 'y': [3, 4]})
@@ -1263,13 +1272,14 @@ def test_plot(self):
12631272

12641273
df = DataFrame(np.random.rand(10, 3),
12651274
index=list(string.ascii_letters[:10]))
1275+
12661276
_check_plot_works(df.plot, use_index=True)
12671277
_check_plot_works(df.plot, sort_columns=False)
12681278
_check_plot_works(df.plot, yticks=[1, 5, 10])
12691279
_check_plot_works(df.plot, xticks=[1, 5, 10])
12701280
_check_plot_works(df.plot, ylim=(-100, 100), xlim=(-100, 100))
12711281

1272-
_check_plot_works(df.plot, subplots=True, title='blah')
1282+
_check_plot_works(df.plot, filterwarnings='ignore', subplots=True, title='blah')
12731283
# We have to redo it here because _check_plot_works does two plots, once without an ax
12741284
# kwarg and once with an ax kwarg and the new sharex behaviour does not remove the
12751285
# visibility of the latter axis (as ax is present).
@@ -2083,7 +2093,7 @@ def test_plot_bar(self):
20832093

20842094
_check_plot_works(df.plot.bar)
20852095
_check_plot_works(df.plot.bar, legend=False)
2086-
_check_plot_works(df.plot.bar, subplots=True)
2096+
_check_plot_works(df.plot.bar, filterwarnings='ignore', subplots=True)
20872097
_check_plot_works(df.plot.bar, stacked=True)
20882098

20892099
df = DataFrame(randn(10, 15),
@@ -2300,7 +2310,7 @@ def test_boxplot_vertical(self):
23002310
self._check_text_labels(ax.get_yticklabels(), labels)
23012311
self.assertEqual(len(ax.lines), self.bp_n_objects * len(numeric_cols))
23022312

2303-
axes = _check_plot_works(df.plot.box, subplots=True,
2313+
axes = _check_plot_works(df.plot.box, filterwarnings='ignore', subplots=True,
23042314
vert=False, logx=True)
23052315
self._check_axes_shape(axes, axes_num=3, layout=(1, 3))
23062316
self._check_ax_scales(axes, xaxis='log')
@@ -2360,7 +2370,7 @@ def test_kde_df(self):
23602370
ax = df.plot(kind='kde', rot=20, fontsize=5)
23612371
self._check_ticks_props(ax, xrot=20, xlabelsize=5, ylabelsize=5)
23622372

2363-
axes = _check_plot_works(df.plot, kind='kde', subplots=True)
2373+
axes = _check_plot_works(df.plot, filterwarnings='ignore', kind='kde', subplots=True)
23642374
self._check_axes_shape(axes, axes_num=4, layout=(4, 1))
23652375

23662376
axes = df.plot(kind='kde', logy=True, subplots=True)
@@ -2387,7 +2397,7 @@ def test_hist_df(self):
23872397
expected = [com.pprint_thing(c) for c in df.columns]
23882398
self._check_legend_labels(ax, labels=expected)
23892399

2390-
axes = _check_plot_works(df.plot.hist, subplots=True, logy=True)
2400+
axes = _check_plot_works(df.plot.hist, filterwarnings='ignore', subplots=True, logy=True)
23912401
self._check_axes_shape(axes, axes_num=4, layout=(4, 1))
23922402
self._check_ax_scales(axes, yaxis='log')
23932403

@@ -3093,7 +3103,7 @@ def test_pie_df(self):
30933103
ax = _check_plot_works(df.plot.pie, y=2)
30943104
self._check_text_labels(ax.texts, df.index)
30953105

3096-
axes = _check_plot_works(df.plot.pie, subplots=True)
3106+
axes = _check_plot_works(df.plot.pie, filterwarnings='ignore', subplots=True)
30973107
self.assertEqual(len(axes), len(df.columns))
30983108
for ax in axes:
30993109
self._check_text_labels(ax.texts, df.index)
@@ -3102,7 +3112,7 @@ def test_pie_df(self):
31023112

31033113
labels = ['A', 'B', 'C', 'D', 'E']
31043114
color_args = ['r', 'g', 'b', 'c', 'm']
3105-
axes = _check_plot_works(df.plot.pie, subplots=True,
3115+
axes = _check_plot_works(df.plot.pie, filterwarnings='ignore', subplots=True,
31063116
labels=labels, colors=color_args)
31073117
self.assertEqual(len(axes), len(df.columns))
31083118

@@ -3156,7 +3166,8 @@ def test_errorbar_plot(self):
31563166
self._check_has_errorbars(ax, xerr=2, yerr=2)
31573167
ax = _check_plot_works(df.plot, xerr=0.2, yerr=0.2, kind=kind)
31583168
self._check_has_errorbars(ax, xerr=2, yerr=2)
3159-
axes = _check_plot_works(df.plot, yerr=df_err, xerr=df_err, subplots=True, kind=kind)
3169+
axes = _check_plot_works(df.plot, filterwarnings='ignore', yerr=df_err,
3170+
xerr=df_err, subplots=True, kind=kind)
31603171
self._check_has_errorbars(axes, xerr=1, yerr=1)
31613172

31623173
ax = _check_plot_works((df+1).plot, yerr=df_err, xerr=df_err, kind='bar', log=True)
@@ -3245,7 +3256,8 @@ def test_errorbar_timeseries(self):
32453256
self._check_has_errorbars(ax, xerr=0, yerr=1)
32463257
ax = _check_plot_works(tdf.plot, yerr=tdf_err, kind=kind)
32473258
self._check_has_errorbars(ax, xerr=0, yerr=2)
3248-
axes = _check_plot_works(tdf.plot, kind=kind, yerr=tdf_err, subplots=True)
3259+
axes = _check_plot_works(tdf.plot, filterwarnings='ignore', kind=kind,
3260+
yerr=tdf_err, subplots=True)
32493261
self._check_has_errorbars(axes, xerr=0, yerr=1)
32503262

32513263
def test_errorbar_asymmetrical(self):
@@ -3690,37 +3702,38 @@ def assert_is_valid_plot_return_object(objs):
36903702
''.format(objs.__class__.__name__))
36913703

36923704

3693-
def _check_plot_works(f, *args, **kwargs):
3705+
def _check_plot_works(f, filterwarnings='always', **kwargs):
36943706
import matplotlib.pyplot as plt
36953707
ret = None
3696-
3697-
try:
3708+
with warnings.catch_warnings():
3709+
warnings.simplefilter(filterwarnings)
36983710
try:
3699-
fig = kwargs['figure']
3700-
except KeyError:
3701-
fig = plt.gcf()
3711+
try:
3712+
fig = kwargs['figure']
3713+
except KeyError:
3714+
fig = plt.gcf()
37023715

3703-
plt.clf()
3716+
plt.clf()
37043717

3705-
ax = kwargs.get('ax', fig.add_subplot(211))
3706-
ret = f(*args, **kwargs)
3718+
ax = kwargs.get('ax', fig.add_subplot(211))
3719+
ret = f(**kwargs)
37073720

3708-
assert_is_valid_plot_return_object(ret)
3709-
3710-
try:
3711-
kwargs['ax'] = fig.add_subplot(212)
3712-
ret = f(*args, **kwargs)
3713-
except Exception:
3714-
pass
3715-
else:
37163721
assert_is_valid_plot_return_object(ret)
37173722

3718-
with ensure_clean(return_filelike=True) as path:
3719-
plt.savefig(path)
3720-
finally:
3721-
tm.close(fig)
3723+
try:
3724+
kwargs['ax'] = fig.add_subplot(212)
3725+
ret = f(**kwargs)
3726+
except Exception:
3727+
pass
3728+
else:
3729+
assert_is_valid_plot_return_object(ret)
3730+
3731+
with ensure_clean(return_filelike=True) as path:
3732+
plt.savefig(path)
3733+
finally:
3734+
tm.close(fig)
37223735

3723-
return ret
3736+
return ret
37243737

37253738
def _generate_4_axes_via_gridspec():
37263739
import matplotlib.pyplot as plt

pandas/tests/test_graphics_others.py

+28-24
Original file line numberDiff line numberDiff line change
@@ -161,22 +161,22 @@ def test_plot_fails_when_ax_differs_from_figure(self):
161161
@slow
162162
def test_autocorrelation_plot(self):
163163
from pandas.tools.plotting import autocorrelation_plot
164-
_check_plot_works(autocorrelation_plot, self.ts)
165-
_check_plot_works(autocorrelation_plot, self.ts.values)
164+
_check_plot_works(autocorrelation_plot, series=self.ts)
165+
_check_plot_works(autocorrelation_plot, series=self.ts.values)
166166

167167
ax = autocorrelation_plot(self.ts, label='Test')
168168
self._check_legend_labels(ax, labels=['Test'])
169169

170170
@slow
171171
def test_lag_plot(self):
172172
from pandas.tools.plotting import lag_plot
173-
_check_plot_works(lag_plot, self.ts)
174-
_check_plot_works(lag_plot, self.ts, lag=5)
173+
_check_plot_works(lag_plot, series=self.ts)
174+
_check_plot_works(lag_plot, series=self.ts, lag=5)
175175

176176
@slow
177177
def test_bootstrap_plot(self):
178178
from pandas.tools.plotting import bootstrap_plot
179-
_check_plot_works(bootstrap_plot, self.ts, size=10)
179+
_check_plot_works(bootstrap_plot, series=self.ts, size=10)
180180

181181

182182
@tm.mplskip
@@ -210,7 +210,7 @@ def test_boxplot_legacy(self):
210210
_check_plot_works(df.boxplot, column='one', by=['indic', 'indic2'])
211211
_check_plot_works(df.boxplot, by='indic')
212212
_check_plot_works(df.boxplot, by=['indic', 'indic2'])
213-
_check_plot_works(plotting.boxplot, df['one'], return_type='dict')
213+
_check_plot_works(plotting.boxplot, data=df['one'], return_type='dict')
214214
_check_plot_works(df.boxplot, notch=1, return_type='dict')
215215
_check_plot_works(df.boxplot, by='indic', notch=1)
216216

@@ -304,6 +304,7 @@ def test_boxplot_empty_column(self):
304304

305305
@slow
306306
def test_hist_df_legacy(self):
307+
from matplotlib.patches import Rectangle
307308
_check_plot_works(self.hist_df.hist)
308309

309310
# make sure layout is handled
@@ -347,7 +348,8 @@ def test_hist_df_legacy(self):
347348
# make sure kwargs to hist are handled
348349
ax = ser.hist(normed=True, cumulative=True, bins=4)
349350
# height of last bin (index 5) must be 1.0
350-
self.assertAlmostEqual(ax.get_children()[5].get_height(), 1.0)
351+
rects = [x for x in ax.get_children() if isinstance(x, Rectangle)]
352+
self.assertAlmostEqual(rects[-1].get_height(), 1.0)
351353

352354
tm.close()
353355
ax = ser.hist(log=True)
@@ -413,9 +415,9 @@ def scat(**kwds):
413415
def scat2(x, y, by=None, ax=None, figsize=None):
414416
return plotting.scatter_plot(df, x, y, by, ax, figsize=None)
415417

416-
_check_plot_works(scat2, 0, 1)
418+
_check_plot_works(scat2, x=0, y=1)
417419
grouper = Series(np.repeat([1, 2, 3, 4, 5], 20), df.index)
418-
_check_plot_works(scat2, 0, 1, by=grouper)
420+
_check_plot_works(scat2, x=0, y=1, by=grouper)
419421

420422
def test_scatter_matrix_axis(self):
421423
tm._skip_if_no_scipy()
@@ -424,15 +426,17 @@ def test_scatter_matrix_axis(self):
424426
with tm.RNGContext(42):
425427
df = DataFrame(randn(100, 3))
426428

427-
axes = _check_plot_works(scatter_matrix, df, range_padding=.1)
429+
axes = _check_plot_works(scatter_matrix, filterwarnings='always', frame=df,
430+
range_padding=.1)
428431
axes0_labels = axes[0][0].yaxis.get_majorticklabels()
429432
# GH 5662
430433
expected = ['-2', '-1', '0', '1', '2']
431434
self._check_text_labels(axes0_labels, expected)
432435
self._check_ticks_props(axes, xlabelsize=8, xrot=90, ylabelsize=8, yrot=0)
433436

434437
df[0] = ((df[0] - 2) / 3)
435-
axes = _check_plot_works(scatter_matrix, df, range_padding=.1)
438+
axes = _check_plot_works(scatter_matrix, filterwarnings='always', frame=df,
439+
range_padding=.1)
436440
axes0_labels = axes[0][0].yaxis.get_majorticklabels()
437441
expected = ['-1.2', '-1.0', '-0.8', '-0.6', '-0.4', '-0.2', '0.0']
438442
self._check_text_labels(axes0_labels, expected)
@@ -445,17 +449,17 @@ def test_andrews_curves(self):
445449

446450
df = self.iris
447451

448-
_check_plot_works(andrews_curves, df, 'Name')
452+
_check_plot_works(andrews_curves, frame=df, class_column='Name')
449453

450454
rgba = ('#556270', '#4ECDC4', '#C7F464')
451-
ax = _check_plot_works(andrews_curves, df, 'Name', color=rgba)
455+
ax = _check_plot_works(andrews_curves, frame=df, class_column='Name', color=rgba)
452456
self._check_colors(ax.get_lines()[:10], linecolors=rgba, mapping=df['Name'][:10])
453457

454458
cnames = ['dodgerblue', 'aquamarine', 'seagreen']
455-
ax = _check_plot_works(andrews_curves, df, 'Name', color=cnames)
459+
ax = _check_plot_works(andrews_curves, frame=df, class_column='Name', color=cnames)
456460
self._check_colors(ax.get_lines()[:10], linecolors=cnames, mapping=df['Name'][:10])
457461

458-
ax = _check_plot_works(andrews_curves, df, 'Name', colormap=cm.jet)
462+
ax = _check_plot_works(andrews_curves, frame=df, class_column='Name', colormap=cm.jet)
459463
cmaps = lmap(cm.jet, np.linspace(0, 1, df['Name'].nunique()))
460464
self._check_colors(ax.get_lines()[:10], linecolors=cmaps, mapping=df['Name'][:10])
461465

@@ -478,23 +482,23 @@ def test_parallel_coordinates(self):
478482

479483
df = self.iris
480484

481-
ax = _check_plot_works(parallel_coordinates, df, 'Name')
485+
ax = _check_plot_works(parallel_coordinates, frame=df, class_column='Name')
482486
nlines = len(ax.get_lines())
483487
nxticks = len(ax.xaxis.get_ticklabels())
484488

485489
rgba = ('#556270', '#4ECDC4', '#C7F464')
486-
ax = _check_plot_works(parallel_coordinates, df, 'Name', color=rgba)
490+
ax = _check_plot_works(parallel_coordinates, frame=df, class_column='Name', color=rgba)
487491
self._check_colors(ax.get_lines()[:10], linecolors=rgba, mapping=df['Name'][:10])
488492

489493
cnames = ['dodgerblue', 'aquamarine', 'seagreen']
490-
ax = _check_plot_works(parallel_coordinates, df, 'Name', color=cnames)
494+
ax = _check_plot_works(parallel_coordinates, frame=df, class_column='Name', color=cnames)
491495
self._check_colors(ax.get_lines()[:10], linecolors=cnames, mapping=df['Name'][:10])
492496

493-
ax = _check_plot_works(parallel_coordinates, df, 'Name', colormap=cm.jet)
497+
ax = _check_plot_works(parallel_coordinates, frame=df, class_column='Name', colormap=cm.jet)
494498
cmaps = lmap(cm.jet, np.linspace(0, 1, df['Name'].nunique()))
495499
self._check_colors(ax.get_lines()[:10], linecolors=cmaps, mapping=df['Name'][:10])
496500

497-
ax = _check_plot_works(parallel_coordinates, df, 'Name', axvlines=False)
501+
ax = _check_plot_works(parallel_coordinates, frame=df, class_column='Name', axvlines=False)
498502
assert len(ax.get_lines()) == (nlines - nxticks)
499503

500504
colors = ['b', 'g', 'r']
@@ -517,20 +521,20 @@ def test_radviz(self):
517521
from matplotlib import cm
518522

519523
df = self.iris
520-
_check_plot_works(radviz, df, 'Name')
524+
_check_plot_works(radviz, frame=df, class_column='Name')
521525

522526
rgba = ('#556270', '#4ECDC4', '#C7F464')
523-
ax = _check_plot_works(radviz, df, 'Name', color=rgba)
527+
ax = _check_plot_works(radviz, frame=df, class_column='Name', color=rgba)
524528
# skip Circle drawn as ticks
525529
patches = [p for p in ax.patches[:20] if p.get_label() != '']
526530
self._check_colors(patches[:10], facecolors=rgba, mapping=df['Name'][:10])
527531

528532
cnames = ['dodgerblue', 'aquamarine', 'seagreen']
529-
_check_plot_works(radviz, df, 'Name', color=cnames)
533+
_check_plot_works(radviz, frame=df, class_column='Name', color=cnames)
530534
patches = [p for p in ax.patches[:20] if p.get_label() != '']
531535
self._check_colors(patches, facecolors=cnames, mapping=df['Name'][:10])
532536

533-
_check_plot_works(radviz, df, 'Name', colormap=cm.jet)
537+
_check_plot_works(radviz, frame=df, class_column='Name', colormap=cm.jet)
534538
cmaps = lmap(cm.jet, np.linspace(0, 1, df['Name'].nunique()))
535539
patches = [p for p in ax.patches[:20] if p.get_label() != '']
536540
self._check_colors(patches, facecolors=cmaps, mapping=df['Name'][:10])

0 commit comments

Comments
 (0)