@@ -1791,18 +1791,20 @@ def aggfun(ser):
1791
1791
agged2 = df .groupby (keys ).aggregate (aggfun )
1792
1792
assert len (agged2 .columns ) + 1 == len (df .columns )
1793
1793
1794
- def test_groupby_level (self ):
1794
+ @pytest .mark .parametrize ('sort' , [True , False ])
1795
+ def test_groupby_level (self , sort ):
1796
+ # GH 17537
1795
1797
frame = self .mframe
1796
1798
deleveled = frame .reset_index ()
1797
1799
1798
- result0 = frame .groupby (level = 0 ).sum ()
1799
- result1 = frame .groupby (level = 1 ).sum ()
1800
+ result0 = frame .groupby (level = 0 , sort = sort ).sum ()
1801
+ result1 = frame .groupby (level = 1 , sort = sort ).sum ()
1800
1802
1801
- expected0 = frame .groupby (deleveled ['first' ].values ).sum ()
1802
- expected1 = frame .groupby (deleveled ['second' ].values ).sum ()
1803
+ expected0 = frame .groupby (deleveled ['first' ].values , sort = sort ).sum ()
1804
+ expected1 = frame .groupby (deleveled ['second' ].values , sort = sort ).sum ()
1803
1805
1804
- expected0 = expected0 . reindex ( frame . index .levels [ 0 ])
1805
- expected1 = expected1 . reindex ( frame . index .levels [ 1 ])
1806
+ expected0 . index .name = 'first'
1807
+ expected1 . index .name = 'second'
1806
1808
1807
1809
assert result0 .index .name == 'first'
1808
1810
assert result1 .index .name == 'second'
@@ -1813,15 +1815,15 @@ def test_groupby_level(self):
1813
1815
assert result1 .index .name == frame .index .names [1 ]
1814
1816
1815
1817
# groupby level name
1816
- result0 = frame .groupby (level = 'first' ).sum ()
1817
- result1 = frame .groupby (level = 'second' ).sum ()
1818
+ result0 = frame .groupby (level = 'first' , sort = sort ).sum ()
1819
+ result1 = frame .groupby (level = 'second' , sort = sort ).sum ()
1818
1820
assert_frame_equal (result0 , expected0 )
1819
1821
assert_frame_equal (result1 , expected1 )
1820
1822
1821
1823
# axis=1
1822
1824
1823
- result0 = frame .T .groupby (level = 0 , axis = 1 ).sum ()
1824
- result1 = frame .T .groupby (level = 1 , axis = 1 ).sum ()
1825
+ result0 = frame .T .groupby (level = 0 , axis = 1 , sort = sort ).sum ()
1826
+ result1 = frame .T .groupby (level = 1 , axis = 1 , sort = sort ).sum ()
1825
1827
assert_frame_equal (result0 , expected0 .T )
1826
1828
assert_frame_equal (result1 , expected1 .T )
1827
1829
@@ -1835,15 +1837,17 @@ def test_groupby_level_index_names(self):
1835
1837
df .groupby (level = 'exp' )
1836
1838
pytest .raises (ValueError , df .groupby , level = 'foo' )
1837
1839
1838
- def test_groupby_level_with_nas (self ):
1840
+ @pytest .mark .parametrize ('sort' , [True , False ])
1841
+ def test_groupby_level_with_nas (self , sort ):
1842
+ # GH 17537
1839
1843
index = MultiIndex (levels = [[1 , 0 ], [0 , 1 , 2 , 3 ]],
1840
1844
labels = [[1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 ], [0 , 1 , 2 , 3 , 0 , 1 ,
1841
1845
2 , 3 ]])
1842
1846
1843
1847
# factorizing doesn't confuse things
1844
1848
s = Series (np .arange (8. ), index = index )
1845
- result = s .groupby (level = 0 ).sum ()
1846
- expected = Series ([22 . , 6 . ], index = [1 , 0 ])
1849
+ result = s .groupby (level = 0 , sort = sort ).sum ()
1850
+ expected = Series ([6 . , 22 . ], index = [0 , 1 ])
1847
1851
assert_series_equal (result , expected )
1848
1852
1849
1853
index = MultiIndex (levels = [[1 , 0 ], [0 , 1 , 2 , 3 ]],
@@ -1852,8 +1856,8 @@ def test_groupby_level_with_nas(self):
1852
1856
1853
1857
# factorizing doesn't confuse things
1854
1858
s = Series (np .arange (8. ), index = index )
1855
- result = s .groupby (level = 0 ).sum ()
1856
- expected = Series ([18 . , 6 . ], index = [1 , 0 ])
1859
+ result = s .groupby (level = 0 , sort = sort ).sum ()
1860
+ expected = Series ([6 . , 18 . ], index = [0.0 , 1. 0 ])
1857
1861
assert_series_equal (result , expected )
1858
1862
1859
1863
def test_groupby_level_apply (self ):
@@ -1936,9 +1940,14 @@ def test_groupby_complex(self):
1936
1940
result = a .sum (level = 0 )
1937
1941
assert_series_equal (result , expected )
1938
1942
1939
- def test_level_preserve_order (self ):
1940
- grouped = self .mframe .groupby (level = 0 )
1941
- exp_labels = np .array ([0 , 0 , 0 , 1 , 1 , 2 , 2 , 3 , 3 , 3 ], np .intp )
1943
+ @pytest .mark .parametrize ('sort,labels' , [
1944
+ [True , [2 , 2 , 2 , 0 , 0 , 1 , 1 , 3 , 3 , 3 ]],
1945
+ [False , [0 , 0 , 0 , 1 , 1 , 2 , 2 , 3 , 3 , 3 ]]
1946
+ ])
1947
+ def test_level_preserve_order (self , sort , labels ):
1948
+ # GH 17537
1949
+ grouped = self .mframe .groupby (level = 0 , sort = sort )
1950
+ exp_labels = np .array (labels , np .intp )
1942
1951
assert_almost_equal (grouped .grouper .labels [0 ], exp_labels )
1943
1952
1944
1953
def test_grouping_labels (self ):
0 commit comments