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
40 changes: 20 additions & 20 deletions pandas/tools/tests/test_pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,26 @@ def test_pivot_table_margins_name_with_aggfunc_list(self):
expected = pd.DataFrame(table.values, index=ix, columns=cols)
tm.assert_frame_equal(table, expected)

def test_categorical_margins(self):
# GH 10989
df = pd.DataFrame({'x': np.arange(8),
'y': np.arange(8) // 4,
'z': np.arange(8) % 2})

expected = pd.DataFrame([[1.0, 2.0, 1.5], [5, 6, 5.5], [3, 4, 3.5]])
expected.index = Index([0, 1, 'All'], name='y')
expected.columns = Index([0, 1, 'All'], name='z')

data = df.copy()
table = data.pivot_table('x', 'y', 'z', margins=True)
tm.assert_frame_equal(table, expected)

data = df.copy()
data.y = data.y.astype('category')
data.z = data.z.astype('category')
table = data.pivot_table('x', 'y', 'z', margins=True)
tm.assert_frame_equal(table, expected)


class TestCrosstab(tm.TestCase):

Expand Down Expand Up @@ -919,26 +939,6 @@ def test_crosstab_dropna(self):
names=['b', 'c'])
tm.assert_index_equal(res.columns, m)

def test_categorical_margins(self):
# GH 10989
df = pd.DataFrame({'x': np.arange(8),
'y': np.arange(8) // 4,
'z': np.arange(8) % 2})

expected = pd.DataFrame([[1.0, 2.0, 1.5], [5, 6, 5.5], [3, 4, 3.5]])
expected.index = Index([0, 1, 'All'], name='y')
expected.columns = Index([0, 1, 'All'], name='z')

data = df.copy()
table = data.pivot_table('x', 'y', 'z', margins=True)
tm.assert_frame_equal(table, expected)

data = df.copy()
data.y = data.y.astype('category')
data.z = data.z.astype('category')
table = data.pivot_table('x', 'y', 'z', margins=True)
tm.assert_frame_equal(table, expected)

def test_crosstab_no_overlap(self):
# GS 10291

Expand Down