Skip to content

Commit 3ada856

Browse files
mroeschkeim-vinicius
authored and
im-vinicius
committed
TST: Clean plotting tests (pandas-dev#53917)
* TST: Clean plotting tests * Fix random state
1 parent 70e61dd commit 3ada856

File tree

5 files changed

+475
-219
lines changed

5 files changed

+475
-219
lines changed

pandas/tests/plotting/test_boxplot_method.py

+80-28
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
mpl = pytest.importorskip("matplotlib")
2929

3030

31+
def _check_ax_limits(col, ax):
32+
y_min, y_max = ax.get_ylim()
33+
assert y_min <= col.min()
34+
assert y_max >= col.max()
35+
36+
3137
class TestDataFramePlots:
3238
def test_stacked_boxplot_set_axis(self):
3339
# GH2980
@@ -88,18 +94,30 @@ def test_boxplot_legacy2(self):
8894
with tm.assert_produces_warning(UserWarning, check_stacklevel=False):
8995
_check_plot_works(df.boxplot, by="X")
9096

97+
def test_boxplot_legacy2_with_ax(self):
98+
df = DataFrame(np.random.rand(10, 2), columns=["Col1", "Col2"])
99+
df["X"] = Series(["A", "A", "A", "A", "A", "B", "B", "B", "B", "B"])
100+
df["Y"] = Series(["A"] * 10)
91101
# When ax is supplied and required number of axes is 1,
92102
# passed ax should be used:
93-
fig, ax = mpl.pyplot.subplots()
103+
_, ax = mpl.pyplot.subplots()
94104
axes = df.boxplot("Col1", by="X", ax=ax)
95105
ax_axes = ax.axes
96106
assert ax_axes is axes
97107

108+
def test_boxplot_legacy2_with_ax_return_type(self):
109+
df = DataFrame(np.random.rand(10, 2), columns=["Col1", "Col2"])
110+
df["X"] = Series(["A", "A", "A", "A", "A", "B", "B", "B", "B", "B"])
111+
df["Y"] = Series(["A"] * 10)
98112
fig, ax = mpl.pyplot.subplots()
99113
axes = df.groupby("Y").boxplot(ax=ax, return_type="axes")
100114
ax_axes = ax.axes
101115
assert ax_axes is axes["A"]
102116

117+
def test_boxplot_legacy2_with_multi_col(self):
118+
df = DataFrame(np.random.rand(10, 2), columns=["Col1", "Col2"])
119+
df["X"] = Series(["A", "A", "A", "A", "A", "B", "B", "B", "B", "B"])
120+
df["Y"] = Series(["A"] * 10)
103121
# Multiple columns with an ax argument should use same figure
104122
fig, ax = mpl.pyplot.subplots()
105123
with tm.assert_produces_warning(UserWarning):
@@ -108,9 +126,13 @@ def test_boxplot_legacy2(self):
108126
)
109127
assert axes["Col1"].get_figure() is fig
110128

129+
def test_boxplot_legacy2_by_none(self):
130+
df = DataFrame(np.random.rand(10, 2), columns=["Col1", "Col2"])
131+
df["X"] = Series(["A", "A", "A", "A", "A", "B", "B", "B", "B", "B"])
132+
df["Y"] = Series(["A"] * 10)
111133
# When by is None, check that all relevant lines are present in the
112134
# dict
113-
fig, ax = mpl.pyplot.subplots()
135+
_, ax = mpl.pyplot.subplots()
114136
d = df.boxplot(ax=ax, return_type="dict")
115137
lines = list(itertools.chain.from_iterable(d.values()))
116138
assert len(ax.get_lines()) == len(lines)
@@ -135,24 +157,20 @@ def test_boxplot_return_type_legacy(self):
135157
result = df.boxplot()
136158
_check_box_return_type(result, "axes")
137159

138-
with tm.assert_produces_warning(False):
139-
result = df.boxplot(return_type="dict")
140-
_check_box_return_type(result, "dict")
141-
142-
with tm.assert_produces_warning(False):
143-
result = df.boxplot(return_type="axes")
144-
_check_box_return_type(result, "axes")
160+
@pytest.mark.parametrize("return_type", ["dict", "axes", "both"])
161+
def test_boxplot_return_type_legacy_return_type(self, return_type):
162+
# API change in https://github.com/pandas-dev/pandas/pull/7096
145163

164+
df = DataFrame(
165+
np.random.randn(6, 4),
166+
index=list(string.ascii_letters[:6]),
167+
columns=["one", "two", "three", "four"],
168+
)
146169
with tm.assert_produces_warning(False):
147-
result = df.boxplot(return_type="both")
148-
_check_box_return_type(result, "both")
170+
result = df.boxplot(return_type=return_type)
171+
_check_box_return_type(result, return_type)
149172

150173
def test_boxplot_axis_limits(self, hist_df):
151-
def _check_ax_limits(col, ax):
152-
y_min, y_max = ax.get_ylim()
153-
assert y_min <= col.min()
154-
assert y_max >= col.max()
155-
156174
df = hist_df.copy()
157175
df["age"] = np.random.randint(1, 20, df.shape[0])
158176
# One full row
@@ -161,6 +179,9 @@ def _check_ax_limits(col, ax):
161179
_check_ax_limits(df["weight"], weight_ax)
162180
assert weight_ax._sharey == height_ax
163181

182+
def test_boxplot_axis_limits_two_rows(self, hist_df):
183+
df = hist_df.copy()
184+
df["age"] = np.random.randint(1, 20, df.shape[0])
164185
# Two rows, one partial
165186
p = df.boxplot(["height", "weight", "age"], by="category")
166187
height_ax, weight_ax, age_ax = p[0, 0], p[0, 1], p[1, 0]
@@ -275,7 +296,7 @@ def test_color_kwd_errors(self, dict_colors, msg):
275296
)
276297
def test_specified_props_kwd(self, props, expected):
277298
# GH 30346
278-
df = DataFrame({k: np.random.random(100) for k in "ABC"})
299+
df = DataFrame({k: np.random.random(10) for k in "ABC"})
279300
kwd = {props: {"color": "C1"}}
280301
result = df.boxplot(return_type="dict", **kwd)
281302

@@ -285,9 +306,9 @@ def test_specified_props_kwd(self, props, expected):
285306
def test_plot_xlabel_ylabel(self, vert):
286307
df = DataFrame(
287308
{
288-
"a": np.random.randn(100),
289-
"b": np.random.randn(100),
290-
"group": np.random.choice(["group1", "group2"], 100),
309+
"a": np.random.randn(10),
310+
"b": np.random.randn(10),
311+
"group": np.random.choice(["group1", "group2"], 10),
291312
}
292313
)
293314
xlabel, ylabel = "x", "y"
@@ -299,9 +320,9 @@ def test_plot_xlabel_ylabel(self, vert):
299320
def test_boxplot_xlabel_ylabel(self, vert):
300321
df = DataFrame(
301322
{
302-
"a": np.random.randn(100),
303-
"b": np.random.randn(100),
304-
"group": np.random.choice(["group1", "group2"], 100),
323+
"a": np.random.randn(10),
324+
"b": np.random.randn(10),
325+
"group": np.random.choice(["group1", "group2"], 10),
305326
}
306327
)
307328
xlabel, ylabel = "x", "y"
@@ -313,9 +334,9 @@ def test_boxplot_xlabel_ylabel(self, vert):
313334
def test_boxplot_group_xlabel_ylabel(self, vert):
314335
df = DataFrame(
315336
{
316-
"a": np.random.randn(100),
317-
"b": np.random.randn(100),
318-
"group": np.random.choice(["group1", "group2"], 100),
337+
"a": np.random.randn(10),
338+
"b": np.random.randn(10),
339+
"group": np.random.choice(["group1", "group2"], 10),
319340
}
320341
)
321342
xlabel, ylabel = "x", "y"
@@ -325,6 +346,15 @@ def test_boxplot_group_xlabel_ylabel(self, vert):
325346
assert subplot.get_ylabel() == ylabel
326347
mpl.pyplot.close()
327348

349+
@pytest.mark.parametrize("vert", [True, False])
350+
def test_boxplot_group_no_xlabel_ylabel(self, vert):
351+
df = DataFrame(
352+
{
353+
"a": np.random.randn(10),
354+
"b": np.random.randn(10),
355+
"group": np.random.choice(["group1", "group2"], 10),
356+
}
357+
)
328358
ax = df.boxplot(by="group", vert=vert)
329359
for subplot in ax:
330360
target_label = subplot.get_xlabel() if vert else subplot.get_ylabel()
@@ -338,6 +368,9 @@ def test_boxplot_legacy1(self, hist_df):
338368
with tm.assert_produces_warning(UserWarning, check_stacklevel=False):
339369
axes = _check_plot_works(grouped.boxplot, return_type="axes")
340370
_check_axes_shape(list(axes.values), axes_num=2, layout=(1, 2))
371+
372+
def test_boxplot_legacy1_return_type(self, hist_df):
373+
grouped = hist_df.groupby(by="gender")
341374
axes = _check_plot_works(grouped.boxplot, subplots=False, return_type="axes")
342375
_check_axes_shape(axes, axes_num=1, layout=(1, 1))
343376

@@ -350,6 +383,11 @@ def test_boxplot_legacy2(self):
350383
axes = _check_plot_works(grouped.boxplot, return_type="axes")
351384
_check_axes_shape(list(axes.values), axes_num=10, layout=(4, 3))
352385

386+
@pytest.mark.slow
387+
def test_boxplot_legacy2_return_type(self):
388+
tuples = zip(string.ascii_letters[:10], range(10))
389+
df = DataFrame(np.random.rand(10, 3), index=MultiIndex.from_tuples(tuples))
390+
grouped = df.groupby(level=1)
353391
axes = _check_plot_works(grouped.boxplot, subplots=False, return_type="axes")
354392
_check_axes_shape(axes, axes_num=1, layout=(1, 1))
355393

@@ -387,8 +425,14 @@ def test_grouped_plot_fignums(self):
387425
assert len(res) == 2
388426
tm.close()
389427

428+
def test_grouped_plot_fignums_excluded_col(self):
429+
n = 10
430+
weight = Series(np.random.normal(166, 20, size=n))
431+
height = Series(np.random.normal(60, 10, size=n))
432+
gender = np.random.RandomState(42).choice(["male", "female"], size=n)
433+
df = DataFrame({"height": height, "weight": weight, "gender": gender})
390434
# now works with GH 5610 as gender is excluded
391-
res = df.groupby("gender").hist()
435+
df.groupby("gender").hist()
392436
tm.close()
393437

394438
@pytest.mark.slow
@@ -545,10 +589,14 @@ def test_grouped_box_multiple_axes(self, hist_df):
545589
# location should be changed if other test is added
546590
# which has earlier alphabetical order
547591
with tm.assert_produces_warning(UserWarning):
548-
fig, axes = mpl.pyplot.subplots(2, 2)
592+
_, axes = mpl.pyplot.subplots(2, 2)
549593
df.groupby("category").boxplot(column="height", return_type="axes", ax=axes)
550594
_check_axes_shape(mpl.pyplot.gcf().axes, axes_num=4, layout=(2, 2))
551595

596+
@pytest.mark.slow
597+
def test_grouped_box_multiple_axes_on_fig(self, hist_df):
598+
# GH 6970, GH 7069
599+
df = hist_df
552600
fig, axes = mpl.pyplot.subplots(2, 3)
553601
with tm.assert_produces_warning(UserWarning):
554602
returned = df.boxplot(
@@ -572,6 +620,10 @@ def test_grouped_box_multiple_axes(self, hist_df):
572620
tm.assert_numpy_array_equal(returned, axes[1])
573621
assert returned[0].figure is fig
574622

623+
@pytest.mark.slow
624+
def test_grouped_box_multiple_axes_ax_error(self, hist_df):
625+
# GH 6970, GH 7069
626+
df = hist_df
575627
msg = "The number of passed axes must be 3, the same as the output plot"
576628
with pytest.raises(ValueError, match=msg):
577629
fig, axes = mpl.pyplot.subplots(2, 3)

pandas/tests/plotting/test_converter.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -222,17 +222,15 @@ def test_conversion_float(self, dtc):
222222
rs = dtc.convert(datetime(2012, 1, 1, 1, 2, 3), None, None)
223223
tm.assert_almost_equal(rs, xp, rtol=rtol)
224224

225-
def test_conversion_outofbounds_datetime(self, dtc):
225+
@pytest.mark.parametrize(
226+
"values",
227+
[
228+
[date(1677, 1, 1), date(1677, 1, 2)],
229+
[datetime(1677, 1, 1, 12), datetime(1677, 1, 2, 12)],
230+
],
231+
)
232+
def test_conversion_outofbounds_datetime(self, dtc, values):
226233
# 2579
227-
values = [date(1677, 1, 1), date(1677, 1, 2)]
228-
rs = dtc.convert(values, None, None)
229-
xp = converter.mdates.date2num(values)
230-
tm.assert_numpy_array_equal(rs, xp)
231-
rs = dtc.convert(values[0], None, None)
232-
xp = converter.mdates.date2num(values[0])
233-
assert rs == xp
234-
235-
values = [datetime(1677, 1, 1, 12), datetime(1677, 1, 2, 12)]
236234
rs = dtc.convert(values, None, None)
237235
xp = converter.mdates.date2num(values)
238236
tm.assert_numpy_array_equal(rs, xp)

0 commit comments

Comments
 (0)