Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions pandas/tools/tests/test_pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,41 @@ def test_crosstab_with_categorial_columns(self):
columns=expected_columns)
tm.assert_frame_equal(result, expected)

def test_crosstab_with_numpy_size(self):
# GH 4003
df = pd.DataFrame({'A': ['one', 'one', 'two', 'three'] * 6,
'B': ['A', 'B', 'C'] * 8,
'C': ['foo', 'foo', 'foo', 'bar', 'bar', 'bar'] * 4,
'D': np.random.randn(24),
'E': np.random.randn(24)})
result = pd.crosstab(index=[df['A'], df['B']],
columns=[df['C']],
margins=True,
aggfunc=np.size,
values=df['D'])
expected_index = pd.MultiIndex(levels=[['All', 'one', 'three', 'two'],
['', 'A', 'B', 'C']],
labels=[[1, 1, 1, 2, 2, 2, 3, 3, 3, 0],
[1, 2, 3, 1, 2, 3, 1, 2, 3, 0]],
names=['A', 'B'])
expected_column = pd.Index(['bar', 'foo', 'All'],
dtype='object',
name='C')
expected_data = np.array([[2., 2., 4.],
[2., 2., 4.],
[2., 2., 4.],
[2., np.nan, 2.],
[np.nan, 2., 2.],
[2., np.nan, 2.],
[np.nan, 2., 2.],
[2., np.nan, 2.],
[np.nan, 2., 2.],
[12., 12., 24.]])
expected = pd.DataFrame(expected_data,
index=expected_index,
columns=expected_column)
tm.assert_frame_equal(result, expected)


if __name__ == '__main__':
import nose
Expand Down