-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Make pivot values work with most iterables #12017
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
Conversation
what issue is this supposed to address? need tests |
@@ -89,8 +89,9 @@ def pivot_table(data, values=None, index=None, columns=None, aggfunc='mean', | |||
|
|||
values_passed = values is not None | |||
if values_passed: | |||
if isinstance(values, (list, tuple)): | |||
if hasattr(values, '__iter__') and not isinstance(values, str): |
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.
use is_list_like
?
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.
Yes that's exactly what I was going for, thanks for the tip!
The problem is that right now the only objects that can be passed in as values to a pivot table are lists and tuples. I would like to expand this so that other iterable objects like generators and (in python 3) dictionary methods like
This works in Python 2, but returns an error in Python 3 because @MaximilianR pointed out that using As far as unit tests go, I'm still learning and I'm not sure how the best way to write those for this would be, but I'll continue looking into it. I may have jumped the gun on this PR a bit, so please let me know if theres a better way I should proceed. |
In lieu of anyone more experienced answering your questions:
Congrats on being halfway to your first PR. |
this needs tests |
@@ -736,6 +736,24 @@ def test_pivot_dtaccessor(self): | |||
index=['X', 'Y'], columns=exp_col) | |||
tm.assert_frame_equal(result, expected) | |||
|
|||
def test_pivot_table_with_iterator_values(self): | |||
aggs = {'D': 'sum', 'E': 'mean'} |
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.
add the issue number as a comment here
lgtm. pls add a whatsnew note in enhancements section, use this PR number as the issue number |
thanks! |
Thanks @jreback and @MaximilianR for all your help and suggestions on my first PR! |
Previously the values check for pivot tables was only looking for lists or tuples. This patch allows for any generators or other iterables (such as dict.keys() in python 3) to be passed as values. They will be converted to lists when they are identified as iterables, excluding strings. The performance hit from the extra list type assignment to iterables that are already lists or tuples should be minor.