@@ -1309,14 +1309,14 @@ def test_boxplot(self):
13091309 df ['indic' ] = ['foo' , 'bar' ] * 3
13101310 df ['indic2' ] = ['foo' , 'bar' , 'foo' ] * 2
13111311
1312- _check_plot_works (df .boxplot )
1313- _check_plot_works (df .boxplot , column = ['one' , 'two' ])
1312+ _check_plot_works (df .boxplot , return_type = 'dict' )
1313+ _check_plot_works (df .boxplot , column = ['one' , 'two' ], return_type = 'dict' )
13141314 _check_plot_works (df .boxplot , column = ['one' , 'two' ], by = 'indic' )
13151315 _check_plot_works (df .boxplot , column = 'one' , by = ['indic' , 'indic2' ])
13161316 _check_plot_works (df .boxplot , by = 'indic' )
13171317 _check_plot_works (df .boxplot , by = ['indic' , 'indic2' ])
1318- _check_plot_works (plotting .boxplot , df ['one' ])
1319- _check_plot_works (df .boxplot , notch = 1 )
1318+ _check_plot_works (plotting .boxplot , df ['one' ], return_type = 'dict' )
1319+ _check_plot_works (df .boxplot , notch = 1 , return_type = 'dict' )
13201320 _check_plot_works (df .boxplot , by = 'indic' , notch = 1 )
13211321
13221322 df = DataFrame (np .random .rand (10 , 2 ), columns = ['Col1' , 'Col2' ])
@@ -1337,10 +1337,83 @@ def test_boxplot(self):
13371337
13381338 # When by is None, check that all relevant lines are present in the dict
13391339 fig , ax = self .plt .subplots ()
1340- d = df .boxplot (ax = ax )
1340+ d = df .boxplot (ax = ax , return_type = 'dict' )
13411341 lines = list (itertools .chain .from_iterable (d .values ()))
13421342 self .assertEqual (len (ax .get_lines ()), len (lines ))
13431343
1344+ @slow
1345+ def test_boxplot_return_type (self ):
1346+ # API change in https://github.com/pydata/pandas/pull/7096
1347+ import matplotlib as mpl
1348+
1349+ df = DataFrame (randn (6 , 4 ),
1350+ index = list (string .ascii_letters [:6 ]),
1351+ columns = ['one' , 'two' , 'three' , 'four' ])
1352+ with tm .assertRaises (ValueError ):
1353+ df .boxplot (return_type = 'NOTATYPE' )
1354+
1355+ with tm .assert_produces_warning (FutureWarning ):
1356+ result = df .boxplot ()
1357+ self .assertIsInstance (result , dict ) # change to Axes in future
1358+
1359+ with tm .assert_produces_warning (False ):
1360+ result = df .boxplot (return_type = 'dict' )
1361+ self .assertIsInstance (result , dict )
1362+
1363+ with tm .assert_produces_warning (False ):
1364+ result = df .boxplot (return_type = 'axes' )
1365+ self .assertIsInstance (result , mpl .axes .Axes )
1366+
1367+ with tm .assert_produces_warning (False ):
1368+ result = df .boxplot (return_type = 'both' )
1369+ self .assertIsInstance (result , tuple )
1370+
1371+ @slow
1372+ def test_boxplot_return_type_by (self ):
1373+ import matplotlib as mpl
1374+
1375+ df = DataFrame (np .random .randn (10 , 2 ))
1376+ df ['g' ] = ['a' ] * 5 + ['b' ] * 5
1377+
1378+ # old style: return_type=None
1379+ result = df .boxplot (by = 'g' )
1380+ self .assertIsInstance (result , np .ndarray )
1381+ self .assertIsInstance (result [0 ], mpl .axes .Axes )
1382+
1383+ result = df .boxplot (by = 'g' , return_type = 'dict' )
1384+ self .assertIsInstance (result , dict )
1385+ self .assertIsInstance (result [0 ], dict )
1386+
1387+ result = df .boxplot (by = 'g' , return_type = 'axes' )
1388+ self .assertIsInstance (result , dict )
1389+ self .assertIsInstance (result [0 ], mpl .axes .Axes )
1390+
1391+ result = df .boxplot (by = 'g' , return_type = 'both' )
1392+ self .assertIsInstance (result , dict )
1393+ self .assertIsInstance (result [0 ], tuple )
1394+ self .assertIsInstance (result [0 ][0 ], mpl .axes .Axes )
1395+ self .assertIsInstance (result [0 ][1 ], dict )
1396+
1397+ # now for groupby
1398+ with tm .assert_produces_warning (FutureWarning ):
1399+ result = df .groupby ('g' ).boxplot ()
1400+ self .assertIsInstance (result , dict )
1401+ self .assertIsInstance (result ['a' ], dict )
1402+
1403+ result = df .groupby ('g' ).boxplot (return_type = 'dict' )
1404+ self .assertIsInstance (result , dict )
1405+ self .assertIsInstance (result ['a' ], dict )
1406+
1407+ result = df .groupby ('g' ).boxplot (return_type = 'axes' )
1408+ self .assertIsInstance (result , dict )
1409+ self .assertIsInstance (result ['a' ], mpl .axes .Axes )
1410+
1411+ result = df .groupby ('g' ).boxplot (return_type = 'both' )
1412+ self .assertIsInstance (result , dict )
1413+ self .assertIsInstance (result ['a' ], tuple )
1414+ self .assertIsInstance (result ['a' ][0 ], mpl .axes .Axes )
1415+ self .assertIsInstance (result ['a' ][1 ], dict )
1416+
13441417 @slow
13451418 def test_kde (self ):
13461419 _skip_if_no_scipy ()
@@ -2044,31 +2117,32 @@ class TestDataFrameGroupByPlots(TestPlotBase):
20442117
20452118 @slow
20462119 def test_boxplot (self ):
2047- # unable to check layout because boxplot doesn't return ndarray
2048- # axes_num can be checked using gcf().axes
20492120 grouped = self .hist_df .groupby (by = 'gender' )
2050- box = _check_plot_works (grouped .boxplot )
2121+ box = _check_plot_works (grouped .boxplot , return_type = 'dict' )
20512122 self ._check_axes_shape (self .plt .gcf ().axes , axes_num = 2 )
20522123
2053- box = _check_plot_works (grouped .boxplot , subplots = False )
2124+ box = _check_plot_works (grouped .boxplot , subplots = False ,
2125+ return_type = 'dict' )
20542126 self ._check_axes_shape (self .plt .gcf ().axes , axes_num = 2 )
20552127
20562128 tuples = lzip (string .ascii_letters [:10 ], range (10 ))
20572129 df = DataFrame (np .random .rand (10 , 3 ),
20582130 index = MultiIndex .from_tuples (tuples ))
20592131
20602132 grouped = df .groupby (level = 1 )
2061- box = _check_plot_works (grouped .boxplot )
2133+ box = _check_plot_works (grouped .boxplot , return_type = 'dict' )
20622134 self ._check_axes_shape (self .plt .gcf ().axes , axes_num = 10 )
20632135
2064- box = _check_plot_works (grouped .boxplot , subplots = False )
2136+ box = _check_plot_works (grouped .boxplot , subplots = False ,
2137+ return_type = 'dict' )
20652138 self ._check_axes_shape (self .plt .gcf ().axes , axes_num = 10 )
20662139
20672140 grouped = df .unstack (level = 1 ).groupby (level = 0 , axis = 1 )
2068- box = _check_plot_works (grouped .boxplot )
2141+ box = _check_plot_works (grouped .boxplot , return_type = 'dict' )
20692142 self ._check_axes_shape (self .plt .gcf ().axes , axes_num = 3 )
20702143
2071- box = _check_plot_works (grouped .boxplot , subplots = False )
2144+ box = _check_plot_works (grouped .boxplot , subplots = False ,
2145+ return_type = 'dict' )
20722146 self ._check_axes_shape (self .plt .gcf ().axes , axes_num = 3 )
20732147
20742148 def test_series_plot_color_kwargs (self ):
@@ -2133,31 +2207,38 @@ def test_grouped_box_layout(self):
21332207 self .assertRaises (ValueError , df .boxplot , column = ['weight' , 'height' ],
21342208 by = df .gender , layout = (1 , 1 ))
21352209 self .assertRaises (ValueError , df .boxplot , column = ['height' , 'weight' , 'category' ],
2136- layout = (2 , 1 ))
2210+ layout = (2 , 1 ), return_type = 'dict' )
21372211
2138- box = _check_plot_works (df .groupby ('gender' ).boxplot , column = 'height' )
2212+ box = _check_plot_works (df .groupby ('gender' ).boxplot , column = 'height' ,
2213+ return_type = 'dict' )
21392214 self ._check_axes_shape (self .plt .gcf ().axes , axes_num = 2 )
21402215
2141- box = _check_plot_works (df .groupby ('category' ).boxplot , column = 'height' )
2216+ box = _check_plot_works (df .groupby ('category' ).boxplot , column = 'height' ,
2217+ return_type = 'dict' )
21422218 self ._check_axes_shape (self .plt .gcf ().axes , axes_num = 4 )
21432219
21442220 # GH 6769
2145- box = _check_plot_works (df .groupby ('classroom' ).boxplot , column = 'height' )
2221+ box = _check_plot_works (df .groupby ('classroom' ).boxplot ,
2222+ column = 'height' , return_type = 'dict' )
21462223 self ._check_axes_shape (self .plt .gcf ().axes , axes_num = 3 )
21472224
21482225 box = df .boxplot (column = ['height' , 'weight' , 'category' ], by = 'gender' )
21492226 self ._check_axes_shape (self .plt .gcf ().axes , axes_num = 3 )
21502227
2151- box = df .groupby ('classroom' ).boxplot (column = ['height' , 'weight' , 'category' ])
2228+ box = df .groupby ('classroom' ).boxplot (
2229+ column = ['height' , 'weight' , 'category' ], return_type = 'dict' )
21522230 self ._check_axes_shape (self .plt .gcf ().axes , axes_num = 3 )
21532231
2154- box = _check_plot_works (df .groupby ('category' ).boxplot , column = 'height' , layout = (3 , 2 ))
2232+ box = _check_plot_works (df .groupby ('category' ).boxplot , column = 'height' ,
2233+ layout = (3 , 2 ), return_type = 'dict' )
21552234 self ._check_axes_shape (self .plt .gcf ().axes , axes_num = 4 )
21562235
21572236 box = df .boxplot (column = ['height' , 'weight' , 'category' ], by = 'gender' , layout = (4 , 1 ))
21582237 self ._check_axes_shape (self .plt .gcf ().axes , axes_num = 3 )
21592238
2160- box = df .groupby ('classroom' ).boxplot (column = ['height' , 'weight' , 'category' ], layout = (1 , 4 ))
2239+ box = df .groupby ('classroom' ).boxplot (
2240+ column = ['height' , 'weight' , 'category' ], layout = (1 , 4 ),
2241+ return_type = 'dict' )
21612242 self ._check_axes_shape (self .plt .gcf ().axes , axes_num = 3 )
21622243
21632244 @slow
0 commit comments