Skip to content

Commit ebac1fd

Browse files
committed
TST: Correct results with np.size and crosstab (pandas-dev#4003)
1 parent 837db72 commit ebac1fd

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
@@ -1212,6 +1212,41 @@ def test_crosstab_errors(self):
12121212
with tm.assertRaisesRegexp(ValueError, error):
12131213
pd.crosstab(df.a, df.b, normalize='all', margins=42)
12141214

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+
12151250

12161251
if __name__ == '__main__':
12171252
import nose

0 commit comments

Comments
 (0)