test_frame_groupby.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. """ Test cases for DataFrame.plot """
  2. import pytest
  3. import pandas.util._test_decorators as td
  4. from pandas import DataFrame
  5. from pandas.tests.plotting.common import TestPlotBase
  6. @td.skip_if_no_mpl
  7. class TestDataFramePlotsGroupby(TestPlotBase):
  8. def _assert_ytickslabels_visibility(self, axes, expected):
  9. for ax, exp in zip(axes, expected):
  10. self._check_visible(ax.get_yticklabels(), visible=exp)
  11. def _assert_xtickslabels_visibility(self, axes, expected):
  12. for ax, exp in zip(axes, expected):
  13. self._check_visible(ax.get_xticklabels(), visible=exp)
  14. @pytest.mark.parametrize(
  15. "kwargs, expected",
  16. [
  17. # behavior without keyword
  18. ({}, [True, False, True, False]),
  19. # set sharey=True should be identical
  20. ({"sharey": True}, [True, False, True, False]),
  21. # sharey=False, all yticklabels should be visible
  22. ({"sharey": False}, [True, True, True, True]),
  23. ],
  24. )
  25. def test_groupby_boxplot_sharey(self, kwargs, expected):
  26. # https://github.com/pandas-dev/pandas/issues/20968
  27. # sharey can now be switched check whether the right
  28. # pair of axes is turned on or off
  29. df = DataFrame(
  30. {
  31. "a": [-1.43, -0.15, -3.70, -1.43, -0.14],
  32. "b": [0.56, 0.84, 0.29, 0.56, 0.85],
  33. "c": [0, 1, 2, 3, 1],
  34. },
  35. index=[0, 1, 2, 3, 4],
  36. )
  37. axes = df.groupby("c").boxplot(**kwargs)
  38. self._assert_ytickslabels_visibility(axes, expected)
  39. @pytest.mark.parametrize(
  40. "kwargs, expected",
  41. [
  42. # behavior without keyword
  43. ({}, [True, True, True, True]),
  44. # set sharex=False should be identical
  45. ({"sharex": False}, [True, True, True, True]),
  46. # sharex=True, xticklabels should be visible
  47. # only for bottom plots
  48. ({"sharex": True}, [False, False, True, True]),
  49. ],
  50. )
  51. def test_groupby_boxplot_sharex(self, kwargs, expected):
  52. # https://github.com/pandas-dev/pandas/issues/20968
  53. # sharex can now be switched check whether the right
  54. # pair of axes is turned on or off
  55. df = DataFrame(
  56. {
  57. "a": [-1.43, -0.15, -3.70, -1.43, -0.14],
  58. "b": [0.56, 0.84, 0.29, 0.56, 0.85],
  59. "c": [0, 1, 2, 3, 1],
  60. },
  61. index=[0, 1, 2, 3, 4],
  62. )
  63. axes = df.groupby("c").boxplot(**kwargs)
  64. self._assert_xtickslabels_visibility(axes, expected)