|
| 1 | +# -*- coding: utf-8 -*- |
1 | 2 |
|
2 | 3 | from datetime import datetime, date, timedelta
|
3 | 4 |
|
|
16 | 17 | from pandas.api.types import CategoricalDtype as CDT
|
17 | 18 |
|
18 | 19 |
|
| 20 | +@pytest.fixture(params=[True, False]) |
| 21 | +def dropna(request): |
| 22 | + return request.param |
| 23 | + |
| 24 | + |
19 | 25 | class TestPivotTable(object):
|
20 | 26 |
|
21 | 27 | def setup_method(self, method):
|
@@ -109,7 +115,6 @@ def test_pivot_table_categorical(self):
|
109 | 115 | index=exp_index)
|
110 | 116 | tm.assert_frame_equal(result, expected)
|
111 | 117 |
|
112 |
| - @pytest.mark.parametrize('dropna', [True, False]) |
113 | 118 | def test_pivot_table_dropna_categoricals(self, dropna):
|
114 | 119 | # GH 15193
|
115 | 120 | categories = ['a', 'b', 'c', 'd']
|
@@ -137,6 +142,25 @@ def test_pivot_table_dropna_categoricals(self, dropna):
|
137 | 142 |
|
138 | 143 | tm.assert_frame_equal(result, expected)
|
139 | 144 |
|
| 145 | + def test_pivot_with_non_observable_dropna(self, dropna): |
| 146 | + # gh-21133 |
| 147 | + df = pd.DataFrame( |
| 148 | + {'A': pd.Categorical([np.nan, 'low', 'high', 'low', 'high'], |
| 149 | + categories=['low', 'high'], |
| 150 | + ordered=True), |
| 151 | + 'B': range(5)}) |
| 152 | + |
| 153 | + result = df.pivot_table(index='A', values='B', dropna=dropna) |
| 154 | + expected = pd.DataFrame( |
| 155 | + {'B': [2, 3]}, |
| 156 | + index=pd.Index( |
| 157 | + pd.Categorical.from_codes([0, 1], |
| 158 | + categories=['low', 'high'], |
| 159 | + ordered=True), |
| 160 | + name='A')) |
| 161 | + |
| 162 | + tm.assert_frame_equal(result, expected) |
| 163 | + |
140 | 164 | def test_pass_array(self):
|
141 | 165 | result = self.data.pivot_table(
|
142 | 166 | 'D', index=self.data.A, columns=self.data.C)
|
|
0 commit comments