Skip to content

Commit bc1e766

Browse files
Revert "TST: fix groupby-empty xfails (pandas-dev#44092)"
This reverts commit 884d00f.
1 parent faacb72 commit bc1e766

File tree

3 files changed

+12
-80
lines changed

3 files changed

+12
-80
lines changed

pandas/core/groupby/generic.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,7 +1622,6 @@ def func(df):
16221622
result = [index[i] if i >= 0 else np.nan for i in indices]
16231623
return df._constructor_sliced(result, index=res.index)
16241624

1625-
func.__name__ = "idxmax"
16261625
result = self._python_apply_general(func, self._obj_with_exclusions)
16271626
self._maybe_warn_numeric_only_depr("idxmax", result, numeric_only)
16281627
return result
@@ -1658,7 +1657,6 @@ def func(df):
16581657
result = [index[i] if i >= 0 else np.nan for i in indices]
16591658
return df._constructor_sliced(result, index=res.index)
16601659

1661-
func.__name__ = "idxmin"
16621660
result = self._python_apply_general(func, self._obj_with_exclusions)
16631661
self._maybe_warn_numeric_only_depr("idxmin", result, numeric_only)
16641662
return result

pandas/core/groupby/ops.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -809,18 +809,6 @@ def apply(
809809
mutated = True
810810
result_values.append(res)
811811

812-
# getattr pattern for __name__ is needed for functools.partial objects
813-
if len(group_keys) == 0 and getattr(f, "__name__", None) not in [
814-
"idxmin",
815-
"idxmax",
816-
"nanargmin",
817-
"nanargmax",
818-
]:
819-
# If group_keys is empty, then no function calls have been made,
820-
# so we will not have raised even if this is an invalid dtype.
821-
# So do one dummy call here to raise appropriate TypeError.
822-
f(data.iloc[:0])
823-
824812
return result_values, mutated
825813

826814
@cache_readonly

pandas/tests/groupby/test_groupby.py

Lines changed: 12 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,23 +1922,17 @@ def test_pivot_table_values_key_error():
19221922
)
19231923
@pytest.mark.filterwarnings("ignore:Dropping invalid columns:FutureWarning")
19241924
@pytest.mark.filterwarnings("ignore:.*Select only valid:FutureWarning")
1925-
def test_empty_groupby(columns, keys, values, method, op, request, using_array_manager):
1925+
def test_empty_groupby(columns, keys, values, method, op, request):
19261926
# GH8093 & GH26411
19271927
override_dtype = None
19281928

19291929
if (
19301930
isinstance(values, Categorical)
19311931
and not isinstance(columns, list)
1932-
and op in ["sum", "prod", "skew", "mad"]
1932+
and op in ["sum", "prod"]
19331933
):
19341934
# handled below GH#41291
1935-
1936-
if using_array_manager and op == "mad":
1937-
right_msg = "Cannot interpret 'CategoricalDtype.* as a data type"
1938-
msg = "Regex pattern \"'Categorical' does not implement.*" + right_msg
1939-
mark = pytest.mark.xfail(raises=AssertionError, match=msg)
1940-
request.node.add_marker(mark)
1941-
1935+
pass
19421936
elif (
19431937
isinstance(values, Categorical)
19441938
and len(keys) == 1
@@ -1957,7 +1951,11 @@ def test_empty_groupby(columns, keys, values, method, op, request, using_array_m
19571951
raises=TypeError, match="'Categorical' does not implement"
19581952
)
19591953
request.node.add_marker(mark)
1960-
elif isinstance(values, Categorical) and len(keys) == 1 and op in ["sum", "prod"]:
1954+
elif (
1955+
isinstance(values, Categorical)
1956+
and len(keys) == 1
1957+
and op in ["mad", "min", "max", "sum", "prod", "skew"]
1958+
):
19611959
mark = pytest.mark.xfail(
19621960
raises=AssertionError, match="(DataFrame|Series) are different"
19631961
)
@@ -1971,20 +1969,6 @@ def test_empty_groupby(columns, keys, values, method, op, request, using_array_m
19711969
raises=AssertionError, match="(DataFrame|Series) are different"
19721970
)
19731971
request.node.add_marker(mark)
1974-
1975-
elif (
1976-
op == "mad"
1977-
and not isinstance(columns, list)
1978-
and isinstance(values, pd.DatetimeIndex)
1979-
and values.tz is not None
1980-
and using_array_manager
1981-
):
1982-
mark = pytest.mark.xfail(
1983-
raises=TypeError,
1984-
match=r"Cannot interpret 'datetime64\[ns, US/Eastern\]' as a data type",
1985-
)
1986-
request.node.add_marker(mark)
1987-
19881972
elif isinstance(values, BooleanArray) and op in ["sum", "prod"]:
19891973
# We expect to get Int64 back for these
19901974
override_dtype = "Int64"
@@ -2015,29 +1999,19 @@ def get_result():
20151999

20162000
if columns == "C":
20172001
# i.e. SeriesGroupBy
2018-
if op in ["prod", "sum", "skew"]:
2002+
if op in ["prod", "sum"]:
20192003
# ops that require more than just ordered-ness
20202004
if df.dtypes[0].kind == "M":
20212005
# GH#41291
20222006
# datetime64 -> prod and sum are invalid
2023-
if op == "skew":
2024-
msg = "does not support reduction 'skew'"
2025-
else:
2026-
msg = "datetime64 type does not support"
2007+
msg = "datetime64 type does not support"
20272008
with pytest.raises(TypeError, match=msg):
20282009
get_result()
20292010

20302011
return
2031-
if op in ["prod", "sum", "skew", "mad"]:
2032-
if isinstance(values, Categorical):
2012+
elif isinstance(values, Categorical):
20332013
# GH#41291
2034-
if op == "mad":
2035-
# mad calls mean, which Categorical doesn't implement
2036-
msg = "does not support reduction 'mean'"
2037-
elif op == "skew":
2038-
msg = f"does not support reduction '{op}'"
2039-
else:
2040-
msg = "category type does not support"
2014+
msg = "category type does not support"
20412015
with pytest.raises(TypeError, match=msg):
20422016
get_result()
20432017

@@ -2084,34 +2058,6 @@ def get_result():
20842058
tm.assert_equal(result, expected)
20852059
return
20862060

2087-
if (
2088-
op in ["mad", "min", "max", "skew"]
2089-
and isinstance(values, Categorical)
2090-
and len(keys) == 1
2091-
):
2092-
# Categorical doesn't implement, so with numeric_only=True
2093-
# these are dropped and we get an empty DataFrame back
2094-
result = get_result()
2095-
expected = df.set_index(keys)[[]]
2096-
2097-
# with numeric_only=True, these are dropped, and we get
2098-
# an empty DataFrame back
2099-
if len(keys) != 1:
2100-
# Categorical is special without 'observed=True'
2101-
lev = Categorical([0], dtype=values.dtype)
2102-
mi = MultiIndex.from_product([lev, lev], names=keys)
2103-
expected = DataFrame([], columns=[], index=mi)
2104-
else:
2105-
# all columns are dropped, but we end up with one row
2106-
# Categorical is special without 'observed=True'
2107-
lev = Categorical([0], dtype=values.dtype)
2108-
ci = Index(lev, name=keys[0])
2109-
expected = DataFrame([], columns=[], index=ci)
2110-
# expected = df.set_index(keys)[columns]
2111-
2112-
tm.assert_equal(result, expected)
2113-
return
2114-
21152061
result = get_result()
21162062
expected = df.set_index(keys)[columns]
21172063
if override_dtype is not None:

0 commit comments

Comments
 (0)