@@ -1309,14 +1309,14 @@ def test_boxplot(self):
1309
1309
df ['indic' ] = ['foo' , 'bar' ] * 3
1310
1310
df ['indic2' ] = ['foo' , 'bar' , 'foo' ] * 2
1311
1311
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' )
1314
1314
_check_plot_works (df .boxplot , column = ['one' , 'two' ], by = 'indic' )
1315
1315
_check_plot_works (df .boxplot , column = 'one' , by = ['indic' , 'indic2' ])
1316
1316
_check_plot_works (df .boxplot , by = 'indic' )
1317
1317
_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' )
1320
1320
_check_plot_works (df .boxplot , by = 'indic' , notch = 1 )
1321
1321
1322
1322
df = DataFrame (np .random .rand (10 , 2 ), columns = ['Col1' , 'Col2' ])
@@ -1337,10 +1337,83 @@ def test_boxplot(self):
1337
1337
1338
1338
# When by is None, check that all relevant lines are present in the dict
1339
1339
fig , ax = self .plt .subplots ()
1340
- d = df .boxplot (ax = ax )
1340
+ d = df .boxplot (ax = ax , return_type = 'dict' )
1341
1341
lines = list (itertools .chain .from_iterable (d .values ()))
1342
1342
self .assertEqual (len (ax .get_lines ()), len (lines ))
1343
1343
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
+
1344
1417
@slow
1345
1418
def test_kde (self ):
1346
1419
_skip_if_no_scipy ()
@@ -2044,31 +2117,32 @@ class TestDataFrameGroupByPlots(TestPlotBase):
2044
2117
2045
2118
@slow
2046
2119
def test_boxplot (self ):
2047
- # unable to check layout because boxplot doesn't return ndarray
2048
- # axes_num can be checked using gcf().axes
2049
2120
grouped = self .hist_df .groupby (by = 'gender' )
2050
- box = _check_plot_works (grouped .boxplot )
2121
+ box = _check_plot_works (grouped .boxplot , return_type = 'dict' )
2051
2122
self ._check_axes_shape (self .plt .gcf ().axes , axes_num = 2 )
2052
2123
2053
- box = _check_plot_works (grouped .boxplot , subplots = False )
2124
+ box = _check_plot_works (grouped .boxplot , subplots = False ,
2125
+ return_type = 'dict' )
2054
2126
self ._check_axes_shape (self .plt .gcf ().axes , axes_num = 2 )
2055
2127
2056
2128
tuples = lzip (string .ascii_letters [:10 ], range (10 ))
2057
2129
df = DataFrame (np .random .rand (10 , 3 ),
2058
2130
index = MultiIndex .from_tuples (tuples ))
2059
2131
2060
2132
grouped = df .groupby (level = 1 )
2061
- box = _check_plot_works (grouped .boxplot )
2133
+ box = _check_plot_works (grouped .boxplot , return_type = 'dict' )
2062
2134
self ._check_axes_shape (self .plt .gcf ().axes , axes_num = 10 )
2063
2135
2064
- box = _check_plot_works (grouped .boxplot , subplots = False )
2136
+ box = _check_plot_works (grouped .boxplot , subplots = False ,
2137
+ return_type = 'dict' )
2065
2138
self ._check_axes_shape (self .plt .gcf ().axes , axes_num = 10 )
2066
2139
2067
2140
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' )
2069
2142
self ._check_axes_shape (self .plt .gcf ().axes , axes_num = 3 )
2070
2143
2071
- box = _check_plot_works (grouped .boxplot , subplots = False )
2144
+ box = _check_plot_works (grouped .boxplot , subplots = False ,
2145
+ return_type = 'dict' )
2072
2146
self ._check_axes_shape (self .plt .gcf ().axes , axes_num = 3 )
2073
2147
2074
2148
def test_series_plot_color_kwargs (self ):
@@ -2133,31 +2207,38 @@ def test_grouped_box_layout(self):
2133
2207
self .assertRaises (ValueError , df .boxplot , column = ['weight' , 'height' ],
2134
2208
by = df .gender , layout = (1 , 1 ))
2135
2209
self .assertRaises (ValueError , df .boxplot , column = ['height' , 'weight' , 'category' ],
2136
- layout = (2 , 1 ))
2210
+ layout = (2 , 1 ), return_type = 'dict' )
2137
2211
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' )
2139
2214
self ._check_axes_shape (self .plt .gcf ().axes , axes_num = 2 )
2140
2215
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' )
2142
2218
self ._check_axes_shape (self .plt .gcf ().axes , axes_num = 4 )
2143
2219
2144
2220
# 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' )
2146
2223
self ._check_axes_shape (self .plt .gcf ().axes , axes_num = 3 )
2147
2224
2148
2225
box = df .boxplot (column = ['height' , 'weight' , 'category' ], by = 'gender' )
2149
2226
self ._check_axes_shape (self .plt .gcf ().axes , axes_num = 3 )
2150
2227
2151
- box = df .groupby ('classroom' ).boxplot (column = ['height' , 'weight' , 'category' ])
2228
+ box = df .groupby ('classroom' ).boxplot (
2229
+ column = ['height' , 'weight' , 'category' ], return_type = 'dict' )
2152
2230
self ._check_axes_shape (self .plt .gcf ().axes , axes_num = 3 )
2153
2231
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' )
2155
2234
self ._check_axes_shape (self .plt .gcf ().axes , axes_num = 4 )
2156
2235
2157
2236
box = df .boxplot (column = ['height' , 'weight' , 'category' ], by = 'gender' , layout = (4 , 1 ))
2158
2237
self ._check_axes_shape (self .plt .gcf ().axes , axes_num = 3 )
2159
2238
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' )
2161
2242
self ._check_axes_shape (self .plt .gcf ().axes , axes_num = 3 )
2162
2243
2163
2244
@slow
0 commit comments