Skip to content

Commit 9737f27

Browse files
committed
BUG: catch attribute error for raw ndarrays to avoid duplicate plots
1 parent 20a8441 commit 9737f27

File tree

4 files changed

+59
-20
lines changed

4 files changed

+59
-20
lines changed

doc/source/groupby.rst

+28
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,34 @@ however pass ``sort=False`` for potential speedups:
188188
df2.groupby(['X'], sort=True).sum()
189189
df2.groupby(['X'], sort=False).sum()
190190
191+
.. _groupby.tabcompletion:
192+
193+
``GroupBy`` will tab complete column names (and other attributes)
194+
195+
.. ipython:: python
196+
:suppress:
197+
198+
n = 10
199+
weight = np.random.normal(166, 20, size=n)
200+
height = np.random.normal(60, 10, size=n)
201+
time = date_range('1/1/2000', periods=n)
202+
gender = tm.choice(['male', 'female'], size=n)
203+
df = DataFrame({'height': height, 'weight': weight,
204+
'gender': gender}, index=time)
205+
206+
.. ipython::
207+
208+
In [7]: df
209+
210+
In [8]: gb = df.groupby('gender')
211+
212+
@verbatim
213+
In [9]: gb.<TAB>
214+
gb.agg gb.boxplot gb.cummin gb.describe gb.filter gb.get_group gb.height gb.last gb.median gb.ngroups gb.plot gb.rank gb.std gb.transform
215+
gb.aggregate gb.count gb.cumprod gb.dtype gb.first gb.groups gb.hist gb.max gb.min gb.nth gb.prod gb.resample gb.sum gb.var
216+
gb.apply gb.cummax gb.cumsum gb.fillna gb.gender gb.head gb.indices gb.mean gb.name gb.ohlc gb.quantile gb.size gb.tail gb.weight
217+
218+
191219
.. _groupby.multiindex:
192220

193221
GroupBy with MultiIndex

doc/source/release.rst

+5
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,8 @@ See :ref:`Internal Refactoring<whatsnew_0130.refactoring>`
380380
function signature.
381381
- :func:`~pandas.read_html` now uses ``TextParser`` to parse HTML data from
382382
bs4/lxml (:issue:`4770`).
383+
- Removed the ``keep_internal`` keyword parameter in
384+
``pandas/core/groupby.py`` because it wasn't being used (:issue:`5102`).
383385

384386
.. _release.bug_fixes-0.13.0:
385387

@@ -555,6 +557,9 @@ Bug Fixes
555557
type of headers (:issue:`5048`).
556558
- Fixed a bug where ``DatetimeIndex`` joins with ``PeriodIndex`` caused a
557559
stack overflow (:issue:`3899`).
560+
- Fixed a bug where ``groupby`` objects didn't allow plots (:issue:`5102`).
561+
- Fixed a bug where ``groupby`` objects weren't tab-completing column names
562+
(:issue:`5102`).
558563

559564

560565
pandas 0.12.0

pandas/core/groupby.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,13 @@ def wrapper(*args, **kwargs):
289289

290290
def curried_with_axis(x):
291291
return f(x, *args, **kwargs_with_axis)
292-
curried_with_axis.__name__ = name
293292

294293
def curried(x):
295294
return f(x, *args, **kwargs)
296-
curried.__name__ = name
295+
296+
# preserve the name so we can detect it when calling plot methods,
297+
# to avoid duplicates
298+
curried.__name__ = curried_with_axis.__name__ = name
297299

298300
# special case otherwise extra plots are created when catching the
299301
# exception below
@@ -1957,14 +1959,14 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
19571959
index = key_index
19581960
else:
19591961
stacked_values = np.vstack([np.asarray(x)
1960-
for x in values]).T
1962+
for x in values]).T
19611963

19621964
index = values[0].index
19631965
columns = key_index
19641966

1965-
except ValueError:
1966-
#GH1738,, values is list of arrays of unequal lengths
1967-
# fall through to the outer else caluse
1967+
except (ValueError, AttributeError):
1968+
# GH1738: values is list of arrays of unequal lengths fall
1969+
# through to the outer else caluse
19681970
return Series(values, index=key_index)
19691971

19701972
return DataFrame(stacked_values, index=index,

pandas/tests/test_groupby.py

+18-14
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,29 @@
22
import nose
33
import unittest
44

5+
from numpy.testing.decorators import slow
6+
57
from datetime import datetime
68
from numpy import nan
79

810
from pandas import bdate_range
911
from pandas.core.index import Index, MultiIndex
1012
from pandas.core.common import rands
1113
from pandas.core.api import Categorical, DataFrame
12-
from pandas.core.groupby import (GroupByError, SpecificationError, DataError,
13-
_apply_whitelist)
14+
from pandas.core.groupby import SpecificationError, DataError
1415
from pandas.core.series import Series
1516
from pandas.util.testing import (assert_panel_equal, assert_frame_equal,
1617
assert_series_equal, assert_almost_equal,
1718
assert_index_equal)
1819
from pandas.compat import(
1920
range, long, lrange, StringIO, lmap, lzip, map, zip, builtins, OrderedDict
2021
)
21-
from pandas import compat, _np_version_under1p7
22+
from pandas import compat
2223
from pandas.core.panel import Panel
2324
from pandas.tools.merge import concat
2425
from collections import defaultdict
2526
import pandas.core.common as com
26-
import pandas.core.datetools as dt
2727
import numpy as np
28-
from numpy.testing import assert_equal
2928

3029
import pandas.core.nanops as nanops
3130

@@ -2730,7 +2729,8 @@ def test_groupby_whitelist(self):
27302729

27312730
def test_series_groupby_plotting_nominally_works(self):
27322731
try:
2733-
import matplotlib.pyplot as plt
2732+
import matplotlib as mpl
2733+
mpl.use('Agg')
27342734
except ImportError:
27352735
raise nose.SkipTest("matplotlib not installed")
27362736
n = 10
@@ -2743,9 +2743,12 @@ def test_series_groupby_plotting_nominally_works(self):
27432743
height.groupby(gender).hist()
27442744
tm.close()
27452745

2746+
@slow
27462747
def test_frame_groupby_plot_boxplot(self):
27472748
try:
27482749
import matplotlib.pyplot as plt
2750+
import matplotlib as mpl
2751+
mpl.use('Agg')
27492752
except ImportError:
27502753
raise nose.SkipTest("matplotlib not installed")
27512754
tm.close()
@@ -2767,12 +2770,15 @@ def test_frame_groupby_plot_boxplot(self):
27672770
self.assertEqual(len(res), 2)
27682771
tm.close()
27692772

2770-
with tm.assertRaises(TypeError, '.*str.+float'):
2773+
with tm.assertRaisesRegexp(TypeError, '.*str.+float'):
27712774
gb.hist()
27722775

2776+
@slow
27732777
def test_frame_groupby_hist(self):
27742778
try:
27752779
import matplotlib.pyplot as plt
2780+
import matplotlib as mpl
2781+
mpl.use('Agg')
27762782
except ImportError:
27772783
raise nose.SkipTest("matplotlib not installed")
27782784
tm.close()
@@ -2791,14 +2797,14 @@ def test_frame_groupby_hist(self):
27912797

27922798
def test_tab_completion(self):
27932799
grp = self.mframe.groupby(level='second')
2794-
results = set([v for v in grp.__dir__() if not v.startswith('_')])
2800+
results = set([v for v in dir(grp) if not v.startswith('_')])
27952801
expected = set(['A','B','C',
27962802
'agg','aggregate','apply','boxplot','filter','first','get_group',
27972803
'groups','hist','indices','last','max','mean','median',
27982804
'min','name','ngroups','nth','ohlc','plot', 'prod',
27992805
'size','std','sum','transform','var', 'count', 'head', 'describe',
2800-
'cummax', 'dtype', 'quantile', 'rank',
2801-
'cumprod', 'tail', 'resample', 'cummin', 'fillna', 'cumsum'])
2806+
'cummax', 'dtype', 'quantile', 'rank', 'cumprod', 'tail',
2807+
'resample', 'cummin', 'fillna', 'cumsum'])
28022808
self.assertEqual(results, expected)
28032809

28042810
def assert_fp_equal(a, b):
@@ -2836,7 +2842,5 @@ def testit(label_list, shape):
28362842

28372843

28382844
if __name__ == '__main__':
2839-
import nose
2840-
nose.runmodule(
2841-
argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure', '-s'],
2842-
exit=False)
2845+
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure',
2846+
'-s'], exit=False)

0 commit comments

Comments
 (0)