@@ -1770,13 +1770,9 @@ def test_empty_groupby(columns, keys, values, method, op, request):
1770
1770
isinstance (values , Categorical )
1771
1771
and not isinstance (columns , list )
1772
1772
and op in ["sum" , "prod" ]
1773
- and method != "apply"
1774
1773
):
1775
1774
# handled below GH#41291
1776
1775
pass
1777
- elif isinstance (values , Categorical ) and len (keys ) == 1 and method == "apply" :
1778
- mark = pytest .mark .xfail (raises = TypeError , match = "'str' object is not callable" )
1779
- request .node .add_marker (mark )
1780
1776
elif (
1781
1777
isinstance (values , Categorical )
1782
1778
and len (keys ) == 1
@@ -1808,21 +1804,16 @@ def test_empty_groupby(columns, keys, values, method, op, request):
1808
1804
isinstance (values , Categorical )
1809
1805
and len (keys ) == 2
1810
1806
and op in ["min" , "max" , "sum" ]
1811
- and method != "apply"
1812
1807
):
1813
1808
mark = pytest .mark .xfail (
1814
1809
raises = AssertionError , match = "(DataFrame|Series) are different"
1815
1810
)
1816
1811
request .node .add_marker (mark )
1817
- elif (
1818
- isinstance (values , pd .core .arrays .BooleanArray )
1819
- and op in ["sum" , "prod" ]
1820
- and method != "apply"
1821
- ):
1812
+ elif isinstance (values , pd .core .arrays .BooleanArray ) and op in ["sum" , "prod" ]:
1822
1813
# We expect to get Int64 back for these
1823
1814
override_dtype = "Int64"
1824
1815
1825
- if isinstance (values [0 ], bool ) and op in ("prod" , "sum" ) and method != "apply" :
1816
+ if isinstance (values [0 ], bool ) and op in ("prod" , "sum" ):
1826
1817
# sum/product of bools is an integer
1827
1818
override_dtype = "int64"
1828
1819
@@ -1846,66 +1837,62 @@ def get_result():
1846
1837
# i.e. SeriesGroupBy
1847
1838
if op in ["prod" , "sum" ]:
1848
1839
# ops that require more than just ordered-ness
1849
- if method != "apply" :
1850
- # FIXME: apply goes through different code path
1851
- if df .dtypes [0 ].kind == "M" :
1852
- # GH#41291
1853
- # datetime64 -> prod and sum are invalid
1854
- msg = "datetime64 type does not support"
1855
- with pytest .raises (TypeError , match = msg ):
1856
- get_result ()
1857
-
1858
- return
1859
- elif isinstance (values , Categorical ):
1860
- # GH#41291
1861
- msg = "category type does not support"
1862
- with pytest .raises (TypeError , match = msg ):
1863
- get_result ()
1864
-
1865
- return
1840
+ if df .dtypes [0 ].kind == "M" :
1841
+ # GH#41291
1842
+ # datetime64 -> prod and sum are invalid
1843
+ msg = "datetime64 type does not support"
1844
+ with pytest .raises (TypeError , match = msg ):
1845
+ get_result ()
1846
+
1847
+ return
1848
+ elif isinstance (values , Categorical ):
1849
+ # GH#41291
1850
+ msg = "category type does not support"
1851
+ with pytest .raises (TypeError , match = msg ):
1852
+ get_result ()
1853
+
1854
+ return
1866
1855
else :
1867
1856
# ie. DataFrameGroupBy
1868
1857
if op in ["prod" , "sum" ]:
1869
1858
# ops that require more than just ordered-ness
1870
- if method != "apply" :
1871
- # FIXME: apply goes through different code path
1872
- if df .dtypes [0 ].kind == "M" :
1873
- # GH#41291
1874
- # datetime64 -> prod and sum are invalid
1875
- result = get_result ()
1876
-
1877
- # with numeric_only=True, these are dropped, and we get
1878
- # an empty DataFrame back
1879
- expected = df .set_index (keys )[[]]
1880
- tm .assert_equal (result , expected )
1881
- return
1882
-
1883
- elif isinstance (values , Categorical ):
1884
- # GH#41291
1885
- # Categorical doesn't implement sum or prod
1886
- result = get_result ()
1887
-
1888
- # with numeric_only=True, these are dropped, and we get
1889
- # an empty DataFrame back
1890
- expected = df .set_index (keys )[[]]
1891
- if len (keys ) != 1 and op == "prod" :
1892
- # TODO: why just prod and not sum?
1893
- # Categorical is special without 'observed=True'
1894
- lev = Categorical ([0 ], dtype = values .dtype )
1895
- mi = MultiIndex .from_product ([lev , lev ], names = ["A" , "B" ])
1896
- expected = DataFrame ([], columns = [], index = mi )
1897
-
1898
- tm .assert_equal (result , expected )
1899
- return
1900
-
1901
- elif df .dtypes [0 ] == object :
1902
- # FIXME: the test is actually wrong here, xref #41341
1903
- result = get_result ()
1904
- # In this case we have list-of-list, will raise TypeError,
1905
- # and subsequently be dropped as nuisance columns
1906
- expected = df .set_index (keys )[[]]
1907
- tm .assert_equal (result , expected )
1908
- return
1859
+ if df .dtypes [0 ].kind == "M" :
1860
+ # GH#41291
1861
+ # datetime64 -> prod and sum are invalid
1862
+ result = get_result ()
1863
+
1864
+ # with numeric_only=True, these are dropped, and we get
1865
+ # an empty DataFrame back
1866
+ expected = df .set_index (keys )[[]]
1867
+ tm .assert_equal (result , expected )
1868
+ return
1869
+
1870
+ elif isinstance (values , Categorical ):
1871
+ # GH#41291
1872
+ # Categorical doesn't implement sum or prod
1873
+ result = get_result ()
1874
+
1875
+ # with numeric_only=True, these are dropped, and we get
1876
+ # an empty DataFrame back
1877
+ expected = df .set_index (keys )[[]]
1878
+ if len (keys ) != 1 and op == "prod" :
1879
+ # TODO: why just prod and not sum?
1880
+ # Categorical is special without 'observed=True'
1881
+ lev = Categorical ([0 ], dtype = values .dtype )
1882
+ mi = MultiIndex .from_product ([lev , lev ], names = ["A" , "B" ])
1883
+ expected = DataFrame ([], columns = [], index = mi )
1884
+
1885
+ tm .assert_equal (result , expected )
1886
+ return
1887
+
1888
+ elif df .dtypes [0 ] == object :
1889
+ # FIXME: the test is actually wrong here, xref #41341
1890
+ result = get_result ()
1891
+ # In this case we have list-of-list, will raise TypeError,
1892
+ # and subsequently be dropped as nuisance columns
1893
+ expected = df .set_index (keys )[[]]
1894
+ tm .assert_equal (result , expected )
1895
+ return
1909
1896
1910
1897
result = get_result ()
1911
1898
expected = df .set_index (keys )[columns ]
0 commit comments