@@ -87,6 +87,26 @@ def test_pivot_table_dropna(self):
87
87
tm .assert_index_equal (pv_col .columns , m )
88
88
tm .assert_index_equal (pv_ind .index , m )
89
89
90
+ df = DataFrame ([[1 , 'a' , 'A' ], [1 , 'b' , 'B' ], [1 , 'c' , None ]],
91
+ columns = ['x' , 'y' , 'z' ])
92
+ actual = df .pivot_table (values = 'x' , index = 'y' , columns = 'z' ,
93
+ aggfunc = 'sum' , fill_value = 0 , margins = True ,
94
+ dropna = True )
95
+ expected = pd .DataFrame ([[1.0 , 0.0 , 1.0 ], [0.0 , 1.0 , 1.0 ],
96
+ [1.0 , 1.0 , 2.0 ]])
97
+ expected .index = Index (['a' , 'b' , 'All' ], name = 'y' )
98
+ expected .columns = Index (['A' , 'B' , 'All' ], name = 'z' )
99
+ tm .assert_frame_equal (actual , expected )
100
+
101
+ actual = df .pivot_table (values = 'x' , index = 'y' , columns = 'z' ,
102
+ aggfunc = 'sum' , fill_value = 0 , margins = True ,
103
+ dropna = False )
104
+ expected = pd .DataFrame ([[1.0 , 0.0 , 0.0 , 1.0 ], [0.0 , 1.0 , 0.0 , 1.0 ],
105
+ [0.0 , 0.0 , 1.0 , 1.0 ], [1.0 , 1.0 , 1.0 , 3.0 ]])
106
+ expected .index = Index (['a' , 'b' , 'c' , 'All' ], name = 'y' )
107
+ expected .columns = Index (['A' , 'B' , None , 'All' ], name = 'z' )
108
+ tm .assert_frame_equal (actual , expected )
109
+
90
110
def test_pass_array (self ):
91
111
result = self .data .pivot_table (
92
112
'D' , index = self .data .A , columns = self .data .C )
@@ -1080,7 +1100,8 @@ def test_margin_dropna(self):
1080
1100
df = DataFrame ({'a' : [1 , np .nan , np .nan , np .nan , 2 , np .nan ],
1081
1101
'b' : [3 , np .nan , 4 , 4 , 4 , 4 ]})
1082
1102
actual = pd .crosstab (df .a , df .b , margins = True , dropna = False )
1083
- expected = pd .DataFrame ([[1 , 0 , 0 , 1 ], [0 , 1 , 0 , 1 ], [0 , 3 , 1 , 4 ], [1 , 4 , 1 , 6 ]])
1103
+ expected = pd .DataFrame ([[1 , 0 , 0 , 1 ], [0 , 1 , 0 , 1 ], [0 , 3 , 1 , 4 ],
1104
+ [1 , 4 , 1 , 6 ]])
1084
1105
expected .index = Index ([1.0 , 2.0 , np .nan , 'All' ], name = 'a' )
1085
1106
expected .columns = Index ([3.0 , 4.0 , np .nan , 'All' ], name = 'b' )
1086
1107
tm .assert_frame_equal (actual , expected )
@@ -1095,8 +1116,9 @@ def test_margin_dropna(self):
1095
1116
actual = pd .crosstab (a , [b , c ], rownames = ['a' ],
1096
1117
colnames = ['b' , 'c' ], margins = True , dropna = False )
1097
1118
1098
- m = MultiIndex (levels = [Index (['All' , np .nan , 'one' , 'two' ]),
1099
- Index (['' , 'dull' , 'shiny' ])], labels = [[1 , 1 , 2 , 2 , 3 , 3 , 0 ],
1119
+ m = MultiIndex (levels = [Index (['All' , np .nan , 'one' , 'two' ]),
1120
+ Index (['' , 'dull' , 'shiny' ])],
1121
+ labels = [[1 , 1 , 2 , 2 , 3 , 3 , 0 ],
1100
1122
[1 , 2 , 1 , 2 , 1 , 2 , 0 ]], names = ['b' , 'c' ])
1101
1123
expected = DataFrame ([[0 , 0 , 1 , 0 , 1 , 0 , 2 ], [0 , 1 , 2 , 0 , 1 , 1 , 5 ],
1102
1124
[0 , 1 , 3 , 0 , 2 , 1 , 7 ]], columns = m )
@@ -1105,13 +1127,12 @@ def test_margin_dropna(self):
1105
1127
1106
1128
actual = pd .crosstab ([a , b ], c , rownames = ['a' , 'b' ],
1107
1129
colnames = ['c' ], margins = True , dropna = False )
1108
-
1109
- print actual .index
1110
- m = MultiIndex (levels = [['All' , 'bar' , 'foo' ], ['' , np .nan , 'one' , 'two' ]],
1130
+ m = MultiIndex (levels = [['All' , 'bar' , 'foo' ],
1131
+ ['' , np .nan , 'one' , 'two' ]],
1111
1132
labels = [[1 , 1 , 1 , 2 , 2 , 2 , 0 ], [1 , 2 , 3 , 1 , 2 , 3 , 0 ]],
1112
1133
names = ['a' , 'b' ])
1113
- expected = DataFrame ([[0 , 0 , 0 ], [1 , 0 , 1 ], [1 , 0 , 1 ], [0 , 1 , 1 ], [ 2 , 0 , 2 ], [ 1 , 1 , 2 ],
1114
- [5 , 2 , 7 ]], index = m )
1134
+ expected = DataFrame ([[0 , 0 , 0 ], [1 , 0 , 1 ], [1 , 0 , 1 ], [0 , 1 , 1 ],
1135
+ [2 , 0 , 2 ], [ 1 , 1 , 2 ], [ 5 , 2 , 7 ]], index = m )
1115
1136
expected .columns = Index (['dull' , 'shiny' , 'All' ], name = 'c' )
1116
1137
tm .assert_frame_equal (actual , expected )
1117
1138
0 commit comments