Skip to content

Commit 1725d24

Browse files
mroeschkejreback
authored andcommitted
TST: Test pivot with categorical data
closes #8860 closes #8731 closes #9534 Looks like the examples in these issues pass in the master branch. Added test to confirm. Author: Matt Roeschke <[email protected]> Closes #14807 from mroeschke/test_pivot_categoricals and squashes the following commits: b506083 [Matt Roeschke] TST: Test pivot with categorical data
1 parent 846e9e5 commit 1725d24

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

pandas/tools/tests/test_pivot.py

+69
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,53 @@ def test_categorical_margins(self):
854854
table = data.pivot_table('x', 'y', 'z', margins=True)
855855
tm.assert_frame_equal(table, expected)
856856

857+
def test_categorical_aggfunc(self):
858+
# GH 9534
859+
df = pd.DataFrame({"C1": ["A", "B", "C", "C"],
860+
"C2": ["a", "a", "b", "b"],
861+
"V": [1, 2, 3, 4]})
862+
df["C1"] = df["C1"].astype("category")
863+
result = df.pivot_table("V", index="C1", columns="C2", aggfunc="count")
864+
865+
expected_index = pd.CategoricalIndex(['A', 'B', 'C'],
866+
categories=['A', 'B', 'C'],
867+
ordered=False,
868+
name='C1')
869+
expected_columns = pd.Index(['a', 'b'], name='C2')
870+
expected_data = np.array([[1., np.nan],
871+
[1., np.nan],
872+
[np.nan, 2.]])
873+
expected = pd.DataFrame(expected_data,
874+
index=expected_index,
875+
columns=expected_columns)
876+
tm.assert_frame_equal(result, expected)
877+
878+
def test_categorical_pivot_index_ordering(self):
879+
# GH 8731
880+
df = pd.DataFrame({'Sales': [100, 120, 220],
881+
'Month': ['January', 'January', 'January'],
882+
'Year': [2013, 2014, 2013]})
883+
months = ['January', 'February', 'March', 'April', 'May', 'June',
884+
'July', 'August', 'September', 'October', 'November',
885+
'December']
886+
df['Month'] = df['Month'].astype('category').cat.set_categories(months)
887+
result = df.pivot_table(values='Sales',
888+
index='Month',
889+
columns='Year',
890+
aggfunc='sum')
891+
expected_columns = pd.Int64Index([2013, 2014], name='Year')
892+
expected_index = pd.CategoricalIndex(months,
893+
categories=months,
894+
ordered=False,
895+
name='Month')
896+
expected_data = np.empty((12, 2))
897+
expected_data.fill(np.nan)
898+
expected_data[0, :] = [320., 120.]
899+
expected = pd.DataFrame(expected_data,
900+
index=expected_index,
901+
columns=expected_columns)
902+
tm.assert_frame_equal(result, expected)
903+
857904

858905
class TestCrosstab(tm.TestCase):
859906

@@ -1212,6 +1259,28 @@ def test_crosstab_errors(self):
12121259
with tm.assertRaisesRegexp(ValueError, error):
12131260
pd.crosstab(df.a, df.b, normalize='all', margins=42)
12141261

1262+
def test_crosstab_with_categorial_columns(self):
1263+
# GH 8860
1264+
df = pd.DataFrame({'MAKE': ['Honda', 'Acura', 'Tesla',
1265+
'Honda', 'Honda', 'Acura'],
1266+
'MODEL': ['Sedan', 'Sedan', 'Electric',
1267+
'Pickup', 'Sedan', 'Sedan']})
1268+
categories = ['Sedan', 'Electric', 'Pickup']
1269+
df['MODEL'] = (df['MODEL'].astype('category')
1270+
.cat.set_categories(categories))
1271+
result = pd.crosstab(df['MAKE'], df['MODEL'])
1272+
1273+
expected_index = pd.Index(['Acura', 'Honda', 'Tesla'], name='MAKE')
1274+
expected_columns = pd.CategoricalIndex(categories,
1275+
categories=categories,
1276+
ordered=False,
1277+
name='MODEL')
1278+
expected_data = [[2, 0, 0], [2, 0, 1], [0, 1, 0]]
1279+
expected = pd.DataFrame(expected_data,
1280+
index=expected_index,
1281+
columns=expected_columns)
1282+
tm.assert_frame_equal(result, expected)
1283+
12151284

12161285
if __name__ == '__main__':
12171286
import nose

0 commit comments

Comments
 (0)