Skip to content
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: Crosstab margins ignoring dropna #12614

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions pandas/tools/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,15 @@ def pivot_table(data, values=None, index=None, columns=None, aggfunc='mean',
table = table.fillna(value=fill_value, downcast='infer')

if margins:
table = _add_margins(table, data, values, rows=index,
cols=columns, aggfunc=aggfunc,
margins_name=margins_name)
if dropna:
data_dropna = data[data.notnull().all(axis = 1)]
table = _add_margins(table, data_dropna, values, rows=index,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need to repeat the if, just do:

if dropna:
   data = data[......]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean like below? I felt unsafe changing data values but if you're okay with it I'll update the codes.

if dropna: data = data[data.notnull().all(axis = 1)] table = _add_margins(table, data, values, rows=index, cols=columns, aggfunc=aggfunc, margins_name=margins_name)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how are you changing data values?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think she means over-writing data. That's fine if all tests clear!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Thanks!

cols=columns, aggfunc=aggfunc,
margins_name=margins_name)
else:
table = _add_margins(table, data, values, rows=index,
cols=columns, aggfunc=aggfunc,
margins_name=margins_name)

# discard the top level
if values_passed and not values_multi and not table.empty:
Expand Down
23 changes: 23 additions & 0 deletions pandas/tools/tests/test_pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,29 @@ def test_crosstab_no_overlap(self):
expected = pd.DataFrame()

tm.assert_frame_equal(actual, expected)

def test_margin_ignore_dropna_bug(self):
# Bug#12577
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

give a small descrption of the problem as well (in a comment)

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)
expected = pd.DataFrame([[1, 0, 1], [1, 3, 4], [2, 3, 5]])
expected.index = Index([1.0, 2.0, 'All'], name = 'a')
expected.columns = Index([3, 4, 'All'], name = 'b')
tm.assert_frame_equal(actual, expected)

df = DataFrame({'a':[1, np.nan, np.nan, np.nan, 2, np.nan], 'b':[3, np.nan, 4, 4, 4, 4]})
actual = pd.crosstab(df.a,df.b, margins=True)
expected = pd.DataFrame([[1, 0, 1], [0, 1, 1], [1, 1, 2]])
expected.index = Index([1.0, 2.0, 'All'], name = 'a')
expected.columns = Index([3.0, 4.0, 'All'], name = 'b')
tm.assert_frame_equal(actual, expected)

df = DataFrame({'a':[1, np.nan, np.nan, np.nan, np.nan, 2], 'b':[3, 3, 4, 4, 4, 4]})
actual = pd.crosstab(df.a,df.b, margins=True)
expected = pd.DataFrame([[1, 0, 1], [0, 1, 1], [1, 1, 2]])
expected.index = Index([1.0, 2.0, 'All'], name = 'a')
expected.columns = Index([3, 4, 'All'], name = 'b')
tm.assert_frame_equal(actual, expected)

if __name__ == '__main__':
import nose
Expand Down