@@ -356,6 +356,54 @@ def _check_has_errorbars(self, axes, xerr=0, yerr=0):
356356 self .assertEqual (xerr , xerr_count )
357357 self .assertEqual (yerr , yerr_count )
358358
359+ def _check_box_return_type (self , returned , return_type , expected_keys = None ):
360+ """
361+ Check box returned type is correct
362+
363+ Parameters
364+ ----------
365+ returned : object to be tested, returned from boxplot
366+ return_type : str
367+ return_type passed to boxplot
368+ expected_keys : list-like, optional
369+ group labels in subplot case. If not passed,
370+ the function checks assuming boxplot uses single ax
371+ """
372+ from matplotlib .axes import Axes
373+ types = {'dict' : dict , 'axes' : Axes , 'both' : tuple }
374+ if expected_keys is None :
375+ # should be fixed when the returning default is changed
376+ if return_type is None :
377+ return_type = 'dict'
378+
379+ self .assertTrue (isinstance (returned , types [return_type ]))
380+ if return_type == 'both' :
381+ self .assertIsInstance (returned .ax , Axes )
382+ self .assertIsInstance (returned .lines , dict )
383+ else :
384+ # should be fixed when the returning default is changed
385+ if return_type is None :
386+ for r in self ._flatten_visible (returned ):
387+ self .assertIsInstance (r , Axes )
388+ return
389+
390+ self .assertTrue (isinstance (returned , OrderedDict ))
391+ self .assertEqual (sorted (returned .keys ()), sorted (expected_keys ))
392+ for key , value in iteritems (returned ):
393+ self .assertTrue (isinstance (value , types [return_type ]))
394+ # check returned dict has correct mapping
395+ if return_type == 'axes' :
396+ self .assertEqual (value .get_title (), key )
397+ elif return_type == 'both' :
398+ self .assertEqual (value .ax .get_title (), key )
399+ self .assertIsInstance (value .ax , Axes )
400+ self .assertIsInstance (value .lines , dict )
401+ elif return_type == 'dict' :
402+ line = value ['medians' ][0 ]
403+ self .assertEqual (line .get_axes ().get_title (), key )
404+ else :
405+ raise AssertionError
406+
359407
360408@tm .mplskip
361409class TestSeriesPlots (TestPlotBase ):
@@ -1421,65 +1469,20 @@ def test_boxplot_return_type(self):
14211469
14221470 with tm .assert_produces_warning (FutureWarning ):
14231471 result = df .boxplot ()
1424- self .assertIsInstance (result , dict ) # change to Axes in future
1472+ # change to Axes in future
1473+ self ._check_box_return_type (result , 'dict' )
14251474
14261475 with tm .assert_produces_warning (False ):
14271476 result = df .boxplot (return_type = 'dict' )
1428- self .assertIsInstance (result , dict )
1477+ self ._check_box_return_type (result , ' dict' )
14291478
14301479 with tm .assert_produces_warning (False ):
14311480 result = df .boxplot (return_type = 'axes' )
1432- self .assertIsInstance (result , mpl . axes . Axes )
1481+ self ._check_box_return_type (result , ' axes' )
14331482
14341483 with tm .assert_produces_warning (False ):
14351484 result = df .boxplot (return_type = 'both' )
1436- self .assertIsInstance (result , tuple )
1437-
1438- @slow
1439- def test_boxplot_return_type_by (self ):
1440- import matplotlib as mpl
1441-
1442- df = DataFrame (np .random .randn (10 , 2 ))
1443- df ['g' ] = ['a' ] * 5 + ['b' ] * 5
1444-
1445- # old style: return_type=None
1446- result = df .boxplot (by = 'g' )
1447- self .assertIsInstance (result , np .ndarray )
1448- self .assertIsInstance (result [0 ], mpl .axes .Axes )
1449-
1450- result = df .boxplot (by = 'g' , return_type = 'dict' )
1451- self .assertIsInstance (result , dict )
1452- self .assertIsInstance (result [0 ], dict )
1453-
1454- result = df .boxplot (by = 'g' , return_type = 'axes' )
1455- self .assertIsInstance (result , dict )
1456- self .assertIsInstance (result [0 ], mpl .axes .Axes )
1457-
1458- result = df .boxplot (by = 'g' , return_type = 'both' )
1459- self .assertIsInstance (result , dict )
1460- self .assertIsInstance (result [0 ], tuple )
1461- self .assertIsInstance (result [0 ][0 ], mpl .axes .Axes )
1462- self .assertIsInstance (result [0 ][1 ], dict )
1463-
1464- # now for groupby
1465- with tm .assert_produces_warning (FutureWarning ):
1466- result = df .groupby ('g' ).boxplot ()
1467- self .assertIsInstance (result , dict )
1468- self .assertIsInstance (result ['a' ], dict )
1469-
1470- result = df .groupby ('g' ).boxplot (return_type = 'dict' )
1471- self .assertIsInstance (result , dict )
1472- self .assertIsInstance (result ['a' ], dict )
1473-
1474- result = df .groupby ('g' ).boxplot (return_type = 'axes' )
1475- self .assertIsInstance (result , dict )
1476- self .assertIsInstance (result ['a' ], mpl .axes .Axes )
1477-
1478- result = df .groupby ('g' ).boxplot (return_type = 'both' )
1479- self .assertIsInstance (result , dict )
1480- self .assertIsInstance (result ['a' ], tuple )
1481- self .assertIsInstance (result ['a' ][0 ], mpl .axes .Axes )
1482- self .assertIsInstance (result ['a' ][1 ], dict )
1485+ self ._check_box_return_type (result , 'both' )
14831486
14841487 @slow
14851488 def test_kde (self ):
@@ -2278,47 +2281,39 @@ def test_grouped_hist(self):
22782281 with tm .assertRaises (AttributeError ):
22792282 plotting .grouped_hist (df .A , by = df .C , foo = 'bar' )
22802283
2281- def _check_box_dict (self , returned , return_type ,
2282- expected_klass , expected_keys ):
2283- self .assertTrue (isinstance (returned , OrderedDict ))
2284- self .assertEqual (sorted (returned .keys ()), sorted (expected_keys ))
2285- for key , value in iteritems (returned ):
2286- self .assertTrue (isinstance (value , expected_klass ))
2287- # check returned dict has correct mapping
2288- if return_type == 'axes' :
2289- self .assertEqual (value .get_title (), key )
2290- elif return_type == 'both' :
2291- self .assertEqual (value .ax .get_title (), key )
2292- elif return_type == 'dict' :
2293- line = value ['medians' ][0 ]
2294- self .assertEqual (line .get_axes ().get_title (), key )
2295- else :
2296- raise AssertionError
2297-
22982284 @slow
22992285 def test_grouped_box_return_type (self ):
2300- import matplotlib .axes
2301-
23022286 df = self .hist_df
23032287
2288+ # old style: return_type=None
2289+ result = df .boxplot (by = 'gender' )
2290+ self .assertIsInstance (result , np .ndarray )
2291+ self ._check_box_return_type (result , None ,
2292+ expected_keys = ['height' , 'weight' , 'category' ])
2293+
2294+ # now for groupby
2295+ with tm .assert_produces_warning (FutureWarning ):
2296+ result = df .groupby ('gender' ).boxplot ()
2297+ self ._check_box_return_type (result , 'dict' , expected_keys = ['Male' , 'Female' ])
2298+
23042299 columns2 = 'X B C D A G Y N Q O' .split ()
23052300 df2 = DataFrame (random .randn (50 , 10 ), columns = columns2 )
23062301 categories2 = 'A B C D E F G H I J' .split ()
23072302 df2 ['category' ] = categories2 * 5
23082303
2309- types = {'dict' : dict , 'axes' : matplotlib .axes .Axes , 'both' : tuple }
2310- for t , klass in iteritems (types ):
2304+ for t in ['dict' , 'axes' , 'both' ]:
23112305 returned = df .groupby ('classroom' ).boxplot (return_type = t )
2312- self ._check_box_dict (returned , t , klass , ['A' , 'B' , 'C' ])
2306+ self ._check_box_return_type (returned , t , expected_keys = ['A' , 'B' , 'C' ])
23132307
23142308 returned = df .boxplot (by = 'classroom' , return_type = t )
2315- self ._check_box_dict (returned , t , klass , ['height' , 'weight' , 'category' ])
2309+ self ._check_box_return_type (returned , t ,
2310+ expected_keys = ['height' , 'weight' , 'category' ])
23162311
23172312 returned = df2 .groupby ('category' ).boxplot (return_type = t )
2318- self ._check_box_dict (returned , t , klass , categories2 )
2313+ self ._check_box_return_type (returned , t , expected_keys = categories2 )
23192314
23202315 returned = df2 .boxplot (by = 'category' , return_type = t )
2321- self ._check_box_dict (returned , t , klass , columns2 )
2316+ self ._check_box_return_type (returned , t , expected_keys = columns2 )
23222317
23232318 @slow
23242319 def test_grouped_box_layout (self ):
0 commit comments