@@ -1212,6 +1212,41 @@ def test_crosstab_errors(self):
1212
1212
with tm .assertRaisesRegexp (ValueError , error ):
1213
1213
pd .crosstab (df .a , df .b , normalize = 'all' , margins = 42 )
1214
1214
1215
+ def test_crosstab_with_numpy_size (self ):
1216
+ # GH 4003
1217
+ df = pd .DataFrame ({'A' : ['one' , 'one' , 'two' , 'three' ] * 6 ,
1218
+ 'B' : ['A' , 'B' , 'C' ] * 8 ,
1219
+ 'C' : ['foo' , 'foo' , 'foo' , 'bar' , 'bar' , 'bar' ] * 4 ,
1220
+ 'D' : np .random .randn (24 ),
1221
+ 'E' : np .random .randn (24 )})
1222
+ result = pd .crosstab (index = [df ['A' ], df ['B' ]],
1223
+ columns = [df ['C' ]],
1224
+ margins = True ,
1225
+ aggfunc = np .size ,
1226
+ values = df ['D' ])
1227
+ expected_index = pd .MultiIndex (levels = [['All' , 'one' , 'three' , 'two' ],
1228
+ ['' , 'A' , 'B' , 'C' ]],
1229
+ labels = [[1 , 1 , 1 , 2 , 2 , 2 , 3 , 3 , 3 , 0 ],
1230
+ [1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 , 0 ]],
1231
+ names = ['A' , 'B' ])
1232
+ expected_column = pd .Index (['bar' , 'foo' , 'All' ],
1233
+ dtype = 'object' ,
1234
+ name = 'C' )
1235
+ expected_data = np .array ([[2. , 2. , 4. ],
1236
+ [2. , 2. , 4. ],
1237
+ [2. , 2. , 4. ],
1238
+ [2. , np .nan , 2. ],
1239
+ [np .nan , 2. , 2. ],
1240
+ [2. , np .nan , 2. ],
1241
+ [np .nan , 2. , 2. ],
1242
+ [2. , np .nan , 2. ],
1243
+ [np .nan , 2. , 2. ],
1244
+ [12. , 12. , 24. ]])
1245
+ expected = pd .DataFrame (expected_data ,
1246
+ index = expected_index ,
1247
+ columns = expected_column )
1248
+ tm .assert_frame_equal (result , expected )
1249
+
1215
1250
1216
1251
if __name__ == '__main__' :
1217
1252
import nose
0 commit comments