Skip to content

Commit b6de920

Browse files
mroeschkejreback
authored andcommitted
TST: Correct results with np.size and crosstab (#4003) (#14755)
missing assert
1 parent bca7be9 commit b6de920

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

pandas/tools/tests/test_pivot.py

+35
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,41 @@ def test_crosstab_with_categorial_columns(self):
12811281
columns=expected_columns)
12821282
tm.assert_frame_equal(result, expected)
12831283

1284+
def test_crosstab_with_numpy_size(self):
1285+
# GH 4003
1286+
df = pd.DataFrame({'A': ['one', 'one', 'two', 'three'] * 6,
1287+
'B': ['A', 'B', 'C'] * 8,
1288+
'C': ['foo', 'foo', 'foo', 'bar', 'bar', 'bar'] * 4,
1289+
'D': np.random.randn(24),
1290+
'E': np.random.randn(24)})
1291+
result = pd.crosstab(index=[df['A'], df['B']],
1292+
columns=[df['C']],
1293+
margins=True,
1294+
aggfunc=np.size,
1295+
values=df['D'])
1296+
expected_index = pd.MultiIndex(levels=[['All', 'one', 'three', 'two'],
1297+
['', 'A', 'B', 'C']],
1298+
labels=[[1, 1, 1, 2, 2, 2, 3, 3, 3, 0],
1299+
[1, 2, 3, 1, 2, 3, 1, 2, 3, 0]],
1300+
names=['A', 'B'])
1301+
expected_column = pd.Index(['bar', 'foo', 'All'],
1302+
dtype='object',
1303+
name='C')
1304+
expected_data = np.array([[2., 2., 4.],
1305+
[2., 2., 4.],
1306+
[2., 2., 4.],
1307+
[2., np.nan, 2.],
1308+
[np.nan, 2., 2.],
1309+
[2., np.nan, 2.],
1310+
[np.nan, 2., 2.],
1311+
[2., np.nan, 2.],
1312+
[np.nan, 2., 2.],
1313+
[12., 12., 24.]])
1314+
expected = pd.DataFrame(expected_data,
1315+
index=expected_index,
1316+
columns=expected_column)
1317+
tm.assert_frame_equal(result, expected)
1318+
12841319

12851320
if __name__ == '__main__':
12861321
import nose

0 commit comments

Comments
 (0)