Skip to content

BUG: passing str to GroupBy.apply #42021

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Jul 17, 2021
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ Plotting

Groupby/resample/rolling
^^^^^^^^^^^^^^^^^^^^^^^^
-
- Fixed bug in :meth:`SeriesGroupBy.apply` where passing an unrecognized string argument failed to raise ``TypeError`` when the underlying ``Series`` is empty (:issue:`42021`)
-

Reshaping
Expand Down
10 changes: 10 additions & 0 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1245,7 +1245,17 @@ def f(g):
raise ValueError(
"func must be a callable if args or kwargs are supplied"
)
elif isinstance(func, str):
if hasattr(self, func):
res = getattr(self, func)
if callable(res):
return res()
return res

else:
raise TypeError(f"apply func should be callable, not '{func}'")
else:

f = func

# ignore SettingWithCopy here in case the user mutates
Expand Down
121 changes: 54 additions & 67 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1769,13 +1769,9 @@ def test_empty_groupby(columns, keys, values, method, op, request):
isinstance(values, Categorical)
and not isinstance(columns, list)
and op in ["sum", "prod"]
and method != "apply"
):
# handled below GH#41291
pass
elif isinstance(values, Categorical) and len(keys) == 1 and method == "apply":
mark = pytest.mark.xfail(raises=TypeError, match="'str' object is not callable")
request.node.add_marker(mark)
elif (
isinstance(values, Categorical)
and len(keys) == 1
Expand Down Expand Up @@ -1807,21 +1803,16 @@ def test_empty_groupby(columns, keys, values, method, op, request):
isinstance(values, Categorical)
and len(keys) == 2
and op in ["min", "max", "sum"]
and method != "apply"
):
mark = pytest.mark.xfail(
raises=AssertionError, match="(DataFrame|Series) are different"
)
request.node.add_marker(mark)
elif (
isinstance(values, pd.core.arrays.BooleanArray)
and op in ["sum", "prod"]
and method != "apply"
):
elif isinstance(values, pd.core.arrays.BooleanArray) and op in ["sum", "prod"]:
# We expect to get Int64 back for these
override_dtype = "Int64"

if isinstance(values[0], bool) and op in ("prod", "sum") and method != "apply":
if isinstance(values[0], bool) and op in ("prod", "sum"):
# sum/product of bools is an integer
override_dtype = "int64"

Expand All @@ -1845,66 +1836,62 @@ def get_result():
# i.e. SeriesGroupBy
if op in ["prod", "sum"]:
# ops that require more than just ordered-ness
if method != "apply":
# FIXME: apply goes through different code path
if df.dtypes[0].kind == "M":
# GH#41291
# datetime64 -> prod and sum are invalid
msg = "datetime64 type does not support"
with pytest.raises(TypeError, match=msg):
get_result()

return
elif isinstance(values, Categorical):
# GH#41291
msg = "category type does not support"
with pytest.raises(TypeError, match=msg):
get_result()

return
if df.dtypes[0].kind == "M":
# GH#41291
# datetime64 -> prod and sum are invalid
msg = "datetime64 type does not support"
with pytest.raises(TypeError, match=msg):
get_result()

return
elif isinstance(values, Categorical):
# GH#41291
msg = "category type does not support"
with pytest.raises(TypeError, match=msg):
get_result()

return
else:
# ie. DataFrameGroupBy
if op in ["prod", "sum"]:
# ops that require more than just ordered-ness
if method != "apply":
# FIXME: apply goes through different code path
if df.dtypes[0].kind == "M":
# GH#41291
# datetime64 -> prod and sum are invalid
result = get_result()

# with numeric_only=True, these are dropped, and we get
# an empty DataFrame back
expected = df.set_index(keys)[[]]
tm.assert_equal(result, expected)
return

elif isinstance(values, Categorical):
# GH#41291
# Categorical doesn't implement sum or prod
result = get_result()

# with numeric_only=True, these are dropped, and we get
# an empty DataFrame back
expected = df.set_index(keys)[[]]
if len(keys) != 1 and op == "prod":
# TODO: why just prod and not sum?
# Categorical is special without 'observed=True'
lev = Categorical([0], dtype=values.dtype)
mi = MultiIndex.from_product([lev, lev], names=["A", "B"])
expected = DataFrame([], columns=[], index=mi)

tm.assert_equal(result, expected)
return

elif df.dtypes[0] == object:
# FIXME: the test is actually wrong here, xref #41341
result = get_result()
# In this case we have list-of-list, will raise TypeError,
# and subsequently be dropped as nuisance columns
expected = df.set_index(keys)[[]]
tm.assert_equal(result, expected)
return
if df.dtypes[0].kind == "M":
# GH#41291
# datetime64 -> prod and sum are invalid
result = get_result()

# with numeric_only=True, these are dropped, and we get
# an empty DataFrame back
expected = df.set_index(keys)[[]]
tm.assert_equal(result, expected)
return

elif isinstance(values, Categorical):
# GH#41291
# Categorical doesn't implement sum or prod
result = get_result()

# with numeric_only=True, these are dropped, and we get
# an empty DataFrame back
expected = df.set_index(keys)[[]]
if len(keys) != 1 and op == "prod":
# TODO: why just prod and not sum?
# Categorical is special without 'observed=True'
lev = Categorical([0], dtype=values.dtype)
mi = MultiIndex.from_product([lev, lev], names=["A", "B"])
expected = DataFrame([], columns=[], index=mi)

tm.assert_equal(result, expected)
return

elif df.dtypes[0] == object:
# FIXME: the test is actually wrong here, xref #41341
result = get_result()
# In this case we have list-of-list, will raise TypeError,
# and subsequently be dropped as nuisance columns
expected = df.set_index(keys)[[]]
tm.assert_equal(result, expected)
return

result = get_result()
expected = df.set_index(keys)[columns]
Expand Down