Skip to content

BUG: boxplot returns incorrect dict #7225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion pandas/tests/test_graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from datetime import datetime, date

from pandas import Series, DataFrame, MultiIndex, PeriodIndex, date_range
from pandas.compat import range, lrange, StringIO, lmap, lzip, u, zip
from pandas.compat import (range, lrange, StringIO, lmap, lzip, u, zip,
iteritems, OrderedDict)
from pandas.util.decorators import cache_readonly
import pandas.core.common as com
import pandas.util.testing as tm
Expand Down Expand Up @@ -2245,6 +2246,48 @@ def test_grouped_hist(self):
with tm.assertRaises(AttributeError):
plotting.grouped_hist(df.A, by=df.C, foo='bar')

def _check_box_dict(self, returned, return_type,
expected_klass, expected_keys):
self.assertTrue(isinstance(returned, OrderedDict))
self.assertEqual(sorted(returned.keys()), sorted(expected_keys))
for key, value in iteritems(returned):
self.assertTrue(isinstance(value, expected_klass))
# check returned dict has correct mapping
if return_type == 'axes':
self.assertEqual(value.get_title(), key)
elif return_type == 'both':
self.assertEqual(value.ax.get_title(), key)
elif return_type == 'dict':
line = value['medians'][0]
self.assertEqual(line.get_axes().get_title(), key)
else:
raise AssertionError

@slow
def test_grouped_box_return_type(self):
import matplotlib.axes

df = self.hist_df

columns2 = 'X B C D A G Y N Q O'.split()
df2 = DataFrame(random.randn(50, 10), columns=columns2)
categories2 = 'A B C D E F G H I J'.split()
df2['category'] = tm.choice(categories2, size=50)

types = {'dict': dict, 'axes': matplotlib.axes.Axes, 'both': tuple}
for t, klass in iteritems(types):
returned = df.groupby('classroom').boxplot(return_type=t)
self._check_box_dict(returned, t, klass, ['A', 'B', 'C'])

returned = df.boxplot(by='classroom', return_type=t)
self._check_box_dict(returned, t, klass, ['height', 'weight', 'category'])

returned = df2.groupby('category').boxplot(return_type=t)
self._check_box_dict(returned, t, klass, categories2)

returned = df2.boxplot(by='category', return_type=t)
self._check_box_dict(returned, t, klass, columns2)

@slow
def test_grouped_box_layout(self):
df = self.hist_df
Expand Down
14 changes: 9 additions & 5 deletions pandas/tools/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -2363,12 +2363,17 @@ def plot_group(grouped, ax):
if return_type is None:
ret = axes
if return_type == 'axes':
ret = dict((k, ax) for k, ax in zip(d.keys(), axes))
ret = compat.OrderedDict()
axes = _flatten(axes)[:len(d)]
for k, ax in zip(d.keys(), axes):
ret[k] = ax
elif return_type == 'dict':
ret = d
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be compat.OrderedDict(d) if we want to always return an OrderedDict.

elif return_type == 'both':
ret = dict((k, BP(ax=ax, lines=line)) for
(k, line), ax in zip(d.items(), axes))
ret = compat.OrderedDict()
axes = _flatten(axes)[:len(d)]
for (k, line), ax in zip(d.items(), axes):
ret[k] = BP(ax=ax, lines=line)
else:
if layout is not None:
raise ValueError("The 'layout' keyword is not supported when "
Expand Down Expand Up @@ -2723,7 +2728,7 @@ def boxplot_frame_groupby(grouped, subplots=True, column=None, fontsize=None,
sharex=False, sharey=True)
axes = _flatten(axes)

ret = {}
ret = compat.OrderedDict()
for (key, group), ax in zip(grouped, axes):
d = group.boxplot(ax=ax, column=column, fontsize=fontsize,
rot=rot, grid=grid, **kwds)
Expand Down Expand Up @@ -2804,7 +2809,6 @@ def _grouped_plot_by_column(plotf, data, columns=None, by=None,
ravel_axes = _flatten(axes)

out_dict = compat.OrderedDict()

for i, col in enumerate(columns):
ax = ravel_axes[i]
gp_col = grouped[col]
Expand Down