|
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,6 +162,60 @@ 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 | + |
| 179 | + def test_where_unobserved_categories(self): |
| 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) |
| 188 | + |
| 189 | + def test_where_other_categorical(self): |
| 190 | + ser = pd.Series( |
| 191 | + Categorical(['a', 'b', 'c'], categories=['d', 'c', 'b', 'a']) |
| 192 | + ) |
| 193 | + other = Categorical(['b', 'c', 'a'], categories=['a', 'c', 'b', 'd']) |
| 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) |
| 197 | + |
| 198 | + def test_where_warns(self): |
| 199 | + ser = pd.Series(Categorical(['a', 'b', 'c'])) |
| 200 | + with tm.assert_produces_warning(FutureWarning): |
| 201 | + result = ser.where([True, False, True], 'd') |
| 202 | + |
| 203 | + expected = pd.Series(np.array(['a', 'd', 'c'], dtype='object')) |
| 204 | + tm.assert_series_equal(result, expected) |
| 205 | + |
| 206 | + def test_where_ordered_differs_rasies(self): |
| 207 | + ser = pd.Series( |
| 208 | + Categorical(['a', 'b', 'c'], categories=['d', 'c', 'b', 'a'], |
| 209 | + ordered=True) |
| 210 | + ) |
| 211 | + other = Categorical(['b', 'c', 'a'], categories=['a', 'c', 'b', 'd'], |
| 212 | + ordered=True) |
| 213 | + with tm.assert_produces_warning(FutureWarning): |
| 214 | + result = ser.where([True, False, True], other) |
| 215 | + |
| 216 | + expected = pd.Series(np.array(['a', 'c', 'c'], dtype=object)) |
| 217 | + tm.assert_series_equal(result, expected) |
| 218 | + |
125 | 219 |
|
126 | 220 | @pytest.mark.parametrize("index", [True, False])
|
127 | 221 | def test_mask_with_boolean(index):
|
|
0 commit comments