Skip to content

Commit 7ae1072

Browse files
committed
TST: Correct results with np.size and crosstab (#4003)
1 parent e991141 commit 7ae1072

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

pandas/tools/tests/test_pivot.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,40 @@ def test_crosstab_with_categorial_columns(self):
12791279
expected = pd.DataFrame(expected_data,
12801280
index=expected_index,
12811281
columns=expected_columns)
1282+
1283+
def test_crosstab_with_numpy_size(self):
1284+
# GH 4003
1285+
df = pd.DataFrame({'A': ['one', 'one', 'two', 'three'] * 6,
1286+
'B': ['A', 'B', 'C'] * 8,
1287+
'C': ['foo', 'foo', 'foo', 'bar', 'bar', 'bar'] * 4,
1288+
'D': np.random.randn(24),
1289+
'E': np.random.randn(24)})
1290+
result = pd.crosstab(index=[df['A'], df['B']],
1291+
columns=[df['C']],
1292+
margins=True,
1293+
aggfunc=np.size,
1294+
values=df['D'])
1295+
expected_index = pd.MultiIndex(levels=[['All', 'one', 'three', 'two'],
1296+
['', 'A', 'B', 'C']],
1297+
labels=[[1, 1, 1, 2, 2, 2, 3, 3, 3, 0],
1298+
[1, 2, 3, 1, 2, 3, 1, 2, 3, 0]],
1299+
names=['A', 'B'])
1300+
expected_column = pd.Index(['bar', 'foo', 'All'],
1301+
dtype='object',
1302+
name='C')
1303+
expected_data = np.array([[2., 2., 4.],
1304+
[2., 2., 4.],
1305+
[2., 2., 4.],
1306+
[2., np.nan, 2.],
1307+
[np.nan, 2., 2.],
1308+
[2., np.nan, 2.],
1309+
[np.nan, 2., 2.],
1310+
[2., np.nan, 2.],
1311+
[np.nan, 2., 2.],
1312+
[12., 12., 24.]])
1313+
expected = pd.DataFrame(expected_data,
1314+
index=expected_index,
1315+
columns=expected_column)
12821316
tm.assert_frame_equal(result, expected)
12831317

12841318

0 commit comments

Comments
 (0)