-
-
Notifications
You must be signed in to change notification settings - Fork 19.3k
BUG: Styler.apply consistently manages Series return objects aligning labels.
#42014
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
BUG: Styler.apply consistently manages Series return objects aligning labels.
#42014
Conversation
…eries return objects
Styler.apply consistently manages Series return objects aligning labels.Styler.apply consistently manages Series return objects aligning labels.
…_apply_funcs_index_columns
…_apply_funcs_index_columns
…_apply_funcs_index_columns
|
This pull request is stale because it has been open for thirty days with no activity. Please update or respond to this comment if you're still interested in working on this. |
…_apply_funcs_index_columns
…_apply_funcs_index_columns
…_apply_funcs_index_columns
…_apply_funcs_index_columns
jreback
left a comment
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.
lgtm small comments
| .. versionchanged:: 1.3.0 | ||
| .. versionchanged:: 1.4.0 |
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.
can you update the examples to show a Series return matching labels/index (e.g. not the same length)
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.
added
…_apply_funcs_index_columns
…_apply_funcs_index_columns
…_apply_funcs_index_columns # Conflicts: # doc/source/whatsnew/v1.4.0.rst
…_apply_funcs_index_columns
…_apply_funcs_index_columns # Conflicts: # doc/source/whatsnew/v1.4.0.rst
|
thanks @attack68 |
This issue is due to the asymmetric application of
DataFrame.apply(#42005).The bug is that when using
Styler.applythe treatment of Series return objects is applied foraxis=0and overwritten foraxis=1.s.apply(series_ret, axis=0)s.apply(series_ret, axis=1)Also the application has been expanded to allow (for axis=0 | 1) Series return objects that are not the same size, or (for axis=None) DataFrame return objects that are not the same size, if they contain only relevant index labels. This allows for more flexible user defined functions. Potentially it also increases performance by not having to loop over labels that no longer exist.
s.apply(series_ret, axis=0)ValueError: Function returned the wrong shape.s.apply(series_ret, axis=1)ValueError: Length mismatch: Expected axis has 1 elements, new values have 2 elementss.apply(array_ret, axis=0)ValueError: Function returned the wrong shape.ValueError: Function created invalid index labels. Usually, this is the result of the function returning a Series which contains invalid labels, or returning an incorrectly shaped, list-like object which cannot be mapped to labels, possibly due to applying the function along the wrong axis.s.apply(array_ret, axis=1)ValueError: Length mismatch: Expected axis has 1 elements, new values have 2 elementsValueError: Function created invalid index labels. Usually, this is the result of the function returning a Series which contains invalid labels, or returning an incorrectly shaped, list-like object which cannot be mapped to labels, possibly due to applying the function along the wrong axis.s.apply(ser_no_idx_ret, axis=0)KeyError: 0ValueError: Function created invalid index labels. Usually, this is the result of the function returning a Series which contains invalid labels, or returning an incorrectly shaped, list-like object which cannot be mapped to labels, possibly due to applying the function along the wrong axis.s.apply(ser_no_idx_ret, axis=1)ValueError: Function created invalid index labels. Usually, this is the result of the function returning a Series which contains invalid labels, or returning an incorrectly shaped, list-like object which cannot be mapped to labels, possibly due to applying the function along the wrong axis.Note all errors do report the shapes, I just didn't include in the table for brevity.
Edge Cases
The edge case here is that if a user has a DataFrame with a default range index (or a DataFrame with some indexes the same as those in default, e.g. 0 or 1) then even returning arrays of the wrong size may work, since
DataFrame.applyassigns them to a RangeIndex.To be consistent with the above this should raise, but it is impossible to hook into the return value of the user-defined function and determine if it is a Series (where non-matching shapes are OK) or list-like (where matching shapes should be enforced)
s.apply(array_ret, axis=0)ValueError: Function returned the wrong shape.s.apply(array_ret, axis=1)ValueError: Length mismatch: Expected axis has 1 elements, new values have 2 elementsASV
Benchmarks unchanged.