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 1 commit
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
Prev Previous commit
Next Next commit
Fix stylistic errors
  • Loading branch information
OXPHOS committed Mar 15, 2016
commit 76fbacf4a6818ad10341d7189243b7156c431ed3
2 changes: 1 addition & 1 deletion pandas/tools/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def pivot_table(data, values=None, index=None, columns=None, aggfunc='mean',

if margins:
if dropna:
data = data[data.notnull().all(axis = 1)]
data = data[data.notnull().all(axis=1)]
table = _add_margins(table, data, values, rows=index,
cols=columns, aggfunc=aggfunc,
margins_name=margins_name)
Expand Down
38 changes: 19 additions & 19 deletions pandas/tools/tests/test_pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -935,34 +935,34 @@ def test_crosstab_no_overlap(self):
expected = pd.DataFrame()

tm.assert_frame_equal(actual, expected)

def test_margin_ignore_dropna_bug(self):
# GH 12577
# pivot_table counts null into margin ('All')
# pivot_table counts null into margin ('All')
# when margins=true and dropna=true

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)
'b': [3, 3, 4, 4, 4, 4]})
actual = pd.crosstab(df.a, df.b, margins=True)
Copy link
Contributor

Choose a reason for hiding this comment

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

explictly add dropna=True for these. tests

then add a test for dropna=False (same set of tests)
and you will find it raises. the level is None fails.

In [10]: pd.crosstab(df.a,df.b,margins=True,dropna=False)
KeyError: 'Level None not found'

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')
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)

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')
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)

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')
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__':
Expand Down