Skip to content

Commit 9c539e4

Browse files
committed
TST: fix up pivot tests
1 parent 221bee8 commit 9c539e4

File tree

2 files changed

+34
-35
lines changed

2 files changed

+34
-35
lines changed

doc/source/whatsnew/v0.23.0.txt

+14
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,20 @@ New Behavior:
531531

532532
df.groupby(['A', 'B', 'C']).count()
533533

534+
Furthermore, the result of pivotting will now only show observed values. ``dropna`` will have no
535+
effect for a Categorical pivotted column.
536+
537+
.. ipython:: python
538+
539+
categories = ['a', 'b', 'c', 'd']
540+
from pandas.api.types import CategoricalDtype as CDT
541+
df = DataFrame({'A': ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c'],
542+
'B': [1, 2, 3, 1, 2, 3, 1, 2, 3],
543+
'C': range(0, 9)})
544+
545+
df['A'] = df['A'].astype(CDT(categories, ordered=False))
546+
df.pivot_table(index='B', columns='A', values='C')
547+
534548
.. _whatsnew_0230.api_breaking.deprecate_panel:
535549

536550
Deprecate Panel

pandas/tests/reshape/test_pivot.py

+20-35
Original file line numberDiff line numberDiff line change
@@ -93,23 +93,23 @@ def test_pivot_table_dropna(self):
9393

9494
def test_pivot_table_categorical(self):
9595

96-
raw_cat1 = Categorical(["a", "a", "b", "b"],
97-
categories=["a", "b", "z"], ordered=True)
98-
raw_cat2 = Categorical(["c", "d", "c", "d"],
99-
categories=["c", "d", "y"], ordered=True)
100-
df = DataFrame({"A": raw_cat1, "B": raw_cat2, "values": [1, 2, 3, 4]})
96+
cat1 = Categorical(["a", "a", "b", "b"],
97+
categories=["a", "b", "z"], ordered=True)
98+
cat2 = Categorical(["c", "d", "c", "d"],
99+
categories=["c", "d", "y"], ordered=True)
100+
df = DataFrame({"A": cat1, "B": cat2, "values": [1, 2, 3, 4]})
101101
result = pd.pivot_table(df, values='values', index=['A', 'B'])
102102

103-
exp_index = pd.MultiIndex.from_product(
104-
[Categorical(["a", "b", "z"], ordered=True),
105-
Categorical(["c", "d", "y"], ordered=True)],
103+
exp_index = pd.MultiIndex.from_arrays(
104+
[cat1, cat2],
106105
names=['A', 'B'])
107106
expected = DataFrame(
108-
{'values': [1, 2, np.nan, 3, 4, np.nan, np.nan, np.nan, np.nan]},
107+
{'values': [1, 2, 3, 4]},
109108
index=exp_index)
110109
tm.assert_frame_equal(result, expected)
111110

112-
def test_pivot_table_dropna_categoricals(self):
111+
@pytest.mark.parametrize('dropna', [True, False])
112+
def test_pivot_table_dropna_categoricals(self, dropna):
113113
# GH 15193
114114
categories = ['a', 'b', 'c', 'd']
115115

@@ -118,30 +118,18 @@ def test_pivot_table_dropna_categoricals(self):
118118
'C': range(0, 9)})
119119

120120
df['A'] = df['A'].astype(CDT(categories, ordered=False))
121-
result_true = df.pivot_table(index='B', columns='A', values='C',
122-
dropna=True)
121+
result = df.pivot_table(index='B', columns='A', values='C',
122+
dropna=dropna)
123123
expected_columns = Series(['a', 'b', 'c'], name='A')
124124
expected_columns = expected_columns.astype(
125125
CDT(categories, ordered=False))
126126
expected_index = Series([1, 2, 3], name='B')
127-
expected_true = DataFrame([[0.0, 3.0, 6.0],
128-
[1.0, 4.0, 7.0],
129-
[2.0, 5.0, 8.0]],
130-
index=expected_index,
131-
columns=expected_columns,)
132-
tm.assert_frame_equal(expected_true, result_true)
133-
134-
result_false = df.pivot_table(index='B', columns='A', values='C',
135-
dropna=False)
136-
expected_columns = (
137-
Series(['a', 'b', 'c', 'd'], name='A').astype('category')
138-
)
139-
expected_false = DataFrame([[0.0, 3.0, 6.0, np.NaN],
140-
[1.0, 4.0, 7.0, np.NaN],
141-
[2.0, 5.0, 8.0, np.NaN]],
142-
index=expected_index,
143-
columns=expected_columns,)
144-
tm.assert_frame_equal(expected_false, result_false)
127+
expected = DataFrame([[0, 3, 6],
128+
[1, 4, 7],
129+
[2, 5, 8]],
130+
index=expected_index,
131+
columns=expected_columns,)
132+
tm.assert_frame_equal(result, expected)
145133

146134
def test_pass_array(self):
147135
result = self.data.pivot_table(
@@ -1132,14 +1120,11 @@ def test_categorical_pivot_index_ordering(self):
11321120
columns='Year',
11331121
aggfunc='sum')
11341122
expected_columns = pd.Int64Index([2013, 2014], name='Year')
1135-
expected_index = pd.CategoricalIndex(months,
1123+
expected_index = pd.CategoricalIndex(['January'],
11361124
categories=months,
11371125
ordered=False,
11381126
name='Month')
1139-
expected_data = np.empty((12, 2))
1140-
expected_data.fill(np.nan)
1141-
expected_data[0, :] = [320., 120.]
1142-
expected = pd.DataFrame(expected_data,
1127+
expected = pd.DataFrame([[320, 120]],
11431128
index=expected_index,
11441129
columns=expected_columns)
11451130
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)