BUG: Crosstab with margins=True ignoring dropna=True · pandas-dev/pandas@f7faee0 (original) (raw)
`@@ -936,6 +936,35 @@ def test_crosstab_no_overlap(self):
`
936
936
``
937
937
`tm.assert_frame_equal(actual, expected)
`
938
938
``
``
939
`+
def test_margin_ignore_dropna_bug(self):
`
``
940
`+
GH 12577
`
``
941
`+
pivot_table counts null into margin ('All')
`
``
942
`+
when margins=true and dropna=true
`
``
943
+
``
944
`+
df = pd.DataFrame({'a': [1, 2, 2, 2, 2, np.nan],
`
``
945
`+
'b': [3, 3, 4, 4, 4, 4]})
`
``
946
`+
actual = pd.crosstab(df.a, df.b, margins=True, dropna=True)
`
``
947
`+
expected = pd.DataFrame([[1, 0, 1], [1, 3, 4], [2, 3, 5]])
`
``
948
`+
expected.index = Index([1.0, 2.0, 'All'], name='a')
`
``
949
`+
expected.columns = Index([3, 4, 'All'], name='b')
`
``
950
`+
tm.assert_frame_equal(actual, expected)
`
``
951
+
``
952
`+
df = DataFrame({'a': [1, np.nan, np.nan, np.nan, 2, np.nan],
`
``
953
`+
'b': [3, np.nan, 4, 4, 4, 4]})
`
``
954
`+
actual = pd.crosstab(df.a, df.b, margins=True, dropna=True)
`
``
955
`+
expected = pd.DataFrame([[1, 0, 1], [0, 1, 1], [1, 1, 2]])
`
``
956
`+
expected.index = Index([1.0, 2.0, 'All'], name='a')
`
``
957
`+
expected.columns = Index([3.0, 4.0, 'All'], name='b')
`
``
958
`+
tm.assert_frame_equal(actual, expected)
`
``
959
+
``
960
`+
df = DataFrame({'a': [1, np.nan, np.nan, np.nan, np.nan, 2],
`
``
961
`+
'b': [3, 3, 4, 4, 4, 4]})
`
``
962
`+
actual = pd.crosstab(df.a, df.b, margins=True, dropna=True)
`
``
963
`+
expected = pd.DataFrame([[1, 0, 1], [0, 1, 1], [1, 1, 2]])
`
``
964
`+
expected.index = Index([1.0, 2.0, 'All'], name='a')
`
``
965
`+
expected.columns = Index([3, 4, 'All'], name='b')
`
``
966
`+
tm.assert_frame_equal(actual, expected)
`
``
967
+
939
968
`if name == 'main':
`
940
969
`import nose
`
941
970
`nose.runmodule(argv=[file, '-vvs', '-x', '--pdb', '--pdb-failure'],
`