|
10 | 10 | from datetime import datetime, date
|
11 | 11 |
|
12 | 12 | from pandas import Series, DataFrame, MultiIndex, PeriodIndex, date_range
|
13 |
| -from pandas.compat import range, lrange, StringIO, lmap, lzip, u, zip |
| 13 | +from pandas.compat import (range, lrange, StringIO, lmap, lzip, u, zip, |
| 14 | + iteritems, OrderedDict) |
14 | 15 | from pandas.util.decorators import cache_readonly
|
15 | 16 | import pandas.core.common as com
|
16 | 17 | import pandas.util.testing as tm
|
@@ -2245,6 +2246,48 @@ def test_grouped_hist(self):
|
2245 | 2246 | with tm.assertRaises(AttributeError):
|
2246 | 2247 | plotting.grouped_hist(df.A, by=df.C, foo='bar')
|
2247 | 2248 |
|
| 2249 | + def _check_box_dict(self, returned, return_type, |
| 2250 | + expected_klass, expected_keys): |
| 2251 | + self.assertTrue(isinstance(returned, OrderedDict)) |
| 2252 | + self.assertEqual(sorted(returned.keys()), sorted(expected_keys)) |
| 2253 | + for key, value in iteritems(returned): |
| 2254 | + self.assertTrue(isinstance(value, expected_klass)) |
| 2255 | + # check returned dict has correct mapping |
| 2256 | + if return_type == 'axes': |
| 2257 | + self.assertEqual(value.get_title(), key) |
| 2258 | + elif return_type == 'both': |
| 2259 | + self.assertEqual(value.ax.get_title(), key) |
| 2260 | + elif return_type == 'dict': |
| 2261 | + line = value['medians'][0] |
| 2262 | + self.assertEqual(line.get_axes().get_title(), key) |
| 2263 | + else: |
| 2264 | + raise AssertionError |
| 2265 | + |
| 2266 | + @slow |
| 2267 | + def test_grouped_box_return_type(self): |
| 2268 | + import matplotlib.axes |
| 2269 | + |
| 2270 | + df = self.hist_df |
| 2271 | + |
| 2272 | + columns2 = 'X B C D A G Y N Q O'.split() |
| 2273 | + df2 = DataFrame(random.randn(50, 10), columns=columns2) |
| 2274 | + categories2 = 'A B C D E F G H I J'.split() |
| 2275 | + df2['category'] = tm.choice(categories2, size=50) |
| 2276 | + |
| 2277 | + types = {'dict': dict, 'axes': matplotlib.axes.Axes, 'both': tuple} |
| 2278 | + for t, klass in iteritems(types): |
| 2279 | + returned = df.groupby('classroom').boxplot(return_type=t) |
| 2280 | + self._check_box_dict(returned, t, klass, ['A', 'B', 'C']) |
| 2281 | + |
| 2282 | + returned = df.boxplot(by='classroom', return_type=t) |
| 2283 | + self._check_box_dict(returned, t, klass, ['height', 'weight', 'category']) |
| 2284 | + |
| 2285 | + returned = df2.groupby('category').boxplot(return_type=t) |
| 2286 | + self._check_box_dict(returned, t, klass, categories2) |
| 2287 | + |
| 2288 | + returned = df2.boxplot(by='category', return_type=t) |
| 2289 | + self._check_box_dict(returned, t, klass, columns2) |
| 2290 | + |
2248 | 2291 | @slow
|
2249 | 2292 | def test_grouped_box_layout(self):
|
2250 | 2293 | df = self.hist_df
|
|
0 commit comments