-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Applying on empty DataFrame returns different types #16621
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
Comments
I suspect that |
agree with @TomAugspurger might be excepting out of the inner loop. @gzcf if you want to investigate and see if you can make a fix that passes the test suite would be fine. |
I'm glad to help. Let me take some time to fix it. |
After I read related codes, I found this is a intended behavior. Check issue #2476 and def _apply_empty_result(self, func, axis, reduce, *args, **kwds):
if reduce is None:
reduce = False
try:
reduce = not isinstance(func(_EMPTY_SERIES, *args, **kwds),
Series)
except Exception:
pass
if reduce:
return Series(NA, index=self._get_agg_axis(axis))
else:
return self.copy() Look, these code will try guessing return type by calling func an empty Series. I don't think this is a good implementation. it's bad to There are some choices:
Please give me some advice. |
I think the correct resolution is to use a series with data types from the data frame, instead of |
Code Sample
Problem description
When apply individual functions on the same empty
DataFrame
, it return different types.That's wired since their only difference is
row['a']
expression which is never executed.When df is not empty, both return
pd.Series
.Test on pandas 1.19.1 and latest version 0.20.2.
Expected Output
I expected it always return
pd.Series
Output of
pd.show_versions()
The text was updated successfully, but these errors were encountered: