1
1
# -*- coding: utf-8 -*-
2
2
from __future__ import print_function
3
3
4
+ from warnings import catch_warnings
4
5
from string import ascii_lowercase
5
6
from datetime import datetime
6
7
from numpy import nan
@@ -814,12 +815,14 @@ def f(grp):
814
815
assert_series_equal (result , e )
815
816
816
817
def test_get_group (self ):
817
- wp = tm .makePanel ()
818
- grouped = wp .groupby (lambda x : x .month , axis = 'major' )
818
+ with catch_warnings (record = True ):
819
+ wp = tm .makePanel ()
820
+ grouped = wp .groupby (lambda x : x .month , axis = 'major' )
819
821
820
- gp = grouped .get_group (1 )
821
- expected = wp .reindex (major = [x for x in wp .major_axis if x .month == 1 ])
822
- assert_panel_equal (gp , expected )
822
+ gp = grouped .get_group (1 )
823
+ expected = wp .reindex (
824
+ major = [x for x in wp .major_axis if x .month == 1 ])
825
+ assert_panel_equal (gp , expected )
823
826
824
827
# GH 5267
825
828
# be datelike friendly
@@ -1317,16 +1320,17 @@ def test_multi_iter_frame(self):
1317
1320
pass
1318
1321
1319
1322
def test_multi_iter_panel (self ):
1320
- wp = tm .makePanel ()
1321
- grouped = wp .groupby ([lambda x : x .month , lambda x : x .weekday ()],
1322
- axis = 1 )
1323
-
1324
- for (month , wd ), group in grouped :
1325
- exp_axis = [x
1326
- for x in wp .major_axis
1327
- if x .month == month and x .weekday () == wd ]
1328
- expected = wp .reindex (major = exp_axis )
1329
- assert_panel_equal (group , expected )
1323
+ with catch_warnings (record = True ):
1324
+ wp = tm .makePanel ()
1325
+ grouped = wp .groupby ([lambda x : x .month , lambda x : x .weekday ()],
1326
+ axis = 1 )
1327
+
1328
+ for (month , wd ), group in grouped :
1329
+ exp_axis = [x
1330
+ for x in wp .major_axis
1331
+ if x .month == month and x .weekday () == wd ]
1332
+ expected = wp .reindex (major = exp_axis )
1333
+ assert_panel_equal (group , expected )
1330
1334
1331
1335
def test_multi_func (self ):
1332
1336
col1 = self .df ['A' ]
@@ -1387,25 +1391,26 @@ def test_groupby_multiple_columns(self):
1387
1391
1388
1392
def _check_op (op ):
1389
1393
1390
- result1 = op (grouped )
1391
-
1392
- expected = defaultdict (dict )
1393
- for n1 , gp1 in data .groupby ('A' ):
1394
- for n2 , gp2 in gp1 .groupby ('B' ):
1395
- expected [n1 ][n2 ] = op (gp2 .loc [:, ['C' , 'D' ]])
1396
- expected = dict ((k , DataFrame (v ))
1397
- for k , v in compat .iteritems (expected ))
1398
- expected = Panel .fromDict (expected ).swapaxes (0 , 1 )
1399
- expected .major_axis .name , expected .minor_axis .name = 'A' , 'B'
1400
-
1401
- # a little bit crude
1402
- for col in ['C' , 'D' ]:
1403
- result_col = op (grouped [col ])
1404
- exp = expected [col ]
1405
- pivoted = result1 [col ].unstack ()
1406
- pivoted2 = result_col .unstack ()
1407
- assert_frame_equal (pivoted .reindex_like (exp ), exp )
1408
- assert_frame_equal (pivoted2 .reindex_like (exp ), exp )
1394
+ with catch_warnings (record = True ):
1395
+ result1 = op (grouped )
1396
+
1397
+ expected = defaultdict (dict )
1398
+ for n1 , gp1 in data .groupby ('A' ):
1399
+ for n2 , gp2 in gp1 .groupby ('B' ):
1400
+ expected [n1 ][n2 ] = op (gp2 .loc [:, ['C' , 'D' ]])
1401
+ expected = dict ((k , DataFrame (v ))
1402
+ for k , v in compat .iteritems (expected ))
1403
+ expected = Panel .fromDict (expected ).swapaxes (0 , 1 )
1404
+ expected .major_axis .name , expected .minor_axis .name = 'A' , 'B'
1405
+
1406
+ # a little bit crude
1407
+ for col in ['C' , 'D' ]:
1408
+ result_col = op (grouped [col ])
1409
+ exp = expected [col ]
1410
+ pivoted = result1 [col ].unstack ()
1411
+ pivoted2 = result_col .unstack ()
1412
+ assert_frame_equal (pivoted .reindex_like (exp ), exp )
1413
+ assert_frame_equal (pivoted2 .reindex_like (exp ), exp )
1409
1414
1410
1415
_check_op (lambda x : x .sum ())
1411
1416
_check_op (lambda x : x .mean ())
@@ -2980,8 +2985,9 @@ def test_dictify(self):
2980
2985
2981
2986
def test_sparse_friendly (self ):
2982
2987
sdf = self .df [['C' , 'D' ]].to_sparse ()
2983
- panel = tm .makePanel ()
2984
- tm .add_nans (panel )
2988
+ with catch_warnings (record = True ):
2989
+ panel = tm .makePanel ()
2990
+ tm .add_nans (panel )
2985
2991
2986
2992
def _check_work (gp ):
2987
2993
gp .mean ()
@@ -2997,27 +3003,28 @@ def _check_work(gp):
2997
3003
# _check_work(panel.groupby(lambda x: x.month, axis=1))
2998
3004
2999
3005
def test_panel_groupby (self ):
3000
- self .panel = tm .makePanel ()
3001
- tm .add_nans (self .panel )
3002
- grouped = self .panel .groupby ({'ItemA' : 0 , 'ItemB' : 0 , 'ItemC' : 1 },
3003
- axis = 'items' )
3004
- agged = grouped .mean ()
3005
- agged2 = grouped .agg (lambda x : x .mean ('items' ))
3006
+ with catch_warnings (record = True ):
3007
+ self .panel = tm .makePanel ()
3008
+ tm .add_nans (self .panel )
3009
+ grouped = self .panel .groupby ({'ItemA' : 0 , 'ItemB' : 0 , 'ItemC' : 1 },
3010
+ axis = 'items' )
3011
+ agged = grouped .mean ()
3012
+ agged2 = grouped .agg (lambda x : x .mean ('items' ))
3006
3013
3007
- tm .assert_panel_equal (agged , agged2 )
3014
+ tm .assert_panel_equal (agged , agged2 )
3008
3015
3009
- self .assert_index_equal (agged .items , Index ([0 , 1 ]))
3016
+ self .assert_index_equal (agged .items , Index ([0 , 1 ]))
3010
3017
3011
- grouped = self .panel .groupby (lambda x : x .month , axis = 'major' )
3012
- agged = grouped .mean ()
3018
+ grouped = self .panel .groupby (lambda x : x .month , axis = 'major' )
3019
+ agged = grouped .mean ()
3013
3020
3014
- exp = Index (sorted (list (set (self .panel .major_axis .month ))))
3015
- self .assert_index_equal (agged .major_axis , exp )
3021
+ exp = Index (sorted (list (set (self .panel .major_axis .month ))))
3022
+ self .assert_index_equal (agged .major_axis , exp )
3016
3023
3017
- grouped = self .panel .groupby ({'A' : 0 , 'B' : 0 , 'C' : 1 , 'D' : 1 },
3018
- axis = 'minor' )
3019
- agged = grouped .mean ()
3020
- self .assert_index_equal (agged .minor_axis , Index ([0 , 1 ]))
3024
+ grouped = self .panel .groupby ({'A' : 0 , 'B' : 0 , 'C' : 1 , 'D' : 1 },
3025
+ axis = 'minor' )
3026
+ agged = grouped .mean ()
3027
+ self .assert_index_equal (agged .minor_axis , Index ([0 , 1 ]))
3021
3028
3022
3029
def test_groupby_2d_malformed (self ):
3023
3030
d = DataFrame (index = lrange (2 ))
0 commit comments