|
3 | 3 | import numpy as np
|
4 | 4 | import pytest
|
5 | 5 |
|
| 6 | +import pandas as pd |
6 | 7 | from pandas import Categorical, CategoricalIndex, Index, PeriodIndex, Series
|
7 | 8 | import pandas.core.common as com
|
8 | 9 | from pandas.tests.arrays.categorical.common import TestCategorical
|
@@ -43,6 +44,45 @@ def test_setitem(self):
|
43 | 44 |
|
44 | 45 | tm.assert_categorical_equal(c, expected)
|
45 | 46 |
|
| 47 | + @pytest.mark.parametrize('other', [ |
| 48 | + pd.Categorical(['b', 'a']), |
| 49 | + pd.Categorical(['b', 'a'], categories=['b', 'a']), |
| 50 | + ]) |
| 51 | + def test_setitem_same_but_unordered(self, other): |
| 52 | + # GH-24142 |
| 53 | + target = pd.Categorical(['a', 'b'], categories=['a', 'b']) |
| 54 | + mask = np.array([True, False]) |
| 55 | + target[mask] = other[mask] |
| 56 | + expected = pd.Categorical(['b', 'b'], categories=['a', 'b']) |
| 57 | + tm.assert_categorical_equal(target, expected) |
| 58 | + |
| 59 | + @pytest.mark.parametrize('other', [ |
| 60 | + pd.Categorical(['b', 'a'], categories=['b', 'a', 'c']), |
| 61 | + pd.Categorical(['b', 'a'], categories=['a', 'b', 'c']), |
| 62 | + pd.Categorical(['a', 'a'], categories=['a']), |
| 63 | + pd.Categorical(['b', 'b'], categories=['b']), |
| 64 | + ]) |
| 65 | + def test_setitem_different_unordered_raises(self, other): |
| 66 | + # GH-24142 |
| 67 | + target = pd.Categorical(['a', 'b'], categories=['a', 'b']) |
| 68 | + mask = np.array([True, False]) |
| 69 | + with pytest.raises(ValueError): |
| 70 | + target[mask] = other[mask] |
| 71 | + |
| 72 | + @pytest.mark.parametrize('other', [ |
| 73 | + pd.Categorical(['b', 'a']), |
| 74 | + pd.Categorical(['b', 'a'], categories=['b', 'a'], ordered=True), |
| 75 | + pd.Categorical(['b', 'a'], categories=['a', 'b', 'c'], ordered=True), |
| 76 | + ]) |
| 77 | + def test_setitem_same_ordered_rasies(self, other): |
| 78 | + # Gh-24142 |
| 79 | + target = pd.Categorical(['a', 'b'], categories=['a', 'b'], |
| 80 | + ordered=True) |
| 81 | + mask = np.array([True, False]) |
| 82 | + |
| 83 | + with pytest.raises(ValueError): |
| 84 | + target[mask] = other[mask] |
| 85 | + |
46 | 86 |
|
47 | 87 | class TestCategoricalIndexing(object):
|
48 | 88 |
|
@@ -122,37 +162,59 @@ def test_get_indexer_non_unique(self, idx_values, key_values, key_class):
|
122 | 162 | tm.assert_numpy_array_equal(expected, result)
|
123 | 163 | tm.assert_numpy_array_equal(exp_miss, res_miss)
|
124 | 164 |
|
| 165 | + def test_where_unobserved_nan(self): |
| 166 | + ser = pd.Series(pd.Categorical(['a', 'b'])) |
| 167 | + result = ser.where([True, False]) |
| 168 | + expected = pd.Series(pd.Categorical(['a', None], |
| 169 | + categories=['a', 'b'])) |
| 170 | + tm.assert_series_equal(result, expected) |
| 171 | + |
| 172 | + # all NA |
| 173 | + ser = pd.Series(pd.Categorical(['a', 'b'])) |
| 174 | + result = ser.where([False, False]) |
| 175 | + expected = pd.Series(pd.Categorical([None, None], |
| 176 | + categories=['a', 'b'])) |
| 177 | + tm.assert_series_equal(result, expected) |
| 178 | + |
125 | 179 | def test_where_unobserved_categories(self):
|
126 |
| - arr = Categorical(['a', 'b', 'c'], categories=['d', 'c', 'b', 'a']) |
127 |
| - result = arr.where([True, True, False], other='b') |
128 |
| - expected = Categorical(['a', 'b', 'b'], categories=arr.categories) |
129 |
| - tm.assert_categorical_equal(result, expected) |
| 180 | + ser = pd.Series( |
| 181 | + Categorical(['a', 'b', 'c'], categories=['d', 'c', 'b', 'a']) |
| 182 | + ) |
| 183 | + result = ser.where([True, True, False], other='b') |
| 184 | + expected = pd.Series( |
| 185 | + Categorical(['a', 'b', 'b'], categories=ser.cat.categories) |
| 186 | + ) |
| 187 | + tm.assert_series_equal(result, expected) |
130 | 188 |
|
131 | 189 | def test_where_other_categorical(self):
|
132 |
| - arr = Categorical(['a', 'b', 'c'], categories=['d', 'c', 'b', 'a']) |
| 190 | + ser = pd.Series( |
| 191 | + Categorical(['a', 'b', 'c'], categories=['d', 'c', 'b', 'a']) |
| 192 | + ) |
133 | 193 | other = Categorical(['b', 'c', 'a'], categories=['a', 'c', 'b', 'd'])
|
134 |
| - result = arr.where([True, False, True], other) |
135 |
| - expected = Categorical(['a', 'c', 'c'], dtype=arr.dtype) |
136 |
| - tm.assert_categorical_equal(result, expected) |
| 194 | + result = ser.where([True, False, True], other) |
| 195 | + expected = pd.Series(Categorical(['a', 'c', 'c'], dtype=ser.dtype)) |
| 196 | + tm.assert_series_equal(result, expected) |
137 | 197 |
|
138 | 198 | def test_where_warns(self):
|
139 |
| - arr = Categorical(['a', 'b', 'c']) |
| 199 | + ser = pd.Series(Categorical(['a', 'b', 'c'])) |
140 | 200 | with tm.assert_produces_warning(FutureWarning):
|
141 |
| - result = arr.where([True, False, True], 'd') |
| 201 | + result = ser.where([True, False, True], 'd') |
142 | 202 |
|
143 |
| - expected = np.array(['a', 'd', 'c'], dtype='object') |
144 |
| - tm.assert_numpy_array_equal(result, expected) |
| 203 | + expected = pd.Series(np.array(['a', 'd', 'c'], dtype='object')) |
| 204 | + tm.assert_series_equal(result, expected) |
145 | 205 |
|
146 | 206 | def test_where_ordered_differs_rasies(self):
|
147 |
| - arr = Categorical(['a', 'b', 'c'], categories=['d', 'c', 'b', 'a'], |
148 |
| - ordered=True) |
| 207 | + ser = pd.Series( |
| 208 | + Categorical(['a', 'b', 'c'], categories=['d', 'c', 'b', 'a'], |
| 209 | + ordered=True) |
| 210 | + ) |
149 | 211 | other = Categorical(['b', 'c', 'a'], categories=['a', 'c', 'b', 'd'],
|
150 | 212 | ordered=True)
|
151 | 213 | with tm.assert_produces_warning(FutureWarning):
|
152 |
| - result = arr.where([True, False, True], other) |
| 214 | + result = ser.where([True, False, True], other) |
153 | 215 |
|
154 |
| - expected = np.array(['a', 'c', 'c'], dtype=object) |
155 |
| - tm.assert_numpy_array_equal(result, expected) |
| 216 | + expected = pd.Series(np.array(['a', 'c', 'c'], dtype=object)) |
| 217 | + tm.assert_series_equal(result, expected) |
156 | 218 |
|
157 | 219 |
|
158 | 220 | @pytest.mark.parametrize("index", [True, False])
|
|
0 commit comments