From 2f4b3dfab511233670c9af7e6a7885d09f75a327 Mon Sep 17 00:00:00 2001 From: Maxim Ivanov Date: Tue, 10 Nov 2020 12:05:27 +0700 Subject: [PATCH 1/6] TST: parametrize tests --- pandas/tests/plotting/test_frame.py | 66 +++++++++++++---------------- 1 file changed, 30 insertions(+), 36 deletions(-) diff --git a/pandas/tests/plotting/test_frame.py b/pandas/tests/plotting/test_frame.py index 11a46858ba281..adeb999253ec5 100644 --- a/pandas/tests/plotting/test_frame.py +++ b/pandas/tests/plotting/test_frame.py @@ -190,11 +190,11 @@ def test_color_single_series_list(self): df = DataFrame({"A": [1, 2, 3]}) _check_plot_works(df.plot, color=["red"]) - def test_rgb_tuple_color(self): + @pytest.mark.parametrize("color", [(1, 0, 0), (1, 0, 0, 0.5)]) + def test_rgb_tuple_color(self, color): # GH 16695 df = DataFrame({"x": [1, 2], "y": [3, 4]}) - _check_plot_works(df.plot, x="x", y="y", color=(1, 0, 0)) - _check_plot_works(df.plot, x="x", y="y", color=(1, 0, 0, 0.5)) + _check_plot_works(df.plot, x="x", y="y", color=color) def test_color_empty_string(self): df = DataFrame(np.random.randn(10, 2)) @@ -443,11 +443,21 @@ def test_subplots(self): for ax in axes: assert ax.get_legend() is None - def test_groupby_boxplot_sharey(self): + @pytest.mark.parametrize( + "kwargs, expected", + [ + # behavior without keyword + ({}, [True, False, True, False]), + # set sharey=True should be identical + ({"sharey": True}, [True, False, True, False]), + # sharey=False, all yticklabels should be visible + ({"sharey": False}, [True, True, True, True]), + ], + ) + def test_groupby_boxplot_sharey(self, kwargs, expected): # https://github.com/pandas-dev/pandas/issues/20968 # sharey can now be switched check whether the right # pair of axes is turned on or off - df = DataFrame( { "a": [-1.43, -0.15, -3.70, -1.43, -0.14], @@ -456,27 +466,25 @@ def test_groupby_boxplot_sharey(self): }, index=[0, 1, 2, 3, 4], ) - - # behavior without keyword - axes = df.groupby("c").boxplot() - expected = [True, False, True, False] - self._assert_ytickslabels_visibility(axes, expected) - - # set sharey=True should be identical - axes = df.groupby("c").boxplot(sharey=True) - expected = [True, False, True, False] - self._assert_ytickslabels_visibility(axes, expected) - - # sharey=False, all yticklabels should be visible - axes = df.groupby("c").boxplot(sharey=False) - expected = [True, True, True, True] + axes = df.groupby("c").boxplot(**kwargs) self._assert_ytickslabels_visibility(axes, expected) - def test_groupby_boxplot_sharex(self): + @pytest.mark.parametrize( + "kwargs, expected", + [ + # behavior without keyword + ({}, [True, True, True, True]), + # set sharex=False should be identical + ({"sharex": False}, [True, True, True, True]), + # sharex=True, xticklabels should be visible + # only for bottom plots + ({"sharex": True}, [False, False, True, True]), + ], + ) + def test_groupby_boxplot_sharex(self, kwargs, expected): # https://github.com/pandas-dev/pandas/issues/20968 # sharex can now be switched check whether the right # pair of axes is turned on or off - df = DataFrame( { "a": [-1.43, -0.15, -3.70, -1.43, -0.14], @@ -485,21 +493,7 @@ def test_groupby_boxplot_sharex(self): }, index=[0, 1, 2, 3, 4], ) - - # behavior without keyword - axes = df.groupby("c").boxplot() - expected = [True, True, True, True] - self._assert_xtickslabels_visibility(axes, expected) - - # set sharex=False should be identical - axes = df.groupby("c").boxplot(sharex=False) - expected = [True, True, True, True] - self._assert_xtickslabels_visibility(axes, expected) - - # sharex=True, yticklabels should be visible - # only for bottom plots - axes = df.groupby("c").boxplot(sharex=True) - expected = [False, False, True, True] + axes = df.groupby("c").boxplot(**kwargs) self._assert_xtickslabels_visibility(axes, expected) @pytest.mark.slow From f559fa99ff58c0ec3fe502a600ca34a731fc77df Mon Sep 17 00:00:00 2001 From: Maxim Ivanov Date: Tue, 10 Nov 2020 12:11:40 +0700 Subject: [PATCH 2/6] TST: create loop --- pandas/tests/plotting/test_frame.py | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/pandas/tests/plotting/test_frame.py b/pandas/tests/plotting/test_frame.py index adeb999253ec5..f7795c9861863 100644 --- a/pandas/tests/plotting/test_frame.py +++ b/pandas/tests/plotting/test_frame.py @@ -552,24 +552,12 @@ def test_subplots_timeseries_y_axis(self): } testdata = DataFrame(data) - ax_numeric = testdata.plot(y="numeric") - assert ( - ax_numeric.get_lines()[0].get_data()[1] == testdata["numeric"].values - ).all() - ax_timedelta = testdata.plot(y="timedelta") - assert ( - ax_timedelta.get_lines()[0].get_data()[1] == testdata["timedelta"].values - ).all() - ax_datetime_no_tz = testdata.plot(y="datetime_no_tz") - assert ( - ax_datetime_no_tz.get_lines()[0].get_data()[1] - == testdata["datetime_no_tz"].values - ).all() - ax_datetime_all_tz = testdata.plot(y="datetime_all_tz") - assert ( - ax_datetime_all_tz.get_lines()[0].get_data()[1] - == testdata["datetime_all_tz"].values - ).all() + y_cols = ["numeric", "timedelta", "datetime_no_tz", "datetime_all_tz"] + for col in y_cols: + ax = testdata.plot(y=col) + result = ax.get_lines()[0].get_data()[1] + expected = testdata[col].values + assert (result == expected).all() msg = "no numeric data to plot" with pytest.raises(TypeError, match=msg): From c4ee5438b1ab4301fa1a3e3573053d6ef29e02f2 Mon Sep 17 00:00:00 2001 From: Maxim Ivanov Date: Tue, 10 Nov 2020 12:14:37 +0700 Subject: [PATCH 3/6] TST: split into multiple and single column tests --- pandas/tests/plotting/test_frame.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/tests/plotting/test_frame.py b/pandas/tests/plotting/test_frame.py index f7795c9861863..a212ba96e0fd4 100644 --- a/pandas/tests/plotting/test_frame.py +++ b/pandas/tests/plotting/test_frame.py @@ -615,7 +615,7 @@ def test_subplots_timeseries_y_axis_not_supported(self): ).all() @pytest.mark.slow - def test_subplots_layout(self): + def test_subplots_layout_multi_column(self): # GH 6667 df = DataFrame(np.random.rand(10, 3), index=list(string.ascii_letters[:10])) @@ -648,7 +648,9 @@ def test_subplots_layout(self): with pytest.raises(ValueError): df.plot(subplots=True, layout=(-1, -1)) - # single column + @pytest.mark.slow + def test_subplots_layout_single_column(self): + # GH 6667 df = DataFrame(np.random.rand(10, 1), index=list(string.ascii_letters[:10])) axes = df.plot(subplots=True) self._check_axes_shape(axes, axes_num=1, layout=(1, 1)) From 9a488eb8b44fe0771acbfe6e2b34bed076aa8247 Mon Sep 17 00:00:00 2001 From: Maxim Ivanov Date: Tue, 10 Nov 2020 12:30:30 +0700 Subject: [PATCH 4/6] TST: parametrize test_bar_barwidth_position --- pandas/tests/plotting/test_frame.py | 55 ++++++++++++++++------------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/pandas/tests/plotting/test_frame.py b/pandas/tests/plotting/test_frame.py index a212ba96e0fd4..a5cf7b9d8c7f0 100644 --- a/pandas/tests/plotting/test_frame.py +++ b/pandas/tests/plotting/test_frame.py @@ -649,16 +649,25 @@ def test_subplots_layout_multi_column(self): df.plot(subplots=True, layout=(-1, -1)) @pytest.mark.slow - def test_subplots_layout_single_column(self): + @pytest.mark.parametrize( + "kwargs, expected_axes_num, expected_layout, expected_shape", + [ + ({}, 1, (1, 1), (1,)), + ({"layout": (3, 3)}, 1, (3, 3), (3, 3)), + ], + ) + def test_subplots_layout_single_column( + self, kwargs, expected_axes_num, expected_layout, expected_shape + ): # GH 6667 df = DataFrame(np.random.rand(10, 1), index=list(string.ascii_letters[:10])) - axes = df.plot(subplots=True) - self._check_axes_shape(axes, axes_num=1, layout=(1, 1)) - assert axes.shape == (1,) - - axes = df.plot(subplots=True, layout=(3, 3)) - self._check_axes_shape(axes, axes_num=1, layout=(3, 3)) - assert axes.shape == (3, 3) + axes = df.plot(subplots=True, **kwargs) + self._check_axes_shape( + axes, + axes_num=expected_axes_num, + layout=expected_layout, + ) + assert axes.shape == expected_shape @pytest.mark.slow def test_subplots_warnings(self): @@ -1050,24 +1059,20 @@ def test_bar_barwidth(self): assert r.get_height() == width @pytest.mark.slow - def test_bar_barwidth_position(self): + @pytest.mark.parametrize( + "kwargs", + [ + {"kind": "bar", "stacked": False}, + {"kind": "bar", "stacked": True}, + {"kind": "barh", "stacked": False}, + {"kind": "barh", "stacked": True}, + {"kind": "bar", "subplots": True}, + {"kind": "barh", "subplots": True}, + ], + ) + def test_bar_barwidth_position(self, kwargs): df = DataFrame(np.random.randn(5, 5)) - self._check_bar_alignment( - df, kind="bar", stacked=False, width=0.9, position=0.2 - ) - self._check_bar_alignment(df, kind="bar", stacked=True, width=0.9, position=0.2) - self._check_bar_alignment( - df, kind="barh", stacked=False, width=0.9, position=0.2 - ) - self._check_bar_alignment( - df, kind="barh", stacked=True, width=0.9, position=0.2 - ) - self._check_bar_alignment( - df, kind="bar", subplots=True, width=0.9, position=0.2 - ) - self._check_bar_alignment( - df, kind="barh", subplots=True, width=0.9, position=0.2 - ) + self._check_bar_alignment(df, width=0.9, position=0.2, **kwargs) @pytest.mark.slow def test_bar_barwidth_position_int(self): From 7f3492a8eee51d14712a025c345acd15f174705b Mon Sep 17 00:00:00 2001 From: Maxim Ivanov Date: Tue, 10 Nov 2020 12:38:54 +0700 Subject: [PATCH 5/6] TST: parametrize test_bar_align --- pandas/tests/plotting/test_frame.py | 64 ++++++++++++++++------------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/pandas/tests/plotting/test_frame.py b/pandas/tests/plotting/test_frame.py index a5cf7b9d8c7f0..316d77dee0f8f 100644 --- a/pandas/tests/plotting/test_frame.py +++ b/pandas/tests/plotting/test_frame.py @@ -1492,39 +1492,47 @@ def _check_bar_alignment( return axes @pytest.mark.slow - def test_bar_stacked_center(self): - # GH2157 - df = DataFrame({"A": [3] * 5, "B": list(range(5))}, index=range(5)) - self._check_bar_alignment(df, kind="bar", stacked=True) - self._check_bar_alignment(df, kind="bar", stacked=True, width=0.9) - self._check_bar_alignment(df, kind="barh", stacked=True) - self._check_bar_alignment(df, kind="barh", stacked=True, width=0.9) - - @pytest.mark.slow - def test_bar_center(self): - df = DataFrame({"A": [3] * 5, "B": list(range(5))}, index=range(5)) - self._check_bar_alignment(df, kind="bar", stacked=False) - self._check_bar_alignment(df, kind="bar", stacked=False, width=0.9) - self._check_bar_alignment(df, kind="barh", stacked=False) - self._check_bar_alignment(df, kind="barh", stacked=False, width=0.9) + @pytest.mark.parametrize( + "kwargs", + [ + # stacked center + dict(kind="bar", stacked=True), + dict(kind="bar", stacked=True, width=0.9), + dict(kind="barh", stacked=True), + dict(kind="barh", stacked=True, width=0.9), + # center + dict(kind="bar", stacked=False), + dict(kind="bar", stacked=False, width=0.9), + dict(kind="barh", stacked=False), + dict(kind="barh", stacked=False, width=0.9), + # subplots center + dict(kind="bar", subplots=True), + dict(kind="bar", subplots=True, width=0.9), + dict(kind="barh", subplots=True), + dict(kind="barh", subplots=True, width=0.9), - @pytest.mark.slow - def test_bar_subplots_center(self): + ], + ) + def test_bar_align_multiple_columns(self, kwargs): + # GH2157 df = DataFrame({"A": [3] * 5, "B": list(range(5))}, index=range(5)) - self._check_bar_alignment(df, kind="bar", subplots=True) - self._check_bar_alignment(df, kind="bar", subplots=True, width=0.9) - self._check_bar_alignment(df, kind="barh", subplots=True) - self._check_bar_alignment(df, kind="barh", subplots=True, width=0.9) + self._check_bar_alignment(df, **kwargs) @pytest.mark.slow - def test_bar_align_single_column(self): + @pytest.mark.parametrize( + "kwargs", + [ + dict(kind="bar", stacked=False), + dict(kind="bar", stacked=True), + dict(kind="barh", stacked=False), + dict(kind="barh", stacked=True), + dict(kind="bar", subplots=True), + dict(kind="barh", subplots=True), + ], + ) + def test_bar_align_single_column(self, kwargs): df = DataFrame(np.random.randn(5)) - self._check_bar_alignment(df, kind="bar", stacked=False) - self._check_bar_alignment(df, kind="bar", stacked=True) - self._check_bar_alignment(df, kind="barh", stacked=False) - self._check_bar_alignment(df, kind="barh", stacked=True) - self._check_bar_alignment(df, kind="bar", subplots=True) - self._check_bar_alignment(df, kind="barh", subplots=True) + self._check_bar_alignment(df, **kwargs) @pytest.mark.slow def test_bar_edge(self): From 7e6308bad90bfb423789446e84585c5e6008698f Mon Sep 17 00:00:00 2001 From: Maxim Ivanov Date: Tue, 10 Nov 2020 12:53:12 +0700 Subject: [PATCH 6/6] TST: parametrize align edge --- pandas/tests/plotting/test_frame.py | 43 +++++++++-------------------- 1 file changed, 13 insertions(+), 30 deletions(-) diff --git a/pandas/tests/plotting/test_frame.py b/pandas/tests/plotting/test_frame.py index 316d77dee0f8f..8336d0b25eea3 100644 --- a/pandas/tests/plotting/test_frame.py +++ b/pandas/tests/plotting/test_frame.py @@ -1510,7 +1510,19 @@ def _check_bar_alignment( dict(kind="bar", subplots=True, width=0.9), dict(kind="barh", subplots=True), dict(kind="barh", subplots=True, width=0.9), - + # align edge + dict(kind="bar", stacked=True, align="edge"), + dict(kind="bar", stacked=True, width=0.9, align="edge"), + dict(kind="barh", stacked=True, align="edge"), + dict(kind="barh", stacked=True, width=0.9, align="edge"), + dict(kind="bar", stacked=False, align="edge"), + dict(kind="bar", stacked=False, width=0.9, align="edge"), + dict(kind="barh", stacked=False, align="edge"), + dict(kind="barh", stacked=False, width=0.9, align="edge"), + dict(kind="bar", subplots=True, align="edge"), + dict(kind="bar", subplots=True, width=0.9, align="edge"), + dict(kind="barh", subplots=True, align="edge"), + dict(kind="barh", subplots=True, width=0.9, align="edge"), ], ) def test_bar_align_multiple_columns(self, kwargs): @@ -1534,35 +1546,6 @@ def test_bar_align_single_column(self, kwargs): df = DataFrame(np.random.randn(5)) self._check_bar_alignment(df, **kwargs) - @pytest.mark.slow - def test_bar_edge(self): - df = DataFrame({"A": [3] * 5, "B": list(range(5))}, index=range(5)) - - self._check_bar_alignment(df, kind="bar", stacked=True, align="edge") - self._check_bar_alignment(df, kind="bar", stacked=True, width=0.9, align="edge") - self._check_bar_alignment(df, kind="barh", stacked=True, align="edge") - self._check_bar_alignment( - df, kind="barh", stacked=True, width=0.9, align="edge" - ) - - self._check_bar_alignment(df, kind="bar", stacked=False, align="edge") - self._check_bar_alignment( - df, kind="bar", stacked=False, width=0.9, align="edge" - ) - self._check_bar_alignment(df, kind="barh", stacked=False, align="edge") - self._check_bar_alignment( - df, kind="barh", stacked=False, width=0.9, align="edge" - ) - - self._check_bar_alignment(df, kind="bar", subplots=True, align="edge") - self._check_bar_alignment( - df, kind="bar", subplots=True, width=0.9, align="edge" - ) - self._check_bar_alignment(df, kind="barh", subplots=True, align="edge") - self._check_bar_alignment( - df, kind="barh", subplots=True, width=0.9, align="edge" - ) - @pytest.mark.slow def test_bar_log_no_subplots(self): # GH3254, GH3298 matplotlib/matplotlib#1882, #1892