-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
BUG: fixed buggy behavior in pivot_table with margins=True and numeric columns #46778
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 all commits
e4b8cbf
32f394d
507c736
0d616f7
f654a3a
9427649
555397e
afb24c1
76d2522
ac54f5c
786cd87
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 |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
|
||
from pandas.core.dtypes.cast import maybe_downcast_to_dtype | ||
from pandas.core.dtypes.common import ( | ||
is_integer, | ||
is_integer_dtype, | ||
is_list_like, | ||
is_nested_list_like, | ||
|
@@ -409,6 +410,17 @@ def _all_key(key): | |
|
||
# slight hack | ||
new_order = [len(cols)] + list(range(len(cols))) | ||
|
||
# GH26568 reorder_levels improperly uses a key that is also a number | ||
# If a number in new_order is also a key, | ||
# convert new_order to their respective keys in row_margin.index.names | ||
for key in row_margin.index.names: | ||
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. Can't this just be a 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. @mroeschke I tried using 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. If I am interpreting your for loop correctly, you're converting 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'm converting positions in the
What I want is something like this:
Are we thinking about it the same way? 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. Based on your for loop though, it appears you want to take on the names not the actual values? |
||
if is_integer(key) and key <= len(cols): | ||
# change new_order to use keys instead, then break loop | ||
for new_order_index, val in enumerate(new_order): | ||
new_order[new_order_index] = row_margin.index.names[val] | ||
break | ||
|
||
row_margin.index = row_margin.index.reorder_levels(new_order) | ||
else: | ||
row_margin = Series(np.nan, index=result.columns) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2184,6 +2184,22 @@ def test_pivot_table_with_margins_and_numeric_columns(self): | |
|
||
tm.assert_frame_equal(result, expected) | ||
|
||
def test_pivot_table_with_margins_and_columns_equals_one(self): | ||
# GH 26568 | ||
df = DataFrame([["a", "x", 1], ["a", "y", 2], ["b", "y", 3], ["b", "z", 4]]) | ||
|
||
result = df.pivot_table( | ||
index=0, columns=1, values=2, aggfunc="sum", fill_value=0, margins=True | ||
) | ||
|
||
expected = DataFrame([[1, 2, 0, 3], [0, 3, 4, 7], [1, 5, 4, 10]]) | ||
expected.columns = ["x", "y", "z", "All"] | ||
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. These attributes can be included in the DataFrame constructor e.g. |
||
expected.index = ["a", "b", "All"] | ||
expected.columns.name = 1 | ||
expected.index.name = 0 | ||
|
||
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
class TestPivot: | ||
def test_pivot(self): | ||
|
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.
This should describe the user facing impact of the change not the internal change i.e. the bug fix of
pivot_table