Skip to content

Commit 365d843

Browse files
authored
TST: parametrize in tests/plotting/test_frame.py (pandas-dev#37735)
1 parent 0ce9eb2 commit 365d843

File tree

1 file changed

+117
-137
lines changed

1 file changed

+117
-137
lines changed

pandas/tests/plotting/test_frame.py

+117-137
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,11 @@ def test_color_single_series_list(self):
197197
df = DataFrame({"A": [1, 2, 3]})
198198
_check_plot_works(df.plot, color=["red"])
199199

200-
def test_rgb_tuple_color(self):
200+
@pytest.mark.parametrize("color", [(1, 0, 0), (1, 0, 0, 0.5)])
201+
def test_rgb_tuple_color(self, color):
201202
# GH 16695
202203
df = DataFrame({"x": [1, 2], "y": [3, 4]})
203-
_check_plot_works(df.plot, x="x", y="y", color=(1, 0, 0))
204-
_check_plot_works(df.plot, x="x", y="y", color=(1, 0, 0, 0.5))
204+
_check_plot_works(df.plot, x="x", y="y", color=color)
205205

206206
def test_color_empty_string(self):
207207
df = DataFrame(np.random.randn(10, 2))
@@ -450,11 +450,21 @@ def test_subplots(self):
450450
for ax in axes:
451451
assert ax.get_legend() is None
452452

453-
def test_groupby_boxplot_sharey(self):
453+
@pytest.mark.parametrize(
454+
"kwargs, expected",
455+
[
456+
# behavior without keyword
457+
({}, [True, False, True, False]),
458+
# set sharey=True should be identical
459+
({"sharey": True}, [True, False, True, False]),
460+
# sharey=False, all yticklabels should be visible
461+
({"sharey": False}, [True, True, True, True]),
462+
],
463+
)
464+
def test_groupby_boxplot_sharey(self, kwargs, expected):
454465
# https://github.com/pandas-dev/pandas/issues/20968
455466
# sharey can now be switched check whether the right
456467
# pair of axes is turned on or off
457-
458468
df = DataFrame(
459469
{
460470
"a": [-1.43, -0.15, -3.70, -1.43, -0.14],
@@ -463,27 +473,25 @@ def test_groupby_boxplot_sharey(self):
463473
},
464474
index=[0, 1, 2, 3, 4],
465475
)
466-
467-
# behavior without keyword
468-
axes = df.groupby("c").boxplot()
469-
expected = [True, False, True, False]
470-
self._assert_ytickslabels_visibility(axes, expected)
471-
472-
# set sharey=True should be identical
473-
axes = df.groupby("c").boxplot(sharey=True)
474-
expected = [True, False, True, False]
475-
self._assert_ytickslabels_visibility(axes, expected)
476-
477-
# sharey=False, all yticklabels should be visible
478-
axes = df.groupby("c").boxplot(sharey=False)
479-
expected = [True, True, True, True]
476+
axes = df.groupby("c").boxplot(**kwargs)
480477
self._assert_ytickslabels_visibility(axes, expected)
481478

482-
def test_groupby_boxplot_sharex(self):
479+
@pytest.mark.parametrize(
480+
"kwargs, expected",
481+
[
482+
# behavior without keyword
483+
({}, [True, True, True, True]),
484+
# set sharex=False should be identical
485+
({"sharex": False}, [True, True, True, True]),
486+
# sharex=True, xticklabels should be visible
487+
# only for bottom plots
488+
({"sharex": True}, [False, False, True, True]),
489+
],
490+
)
491+
def test_groupby_boxplot_sharex(self, kwargs, expected):
483492
# https://github.com/pandas-dev/pandas/issues/20968
484493
# sharex can now be switched check whether the right
485494
# pair of axes is turned on or off
486-
487495
df = DataFrame(
488496
{
489497
"a": [-1.43, -0.15, -3.70, -1.43, -0.14],
@@ -492,21 +500,7 @@ def test_groupby_boxplot_sharex(self):
492500
},
493501
index=[0, 1, 2, 3, 4],
494502
)
495-
496-
# behavior without keyword
497-
axes = df.groupby("c").boxplot()
498-
expected = [True, True, True, True]
499-
self._assert_xtickslabels_visibility(axes, expected)
500-
501-
# set sharex=False should be identical
502-
axes = df.groupby("c").boxplot(sharex=False)
503-
expected = [True, True, True, True]
504-
self._assert_xtickslabels_visibility(axes, expected)
505-
506-
# sharex=True, yticklabels should be visible
507-
# only for bottom plots
508-
axes = df.groupby("c").boxplot(sharex=True)
509-
expected = [False, False, True, True]
503+
axes = df.groupby("c").boxplot(**kwargs)
510504
self._assert_xtickslabels_visibility(axes, expected)
511505

512506
@pytest.mark.slow
@@ -565,24 +559,12 @@ def test_subplots_timeseries_y_axis(self):
565559
}
566560
testdata = DataFrame(data)
567561

568-
ax_numeric = testdata.plot(y="numeric")
569-
assert (
570-
ax_numeric.get_lines()[0].get_data()[1] == testdata["numeric"].values
571-
).all()
572-
ax_timedelta = testdata.plot(y="timedelta")
573-
assert (
574-
ax_timedelta.get_lines()[0].get_data()[1] == testdata["timedelta"].values
575-
).all()
576-
ax_datetime_no_tz = testdata.plot(y="datetime_no_tz")
577-
assert (
578-
ax_datetime_no_tz.get_lines()[0].get_data()[1]
579-
== testdata["datetime_no_tz"].values
580-
).all()
581-
ax_datetime_all_tz = testdata.plot(y="datetime_all_tz")
582-
assert (
583-
ax_datetime_all_tz.get_lines()[0].get_data()[1]
584-
== testdata["datetime_all_tz"].values
585-
).all()
562+
y_cols = ["numeric", "timedelta", "datetime_no_tz", "datetime_all_tz"]
563+
for col in y_cols:
564+
ax = testdata.plot(y=col)
565+
result = ax.get_lines()[0].get_data()[1]
566+
expected = testdata[col].values
567+
assert (result == expected).all()
586568

587569
msg = "no numeric data to plot"
588570
with pytest.raises(TypeError, match=msg):
@@ -640,7 +622,7 @@ def test_subplots_timeseries_y_axis_not_supported(self):
640622
).all()
641623

642624
@pytest.mark.slow
643-
def test_subplots_layout(self):
625+
def test_subplots_layout_multi_column(self):
644626
# GH 6667
645627
df = DataFrame(np.random.rand(10, 3), index=list(string.ascii_letters[:10]))
646628

@@ -673,15 +655,26 @@ def test_subplots_layout(self):
673655
with pytest.raises(ValueError):
674656
df.plot(subplots=True, layout=(-1, -1))
675657

676-
# single column
658+
@pytest.mark.slow
659+
@pytest.mark.parametrize(
660+
"kwargs, expected_axes_num, expected_layout, expected_shape",
661+
[
662+
({}, 1, (1, 1), (1,)),
663+
({"layout": (3, 3)}, 1, (3, 3), (3, 3)),
664+
],
665+
)
666+
def test_subplots_layout_single_column(
667+
self, kwargs, expected_axes_num, expected_layout, expected_shape
668+
):
669+
# GH 6667
677670
df = DataFrame(np.random.rand(10, 1), index=list(string.ascii_letters[:10]))
678-
axes = df.plot(subplots=True)
679-
self._check_axes_shape(axes, axes_num=1, layout=(1, 1))
680-
assert axes.shape == (1,)
681-
682-
axes = df.plot(subplots=True, layout=(3, 3))
683-
self._check_axes_shape(axes, axes_num=1, layout=(3, 3))
684-
assert axes.shape == (3, 3)
671+
axes = df.plot(subplots=True, **kwargs)
672+
self._check_axes_shape(
673+
axes,
674+
axes_num=expected_axes_num,
675+
layout=expected_layout,
676+
)
677+
assert axes.shape == expected_shape
685678

686679
@pytest.mark.slow
687680
def test_subplots_warnings(self):
@@ -1073,24 +1066,20 @@ def test_bar_barwidth(self):
10731066
assert r.get_height() == width
10741067

10751068
@pytest.mark.slow
1076-
def test_bar_barwidth_position(self):
1069+
@pytest.mark.parametrize(
1070+
"kwargs",
1071+
[
1072+
{"kind": "bar", "stacked": False},
1073+
{"kind": "bar", "stacked": True},
1074+
{"kind": "barh", "stacked": False},
1075+
{"kind": "barh", "stacked": True},
1076+
{"kind": "bar", "subplots": True},
1077+
{"kind": "barh", "subplots": True},
1078+
],
1079+
)
1080+
def test_bar_barwidth_position(self, kwargs):
10771081
df = DataFrame(np.random.randn(5, 5))
1078-
self._check_bar_alignment(
1079-
df, kind="bar", stacked=False, width=0.9, position=0.2
1080-
)
1081-
self._check_bar_alignment(df, kind="bar", stacked=True, width=0.9, position=0.2)
1082-
self._check_bar_alignment(
1083-
df, kind="barh", stacked=False, width=0.9, position=0.2
1084-
)
1085-
self._check_bar_alignment(
1086-
df, kind="barh", stacked=True, width=0.9, position=0.2
1087-
)
1088-
self._check_bar_alignment(
1089-
df, kind="bar", subplots=True, width=0.9, position=0.2
1090-
)
1091-
self._check_bar_alignment(
1092-
df, kind="barh", subplots=True, width=0.9, position=0.2
1093-
)
1082+
self._check_bar_alignment(df, width=0.9, position=0.2, **kwargs)
10941083

10951084
@pytest.mark.slow
10961085
def test_bar_barwidth_position_int(self):
@@ -1508,68 +1497,59 @@ def _check_bar_alignment(
15081497
return axes
15091498

15101499
@pytest.mark.slow
1511-
def test_bar_stacked_center(self):
1500+
@pytest.mark.parametrize(
1501+
"kwargs",
1502+
[
1503+
# stacked center
1504+
dict(kind="bar", stacked=True),
1505+
dict(kind="bar", stacked=True, width=0.9),
1506+
dict(kind="barh", stacked=True),
1507+
dict(kind="barh", stacked=True, width=0.9),
1508+
# center
1509+
dict(kind="bar", stacked=False),
1510+
dict(kind="bar", stacked=False, width=0.9),
1511+
dict(kind="barh", stacked=False),
1512+
dict(kind="barh", stacked=False, width=0.9),
1513+
# subplots center
1514+
dict(kind="bar", subplots=True),
1515+
dict(kind="bar", subplots=True, width=0.9),
1516+
dict(kind="barh", subplots=True),
1517+
dict(kind="barh", subplots=True, width=0.9),
1518+
# align edge
1519+
dict(kind="bar", stacked=True, align="edge"),
1520+
dict(kind="bar", stacked=True, width=0.9, align="edge"),
1521+
dict(kind="barh", stacked=True, align="edge"),
1522+
dict(kind="barh", stacked=True, width=0.9, align="edge"),
1523+
dict(kind="bar", stacked=False, align="edge"),
1524+
dict(kind="bar", stacked=False, width=0.9, align="edge"),
1525+
dict(kind="barh", stacked=False, align="edge"),
1526+
dict(kind="barh", stacked=False, width=0.9, align="edge"),
1527+
dict(kind="bar", subplots=True, align="edge"),
1528+
dict(kind="bar", subplots=True, width=0.9, align="edge"),
1529+
dict(kind="barh", subplots=True, align="edge"),
1530+
dict(kind="barh", subplots=True, width=0.9, align="edge"),
1531+
],
1532+
)
1533+
def test_bar_align_multiple_columns(self, kwargs):
15121534
# GH2157
15131535
df = DataFrame({"A": [3] * 5, "B": list(range(5))}, index=range(5))
1514-
self._check_bar_alignment(df, kind="bar", stacked=True)
1515-
self._check_bar_alignment(df, kind="bar", stacked=True, width=0.9)
1516-
self._check_bar_alignment(df, kind="barh", stacked=True)
1517-
self._check_bar_alignment(df, kind="barh", stacked=True, width=0.9)
1518-
1519-
@pytest.mark.slow
1520-
def test_bar_center(self):
1521-
df = DataFrame({"A": [3] * 5, "B": list(range(5))}, index=range(5))
1522-
self._check_bar_alignment(df, kind="bar", stacked=False)
1523-
self._check_bar_alignment(df, kind="bar", stacked=False, width=0.9)
1524-
self._check_bar_alignment(df, kind="barh", stacked=False)
1525-
self._check_bar_alignment(df, kind="barh", stacked=False, width=0.9)
1536+
self._check_bar_alignment(df, **kwargs)
15261537

15271538
@pytest.mark.slow
1528-
def test_bar_subplots_center(self):
1529-
df = DataFrame({"A": [3] * 5, "B": list(range(5))}, index=range(5))
1530-
self._check_bar_alignment(df, kind="bar", subplots=True)
1531-
self._check_bar_alignment(df, kind="bar", subplots=True, width=0.9)
1532-
self._check_bar_alignment(df, kind="barh", subplots=True)
1533-
self._check_bar_alignment(df, kind="barh", subplots=True, width=0.9)
1534-
1535-
@pytest.mark.slow
1536-
def test_bar_align_single_column(self):
1539+
@pytest.mark.parametrize(
1540+
"kwargs",
1541+
[
1542+
dict(kind="bar", stacked=False),
1543+
dict(kind="bar", stacked=True),
1544+
dict(kind="barh", stacked=False),
1545+
dict(kind="barh", stacked=True),
1546+
dict(kind="bar", subplots=True),
1547+
dict(kind="barh", subplots=True),
1548+
],
1549+
)
1550+
def test_bar_align_single_column(self, kwargs):
15371551
df = DataFrame(np.random.randn(5))
1538-
self._check_bar_alignment(df, kind="bar", stacked=False)
1539-
self._check_bar_alignment(df, kind="bar", stacked=True)
1540-
self._check_bar_alignment(df, kind="barh", stacked=False)
1541-
self._check_bar_alignment(df, kind="barh", stacked=True)
1542-
self._check_bar_alignment(df, kind="bar", subplots=True)
1543-
self._check_bar_alignment(df, kind="barh", subplots=True)
1544-
1545-
@pytest.mark.slow
1546-
def test_bar_edge(self):
1547-
df = DataFrame({"A": [3] * 5, "B": list(range(5))}, index=range(5))
1548-
1549-
self._check_bar_alignment(df, kind="bar", stacked=True, align="edge")
1550-
self._check_bar_alignment(df, kind="bar", stacked=True, width=0.9, align="edge")
1551-
self._check_bar_alignment(df, kind="barh", stacked=True, align="edge")
1552-
self._check_bar_alignment(
1553-
df, kind="barh", stacked=True, width=0.9, align="edge"
1554-
)
1555-
1556-
self._check_bar_alignment(df, kind="bar", stacked=False, align="edge")
1557-
self._check_bar_alignment(
1558-
df, kind="bar", stacked=False, width=0.9, align="edge"
1559-
)
1560-
self._check_bar_alignment(df, kind="barh", stacked=False, align="edge")
1561-
self._check_bar_alignment(
1562-
df, kind="barh", stacked=False, width=0.9, align="edge"
1563-
)
1564-
1565-
self._check_bar_alignment(df, kind="bar", subplots=True, align="edge")
1566-
self._check_bar_alignment(
1567-
df, kind="bar", subplots=True, width=0.9, align="edge"
1568-
)
1569-
self._check_bar_alignment(df, kind="barh", subplots=True, align="edge")
1570-
self._check_bar_alignment(
1571-
df, kind="barh", subplots=True, width=0.9, align="edge"
1572-
)
1552+
self._check_bar_alignment(df, **kwargs)
15731553

15741554
@pytest.mark.slow
15751555
def test_bar_log_no_subplots(self):

0 commit comments

Comments
 (0)