-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG:Pivot table drops column/index names=nan when dropna=false #16142
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
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 | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -132,7 +132,7 @@ def pivot_table(data, values=None, index=None, columns=None, aggfunc='mean', | |||||||||||||||||||||||||
pass | ||||||||||||||||||||||||||
values = list(values) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
grouped = data.groupby(keys) | ||||||||||||||||||||||||||
grouped = data.groupby(keys, dropna=dropna) | ||||||||||||||||||||||||||
agged = grouped.agg(aggfunc) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
table = agged | ||||||||||||||||||||||||||
|
@@ -159,15 +159,15 @@ def pivot_table(data, values=None, index=None, columns=None, aggfunc='mean', | |||||||||||||||||||||||||
if isinstance(table, DataFrame): | ||||||||||||||||||||||||||
table = table.sort_index(axis=1) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
if fill_value is not None: | ||||||||||||||||||||||||||
table = table.fillna(value=fill_value, downcast='infer') | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
if margins: | ||||||||||||||||||||||||||
if dropna: | ||||||||||||||||||||||||||
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 remove this df = pd.DataFrame({'a': [1, 2, 2, 2, 2, np.nan],
'b': [3, 3, 4, 4, 4, 4]})
actual = pd.crosstab(df.a, df.b, margins=True, dropna=False)
expected = pd.DataFrame([[1, 0, 1], [1, 3, 4], [2, 4, 6]])
expected.index = Index([1.0, 2.0, 'All'], name='a')
expected.columns = Index([3, 4, 'All'], name='b') Here's the result and expected
You have more experience with this section of the code than I do, but the margins on the 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. You're definitely right. I think it should be (if
|
||||||||||||||||||||||||||
data = data[data.notnull().all(axis=1)] | ||||||||||||||||||||||||||
table = _add_margins(table, data, values, rows=index, | ||||||||||||||||||||||||||
cols=columns, aggfunc=aggfunc, | ||||||||||||||||||||||||||
margins_name=margins_name) | ||||||||||||||||||||||||||
margins_name=margins_name, dropna=dropna) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
if fill_value is not None: | ||||||||||||||||||||||||||
table = table.fillna(value=fill_value, downcast='infer') | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
# discard the top level | ||||||||||||||||||||||||||
if values_passed and not values_multi and not table.empty and \ | ||||||||||||||||||||||||||
|
@@ -188,7 +188,7 @@ def pivot_table(data, values=None, index=None, columns=None, aggfunc='mean', | |||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
def _add_margins(table, data, values, rows, cols, aggfunc, | ||||||||||||||||||||||||||
margins_name='All'): | ||||||||||||||||||||||||||
margins_name='All', dropna=True): | ||||||||||||||||||||||||||
if not isinstance(margins_name, compat.string_types): | ||||||||||||||||||||||||||
raise ValueError('margins_name argument must be a string') | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
@@ -219,7 +219,8 @@ def _add_margins(table, data, values, rows, cols, aggfunc, | |||||||||||||||||||||||||
marginal_result_set = _generate_marginal_results(table, data, values, | ||||||||||||||||||||||||||
rows, cols, aggfunc, | ||||||||||||||||||||||||||
grand_margin, | ||||||||||||||||||||||||||
margins_name) | ||||||||||||||||||||||||||
margins_name, | ||||||||||||||||||||||||||
dropna=dropna) | ||||||||||||||||||||||||||
if not isinstance(marginal_result_set, tuple): | ||||||||||||||||||||||||||
return marginal_result_set | ||||||||||||||||||||||||||
result, margin_keys, row_margin = marginal_result_set | ||||||||||||||||||||||||||
|
@@ -277,8 +278,7 @@ def _compute_grand_margin(data, values, aggfunc, | |||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
def _generate_marginal_results(table, data, values, rows, cols, aggfunc, | ||||||||||||||||||||||||||
grand_margin, | ||||||||||||||||||||||||||
margins_name='All'): | ||||||||||||||||||||||||||
grand_margin, margins_name='All', dropna=True): | ||||||||||||||||||||||||||
if len(cols) > 0: | ||||||||||||||||||||||||||
# need to "interleave" the margins | ||||||||||||||||||||||||||
table_pieces = [] | ||||||||||||||||||||||||||
|
@@ -288,7 +288,8 @@ def _all_key(key): | |||||||||||||||||||||||||
return (key, margins_name) + ('',) * (len(cols) - 1) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
if len(rows) > 0: | ||||||||||||||||||||||||||
margin = data[rows + values].groupby(rows).agg(aggfunc) | ||||||||||||||||||||||||||
margin = data[rows + | ||||||||||||||||||||||||||
values].groupby(rows, dropna=dropna).agg(aggfunc) | ||||||||||||||||||||||||||
cat_axis = 1 | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
for key, piece in table.groupby(level=0, axis=cat_axis): | ||||||||||||||||||||||||||
|
@@ -325,7 +326,8 @@ def _all_key(key): | |||||||||||||||||||||||||
margin_keys = table.columns | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
if len(cols) > 0: | ||||||||||||||||||||||||||
row_margin = data[cols + values].groupby(cols).agg(aggfunc) | ||||||||||||||||||||||||||
row_margin = data[cols + | ||||||||||||||||||||||||||
values].groupby(cols, dropna=dropna).agg(aggfunc) | ||||||||||||||||||||||||||
row_margin = row_margin.stack() | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
# slight hack | ||||||||||||||||||||||||||
|
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.
Do you have some unintentional changes in here? This shouldn't be removed.
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.
I feel like the Index,
Index([None, u'A', u'B'], dtype='object')
, needs to be passed toCategorical
when doingMultiIndex
, as whendropna=False
,None
could also be the index/column name. Or I didn't get this correctly?