|
2 | 2 |
|
3 | 3 |
|
4 | 4 | import numpy as np
|
| 5 | +import pytest |
5 | 6 |
|
6 | 7 | import pandas.util._test_decorators as td
|
7 | 8 |
|
8 |
| -from pandas import DataFrame, Series |
| 9 | +from pandas import DataFrame, Index, Series |
9 | 10 | import pandas._testing as tm
|
10 | 11 | from pandas.tests.plotting.common import TestPlotBase
|
11 | 12 |
|
@@ -65,3 +66,49 @@ def test_plot_kwargs(self):
|
65 | 66 |
|
66 | 67 | res = df.groupby("z").plot.scatter(x="x", y="y")
|
67 | 68 | assert len(res["a"].collections) == 1
|
| 69 | + |
| 70 | + @pytest.mark.parametrize("column, expected_axes_num", [(None, 2), ("b", 1)]) |
| 71 | + def test_groupby_hist_frame_with_legend(self, column, expected_axes_num): |
| 72 | + # GH 6279 - DataFrameGroupBy histogram can have a legend |
| 73 | + expected_layout = (1, expected_axes_num) |
| 74 | + expected_labels = column or [["a"], ["b"]] |
| 75 | + |
| 76 | + index = Index(15 * ["1"] + 15 * ["2"], name="c") |
| 77 | + df = DataFrame(np.random.randn(30, 2), index=index, columns=["a", "b"]) |
| 78 | + g = df.groupby("c") |
| 79 | + |
| 80 | + for axes in g.hist(legend=True, column=column): |
| 81 | + self._check_axes_shape( |
| 82 | + axes, axes_num=expected_axes_num, layout=expected_layout |
| 83 | + ) |
| 84 | + for ax, expected_label in zip(axes[0], expected_labels): |
| 85 | + self._check_legend_labels(ax, expected_label) |
| 86 | + |
| 87 | + @pytest.mark.parametrize("column", [None, "b"]) |
| 88 | + def test_groupby_hist_frame_with_legend_raises(self, column): |
| 89 | + # GH 6279 - DataFrameGroupBy histogram with legend and label raises |
| 90 | + index = Index(15 * ["1"] + 15 * ["2"], name="c") |
| 91 | + df = DataFrame(np.random.randn(30, 2), index=index, columns=["a", "b"]) |
| 92 | + g = df.groupby("c") |
| 93 | + |
| 94 | + with pytest.raises(ValueError, match="Cannot use both legend and label"): |
| 95 | + g.hist(legend=True, column=column, label="d") |
| 96 | + |
| 97 | + def test_groupby_hist_series_with_legend(self): |
| 98 | + # GH 6279 - SeriesGroupBy histogram can have a legend |
| 99 | + index = Index(15 * ["1"] + 15 * ["2"], name="c") |
| 100 | + df = DataFrame(np.random.randn(30, 2), index=index, columns=["a", "b"]) |
| 101 | + g = df.groupby("c") |
| 102 | + |
| 103 | + for ax in g["a"].hist(legend=True): |
| 104 | + self._check_axes_shape(ax, axes_num=1, layout=(1, 1)) |
| 105 | + self._check_legend_labels(ax, ["1", "2"]) |
| 106 | + |
| 107 | + def test_groupby_hist_series_with_legend_raises(self): |
| 108 | + # GH 6279 - SeriesGroupBy histogram with legend and label raises |
| 109 | + index = Index(15 * ["1"] + 15 * ["2"], name="c") |
| 110 | + df = DataFrame(np.random.randn(30, 2), index=index, columns=["a", "b"]) |
| 111 | + g = df.groupby("c") |
| 112 | + |
| 113 | + with pytest.raises(ValueError, match="Cannot use both legend and label"): |
| 114 | + g.hist(legend=True, label="d") |
0 commit comments