-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Fix calling groupBy(...).apply(func) on an empty dataframe invokes func #48579
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
Changes from 4 commits
432ffba
519fa10
f734276
7285e60
1ad10e1
2964658
98b3030
405e28a
6003dc3
60941f4
862a58f
ee08f0f
348b14a
336c6c7
6972fd4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1331,3 +1331,31 @@ def test_result_name_when_one_group(name): | |
expected = Series([1, 2], name=name) | ||
|
||
tm.assert_series_equal(result, expected) | ||
|
||
|
||
def test_empty_df(): | ||
empty_df = DataFrame({"a": [], "b": []}) | ||
|
||
# Both operations should return an empty series instead of IndexError for apply UDF | ||
result = empty_df.groupby("a", group_keys=True).b.apply(lambda x: x.values[-1]) | ||
expected = empty_df.groupby("a", group_keys=True).b.agg("sum") | ||
|
||
tm.assert_series_equal(result, expected) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"error_type", | ||
[ | ||
TypeError, | ||
ValueError, | ||
IndexError, | ||
], | ||
) | ||
def test_udf_raise_error_on_empty_df(error_type): | ||
empty_df = DataFrame({"a": [], "b": []}) | ||
|
||
def f(group): | ||
raise error_type | ||
|
||
# Exception should not be raised. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would expect the exception in Since this isn't directly related the original issue, maybe this can be addressed later. |
||
empty_df.groupby("a", group_keys=True).b.apply(f) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you assert the result of this operation? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you compare both of these results to an actual empty Series? Just in case a code change returns the same, incorrect result for both
result
andexpected
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mroeschke I am having an issue equating it to an empty
series
.Returns
How do I create an empty
series
that equalsresult
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.