From 17cfac217c0182fe57dc2edf575976b541af4a57 Mon Sep 17 00:00:00 2001 From: Avinash Pancham Date: Mon, 12 Oct 2020 01:19:18 +0200 Subject: [PATCH 1/8] TST: Verify functioning of histtype argument (GH23992) --- pandas/tests/plotting/test_hist_method.py | 53 +++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/pandas/tests/plotting/test_hist_method.py b/pandas/tests/plotting/test_hist_method.py index d9a58e808661b..6a217b5db9349 100644 --- a/pandas/tests/plotting/test_hist_method.py +++ b/pandas/tests/plotting/test_hist_method.py @@ -129,6 +129,23 @@ def test_plot_fails_when_ax_differs_from_figure(self): with pytest.raises(AssertionError): self.ts.hist(ax=ax1, figure=fig2) + @pytest.mark.parametrize( + "histtype, expected, agg_func", + [ + ("bar", True, all), + ("barstacked", True, all), + ("step", False, any), + ("stepfilled", True, all), + ], + ) + def test_histtype_argument(self, histtype, expected, agg_func): + # GH23992 Verify functioning of histtype argument + ser = Series(np.random.randint(1, 10)) + ax = ser.hist(bins=12, histtype=histtype) + + result = agg_func(patch.fill for patch in ax.patches) + assert result == expected + @pytest.mark.parametrize( "by, expected_axes_num, expected_layout", [(None, 1, (1, 1)), ("b", 2, (1, 2))] ) @@ -365,6 +382,23 @@ def test_hist_column_order_unchanged(self, column, expected): assert result == expected + @pytest.mark.parametrize( + "histtype, expected, agg_func", + [ + ("bar", True, all), + ("barstacked", True, all), + ("step", False, any), + ("stepfilled", True, all), + ], + ) + def test_histtype_argument(self, histtype, expected, agg_func): + # GH23992 Verify functioning of histtype argument + df = DataFrame(np.random.randint(1, 10, size=(100, 2)), columns=["a", "b"]) + ax = df.hist(bins=12, histtype=histtype, layout=(-1, 1)) + + result = agg_func(patch.fill for row in ax for patch in row[0].patches) + assert result == expected + @pytest.mark.parametrize("by", [None, "c"]) @pytest.mark.parametrize("column", [None, "b"]) def test_hist_with_legend(self, by, column): @@ -595,3 +629,22 @@ def test_axis_share_xy(self): assert ax1._shared_y_axes.joined(ax1, ax2) assert ax2._shared_y_axes.joined(ax1, ax2) + + @pytest.mark.parametrize( + "histtype, expected, agg_func", + [ + ("bar", True, all), + ("barstacked", True, all), + ("step", False, any), + ("stepfilled", True, all), + ], + ) + def test_histtype_argument(self, histtype, expected, agg_func): + # GH23992 Verify functioning of histtype argument + df = DataFrame( + np.random.randint(1, 10, size=(100, 2)), columns=["a", "b"] + ).groupby("a") + ax = df.hist(bins=12, histtype=histtype, layout=(-1, 1)) + + result = agg_func(patch.fill for rows in ax for patch in rows[0][0].patches) + assert result == expected From 19776195d69dcfab5c5c0c9e7a1d56e0ebfaeea1 Mon Sep 17 00:00:00 2001 From: Avinash Pancham Date: Sun, 18 Oct 2020 23:36:17 +0200 Subject: [PATCH 2/8] Generalize checks and store in common.py --- pandas/tests/plotting/common.py | 21 ++++++++ pandas/tests/plotting/test_hist_method.py | 58 ++++++++++------------- 2 files changed, 46 insertions(+), 33 deletions(-) diff --git a/pandas/tests/plotting/common.py b/pandas/tests/plotting/common.py index 2a6bd97c93b8e..b269882097660 100644 --- a/pandas/tests/plotting/common.py +++ b/pandas/tests/plotting/common.py @@ -173,6 +173,27 @@ def _check_visible(self, collections, visible=True): for patch in collections: assert patch.get_visible() == visible + def _check_filled(self, ax, filled=True): + """ + Check each artist is filled or not + + Parameters + ---------- + ax : matplotlib Axes object or a numpy.ndarray of them + filled : bool + expected filling + """ + + def _check_patch_filling(artist): + for patch in artist.patches: + assert patch.fill == filled + + if isinstance(ax, np.ndarray): + for rows in ax.flatten(): + _check_patch_filling(rows) + else: + _check_patch_filling(ax) + def _get_colors_mapped(self, series, colors): unique = series.unique() # unique and colors length can be differed diff --git a/pandas/tests/plotting/test_hist_method.py b/pandas/tests/plotting/test_hist_method.py index 6a217b5db9349..ebf58ef7da2b9 100644 --- a/pandas/tests/plotting/test_hist_method.py +++ b/pandas/tests/plotting/test_hist_method.py @@ -130,21 +130,19 @@ def test_plot_fails_when_ax_differs_from_figure(self): self.ts.hist(ax=ax1, figure=fig2) @pytest.mark.parametrize( - "histtype, expected, agg_func", + "histtype, expected", [ - ("bar", True, all), - ("barstacked", True, all), - ("step", False, any), - ("stepfilled", True, all), + ("bar", True), + ("barstacked", True), + ("step", False), + ("stepfilled", True), ], ) - def test_histtype_argument(self, histtype, expected, agg_func): + def test_histtype_argument(self, histtype, expected): # GH23992 Verify functioning of histtype argument ser = Series(np.random.randint(1, 10)) - ax = ser.hist(bins=12, histtype=histtype) - - result = agg_func(patch.fill for patch in ax.patches) - assert result == expected + ax = ser.hist(histtype=histtype) + self._check_filled(ax, filled=expected) @pytest.mark.parametrize( "by, expected_axes_num, expected_layout", [(None, 1, (1, 1)), ("b", 2, (1, 2))] @@ -383,21 +381,19 @@ def test_hist_column_order_unchanged(self, column, expected): assert result == expected @pytest.mark.parametrize( - "histtype, expected, agg_func", + "histtype, expected", [ - ("bar", True, all), - ("barstacked", True, all), - ("step", False, any), - ("stepfilled", True, all), + ("bar", True), + ("barstacked", True), + ("step", False), + ("stepfilled", True), ], ) - def test_histtype_argument(self, histtype, expected, agg_func): + def test_histtype_argument(self, histtype, expected): # GH23992 Verify functioning of histtype argument df = DataFrame(np.random.randint(1, 10, size=(100, 2)), columns=["a", "b"]) - ax = df.hist(bins=12, histtype=histtype, layout=(-1, 1)) - - result = agg_func(patch.fill for row in ax for patch in row[0].patches) - assert result == expected + ax = df.hist(histtype=histtype) + self._check_filled(ax, filled=expected) @pytest.mark.parametrize("by", [None, "c"]) @pytest.mark.parametrize("column", [None, "b"]) @@ -631,20 +627,16 @@ def test_axis_share_xy(self): assert ax2._shared_y_axes.joined(ax1, ax2) @pytest.mark.parametrize( - "histtype, expected, agg_func", + "histtype, expected", [ - ("bar", True, all), - ("barstacked", True, all), - ("step", False, any), - ("stepfilled", True, all), + ("bar", True), + ("barstacked", True), + ("step", False), + ("stepfilled", True), ], ) - def test_histtype_argument(self, histtype, expected, agg_func): + def test_histtype_argument(self, histtype, expected): # GH23992 Verify functioning of histtype argument - df = DataFrame( - np.random.randint(1, 10, size=(100, 2)), columns=["a", "b"] - ).groupby("a") - ax = df.hist(bins=12, histtype=histtype, layout=(-1, 1)) - - result = agg_func(patch.fill for rows in ax for patch in rows[0][0].patches) - assert result == expected + df = DataFrame(np.random.randint(1, 10, size=(100, 2)), columns=["a", "b"]) + ax = df.hist(by="a", histtype=histtype) + self._check_filled(ax, filled=expected) From a59118b0f9dc33d5a83b6d6a180a9a6c65e2117f Mon Sep 17 00:00:00 2001 From: Avinash Pancham Date: Mon, 26 Oct 2020 21:47:29 +0100 Subject: [PATCH 3/8] Update _check_filling function --- pandas/tests/plotting/common.py | 17 ++++++----------- pandas/tests/plotting/test_hist_method.py | 6 +++--- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/pandas/tests/plotting/common.py b/pandas/tests/plotting/common.py index b269882097660..0c92281196752 100644 --- a/pandas/tests/plotting/common.py +++ b/pandas/tests/plotting/common.py @@ -173,27 +173,22 @@ def _check_visible(self, collections, visible=True): for patch in collections: assert patch.get_visible() == visible - def _check_filled(self, ax, filled=True): + def _check_filling(self, axes, filled=True): """ - Check each artist is filled or not + Check for each artist whether it is filled or not Parameters ---------- - ax : matplotlib Axes object or a numpy.ndarray of them + axes : matplotlib Axes object, or its list-like filled : bool expected filling """ - def _check_patch_filling(artist): - for patch in artist.patches: + axes = self._flatten_visible(axes) + for ax in axes: + for patch in ax.patches: assert patch.fill == filled - if isinstance(ax, np.ndarray): - for rows in ax.flatten(): - _check_patch_filling(rows) - else: - _check_patch_filling(ax) - def _get_colors_mapped(self, series, colors): unique = series.unique() # unique and colors length can be differed diff --git a/pandas/tests/plotting/test_hist_method.py b/pandas/tests/plotting/test_hist_method.py index ebf58ef7da2b9..6d0cca7838163 100644 --- a/pandas/tests/plotting/test_hist_method.py +++ b/pandas/tests/plotting/test_hist_method.py @@ -142,7 +142,7 @@ def test_histtype_argument(self, histtype, expected): # GH23992 Verify functioning of histtype argument ser = Series(np.random.randint(1, 10)) ax = ser.hist(histtype=histtype) - self._check_filled(ax, filled=expected) + self._check_filling(ax, filled=expected) @pytest.mark.parametrize( "by, expected_axes_num, expected_layout", [(None, 1, (1, 1)), ("b", 2, (1, 2))] @@ -393,7 +393,7 @@ def test_histtype_argument(self, histtype, expected): # GH23992 Verify functioning of histtype argument df = DataFrame(np.random.randint(1, 10, size=(100, 2)), columns=["a", "b"]) ax = df.hist(histtype=histtype) - self._check_filled(ax, filled=expected) + self._check_filling(ax, filled=expected) @pytest.mark.parametrize("by", [None, "c"]) @pytest.mark.parametrize("column", [None, "b"]) @@ -639,4 +639,4 @@ def test_histtype_argument(self, histtype, expected): # GH23992 Verify functioning of histtype argument df = DataFrame(np.random.randint(1, 10, size=(100, 2)), columns=["a", "b"]) ax = df.hist(by="a", histtype=histtype) - self._check_filled(ax, filled=expected) + self._check_filling(ax, filled=expected) From e45686e1845fc1e12264f7ef621bf84c944887df Mon Sep 17 00:00:00 2001 From: Avinash Pancham Date: Wed, 28 Oct 2020 16:37:32 +0100 Subject: [PATCH 4/8] Add type hinting --- pandas/tests/plotting/common.py | 6 +++++- pandas/tests/plotting/test_hist_method.py | 6 +++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/pandas/tests/plotting/common.py b/pandas/tests/plotting/common.py index 0c92281196752..0da94e14522ee 100644 --- a/pandas/tests/plotting/common.py +++ b/pandas/tests/plotting/common.py @@ -1,6 +1,8 @@ import os +from typing import Sequence, Union import warnings +from matplotlib.axes import Axes import numpy as np from numpy import random @@ -173,7 +175,9 @@ def _check_visible(self, collections, visible=True): for patch in collections: assert patch.get_visible() == visible - def _check_filling(self, axes, filled=True): + def _check_filling( + self, axes: Union[Axes, Sequence[Axes]], filled: bool = True + ) -> None: """ Check for each artist whether it is filled or not diff --git a/pandas/tests/plotting/test_hist_method.py b/pandas/tests/plotting/test_hist_method.py index 6d0cca7838163..0c117905bcc72 100644 --- a/pandas/tests/plotting/test_hist_method.py +++ b/pandas/tests/plotting/test_hist_method.py @@ -138,7 +138,7 @@ def test_plot_fails_when_ax_differs_from_figure(self): ("stepfilled", True), ], ) - def test_histtype_argument(self, histtype, expected): + def test_histtype_argument(self, histtype: str, expected: bool) -> None: # GH23992 Verify functioning of histtype argument ser = Series(np.random.randint(1, 10)) ax = ser.hist(histtype=histtype) @@ -389,7 +389,7 @@ def test_hist_column_order_unchanged(self, column, expected): ("stepfilled", True), ], ) - def test_histtype_argument(self, histtype, expected): + def test_histtype_argument(self, histtype: str, expected: bool) -> None: # GH23992 Verify functioning of histtype argument df = DataFrame(np.random.randint(1, 10, size=(100, 2)), columns=["a", "b"]) ax = df.hist(histtype=histtype) @@ -635,7 +635,7 @@ def test_axis_share_xy(self): ("stepfilled", True), ], ) - def test_histtype_argument(self, histtype, expected): + def test_histtype_argument(self, histtype: str, expected: bool) -> None: # GH23992 Verify functioning of histtype argument df = DataFrame(np.random.randint(1, 10, size=(100, 2)), columns=["a", "b"]) ax = df.hist(by="a", histtype=histtype) From db905746c964116cc909c893961371496a51e093 Mon Sep 17 00:00:00 2001 From: Avinash Pancham Date: Wed, 28 Oct 2020 17:27:45 +0100 Subject: [PATCH 5/8] Update Axis reference to be in line with plotting/_matplotlib/tools.py --- pandas/tests/plotting/common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/plotting/common.py b/pandas/tests/plotting/common.py index 0da94e14522ee..48bd16dd96675 100644 --- a/pandas/tests/plotting/common.py +++ b/pandas/tests/plotting/common.py @@ -176,7 +176,7 @@ def _check_visible(self, collections, visible=True): assert patch.get_visible() == visible def _check_filling( - self, axes: Union[Axes, Sequence[Axes]], filled: bool = True + self, axes: Union["Axes", Sequence["Axes"]], filled: bool = True ) -> None: """ Check for each artist whether it is filled or not From d89f98b7b34d71aaac6e6acfcd2817303239ad22 Mon Sep 17 00:00:00 2001 From: Avinash Pancham Date: Fri, 30 Oct 2020 21:09:42 +0100 Subject: [PATCH 6/8] Add conditional import of matplotlib --- pandas/tests/plotting/common.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/tests/plotting/common.py b/pandas/tests/plotting/common.py index 48bd16dd96675..d2f99a57d9857 100644 --- a/pandas/tests/plotting/common.py +++ b/pandas/tests/plotting/common.py @@ -1,8 +1,7 @@ import os -from typing import Sequence, Union +from typing import TYPE_CHECKING, Sequence, Union import warnings -from matplotlib.axes import Axes import numpy as np from numpy import random @@ -15,6 +14,9 @@ from pandas import DataFrame, Series, to_datetime import pandas._testing as tm +if TYPE_CHECKING: + from matplotlib.axes import Axes + @td.skip_if_no_mpl class TestPlotBase: From 1581af13daa769b11c7d6af7216a9324749c5e80 Mon Sep 17 00:00:00 2001 From: Avinash Pancham Date: Fri, 30 Oct 2020 21:10:59 +0100 Subject: [PATCH 7/8] Remove type hinting from tests --- pandas/tests/plotting/test_hist_method.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/tests/plotting/test_hist_method.py b/pandas/tests/plotting/test_hist_method.py index 0c117905bcc72..6d0cca7838163 100644 --- a/pandas/tests/plotting/test_hist_method.py +++ b/pandas/tests/plotting/test_hist_method.py @@ -138,7 +138,7 @@ def test_plot_fails_when_ax_differs_from_figure(self): ("stepfilled", True), ], ) - def test_histtype_argument(self, histtype: str, expected: bool) -> None: + def test_histtype_argument(self, histtype, expected): # GH23992 Verify functioning of histtype argument ser = Series(np.random.randint(1, 10)) ax = ser.hist(histtype=histtype) @@ -389,7 +389,7 @@ def test_hist_column_order_unchanged(self, column, expected): ("stepfilled", True), ], ) - def test_histtype_argument(self, histtype: str, expected: bool) -> None: + def test_histtype_argument(self, histtype, expected): # GH23992 Verify functioning of histtype argument df = DataFrame(np.random.randint(1, 10, size=(100, 2)), columns=["a", "b"]) ax = df.hist(histtype=histtype) @@ -635,7 +635,7 @@ def test_axis_share_xy(self): ("stepfilled", True), ], ) - def test_histtype_argument(self, histtype: str, expected: bool) -> None: + def test_histtype_argument(self, histtype, expected): # GH23992 Verify functioning of histtype argument df = DataFrame(np.random.randint(1, 10, size=(100, 2)), columns=["a", "b"]) ax = df.hist(by="a", histtype=histtype) From d728305e61ced45c733d9e5f717ca0fc9b59bf42 Mon Sep 17 00:00:00 2001 From: Avinash Pancham Date: Sat, 31 Oct 2020 16:46:18 +0100 Subject: [PATCH 8/8] Rename function as requested --- pandas/tests/plotting/common.py | 2 +- pandas/tests/plotting/test_hist_method.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/tests/plotting/common.py b/pandas/tests/plotting/common.py index f507db0d6b307..82a62c4588b94 100644 --- a/pandas/tests/plotting/common.py +++ b/pandas/tests/plotting/common.py @@ -176,7 +176,7 @@ def _check_visible(self, collections, visible=True): for patch in collections: assert patch.get_visible() == visible - def _check_filling( + def _check_patches_all_filled( self, axes: Union["Axes", Sequence["Axes"]], filled: bool = True ) -> None: """ diff --git a/pandas/tests/plotting/test_hist_method.py b/pandas/tests/plotting/test_hist_method.py index 4c994eebbb989..49335230171c6 100644 --- a/pandas/tests/plotting/test_hist_method.py +++ b/pandas/tests/plotting/test_hist_method.py @@ -141,7 +141,7 @@ def test_histtype_argument(self, histtype, expected): # GH23992 Verify functioning of histtype argument ser = Series(np.random.randint(1, 10)) ax = ser.hist(histtype=histtype) - self._check_filling(ax, filled=expected) + self._check_patches_all_filled(ax, filled=expected) @pytest.mark.parametrize( "by, expected_axes_num, expected_layout", [(None, 1, (1, 1)), ("b", 2, (1, 2))] @@ -392,7 +392,7 @@ def test_histtype_argument(self, histtype, expected): # GH23992 Verify functioning of histtype argument df = DataFrame(np.random.randint(1, 10, size=(100, 2)), columns=["a", "b"]) ax = df.hist(histtype=histtype) - self._check_filling(ax, filled=expected) + self._check_patches_all_filled(ax, filled=expected) @pytest.mark.parametrize("by", [None, "c"]) @pytest.mark.parametrize("column", [None, "b"]) @@ -638,4 +638,4 @@ def test_histtype_argument(self, histtype, expected): # GH23992 Verify functioning of histtype argument df = DataFrame(np.random.randint(1, 10, size=(100, 2)), columns=["a", "b"]) ax = df.hist(by="a", histtype=histtype) - self._check_filling(ax, filled=expected) + self._check_patches_all_filled(ax, filled=expected)