From 67a17c4dfd8ab895d70099383e52e66efefd4768 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Wed, 23 Dec 2015 21:38:18 +0100 Subject: [PATCH] Fix passing args in groupby plot (GH11805) --- doc/source/whatsnew/v0.18.0.txt | 3 ++- pandas/core/groupby.py | 2 +- pandas/tests/test_graphics.py | 17 ++++++++++++++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v0.18.0.txt b/doc/source/whatsnew/v0.18.0.txt index 4614ce9acf3d5..89ffd0b015846 100644 --- a/doc/source/whatsnew/v0.18.0.txt +++ b/doc/source/whatsnew/v0.18.0.txt @@ -330,8 +330,9 @@ Bug Fixes - Bug in ``to_numeric`` where it does not raise if input is more than one dimension (:issue:`11776`) - Bug in parsing timezone offset strings with non-zero minutes (:issue:`11708`) - - Bug in ``df.plot`` using incorrect colors for bar plots under matplotlib 1.5+ (:issue:`11614`) +- Bug in the ``groupby`` ``plot`` method when using keyword arguments (:issue:`11805`). + - Bug in ``.loc`` result with duplicated key may have ``Index`` with incorrect dtype (:issue:`11497`) - Bug in ``pd.rolling_median`` where memory allocation failed even with sufficient memory (:issue:`11696`) diff --git a/pandas/core/groupby.py b/pandas/core/groupby.py index 5428ee5484bfa..31ed2c38cd29f 100644 --- a/pandas/core/groupby.py +++ b/pandas/core/groupby.py @@ -287,7 +287,7 @@ def __init__(self, groupby): self._groupby = groupby def __call__(self, *args, **kwargs): - def f(self, *args, **kwargs): + def f(self): return self.plot(*args, **kwargs) f.__name__ = 'plot' return self._groupby.apply(f) diff --git a/pandas/tests/test_graphics.py b/pandas/tests/test_graphics.py index 27ca4c157f594..ff69068a3495c 100644 --- a/pandas/tests/test_graphics.py +++ b/pandas/tests/test_graphics.py @@ -3718,7 +3718,7 @@ def test_plain_axes(self): def test_passed_bar_colors(self): import matplotlib as mpl - color_tuples = [(0.9, 0, 0, 1), (0, 0.9, 0, 1), (0, 0, 0.9, 1)] + color_tuples = [(0.9, 0, 0, 1), (0, 0.9, 0, 1), (0, 0, 0.9, 1)] colormap = mpl.colors.ListedColormap(color_tuples) barplot = pd.DataFrame([[1,2,3]]).plot(kind="bar", cmap=colormap) self.assertEqual(color_tuples, [c.get_facecolor() for c in barplot.patches]) @@ -3781,6 +3781,21 @@ def test_plot_submethod_works(self): df.groupby('z')['x'].plot.line() tm.close() + def test_plot_kwargs(self): + + df = DataFrame({'x': [1, 2, 3, 4, 5], + 'y': [1, 2, 3, 2, 1], + 'z': list('ababa')}) + + res = df.groupby('z').plot(kind='scatter', x='x', y='y') + # check that a scatter plot is effectively plotted: the axes should + # contain a PathCollection from the scatter plot (GH11805) + self.assertEqual(len(res['a'].collections), 1) + + res = df.groupby('z').plot.scatter(x='x', y='y') + self.assertEqual(len(res['a'].collections), 1) + + def assert_is_valid_plot_return_object(objs): import matplotlib.pyplot as plt if isinstance(objs, np.ndarray):