|
8 | 8 |
|
9 | 9 | from pandas import (
|
10 | 10 | Categorical,
|
| 11 | + CategoricalDtype, |
11 | 12 | Index,
|
12 | 13 | NaT,
|
13 | 14 | Series,
|
@@ -196,84 +197,49 @@ def test_searchsorted(self, ordered):
|
196 | 197 | with pytest.raises(KeyError, match="cucumber"):
|
197 | 198 | ser.searchsorted(["bread", "cucumber"])
|
198 | 199 |
|
199 |
| - def test_unique(self): |
| 200 | + def test_unique(self, ordered): |
| 201 | + # GH38140 |
| 202 | + dtype = CategoricalDtype(["a", "b", "c"], ordered=ordered) |
| 203 | + |
200 | 204 | # categories are reordered based on value when ordered=False
|
201 |
| - cat = Categorical(["a", "b"]) |
202 |
| - exp = Index(["a", "b"]) |
| 205 | + cat = Categorical(["a", "b", "c"], dtype=dtype) |
203 | 206 | res = cat.unique()
|
204 |
| - tm.assert_index_equal(res.categories, exp) |
205 | 207 | tm.assert_categorical_equal(res, cat)
|
206 | 208 |
|
207 |
| - cat = Categorical(["a", "b", "a", "a"], categories=["a", "b", "c"]) |
| 209 | + cat = Categorical(["a", "b", "a", "a"], dtype=dtype) |
208 | 210 | res = cat.unique()
|
209 |
| - tm.assert_index_equal(res.categories, exp) |
210 |
| - tm.assert_categorical_equal(res, Categorical(exp)) |
| 211 | + tm.assert_categorical_equal(res, Categorical(["a", "b"], dtype=dtype)) |
211 | 212 |
|
212 |
| - cat = Categorical(["c", "a", "b", "a", "a"], categories=["a", "b", "c"]) |
213 |
| - exp = Index(["c", "a", "b"]) |
| 213 | + cat = Categorical(["c", "a", "b", "a", "a"], dtype=dtype) |
214 | 214 | res = cat.unique()
|
215 |
| - tm.assert_index_equal(res.categories, exp) |
216 |
| - exp_cat = Categorical(exp, categories=["c", "a", "b"]) |
| 215 | + exp_cat = Categorical(["c", "a", "b"], dtype=dtype) |
217 | 216 | tm.assert_categorical_equal(res, exp_cat)
|
218 | 217 |
|
219 | 218 | # nan must be removed
|
220 |
| - cat = Categorical(["b", np.nan, "b", np.nan, "a"], categories=["a", "b", "c"]) |
221 |
| - res = cat.unique() |
222 |
| - exp = Index(["b", "a"]) |
223 |
| - tm.assert_index_equal(res.categories, exp) |
224 |
| - exp_cat = Categorical(["b", np.nan, "a"], categories=["b", "a"]) |
225 |
| - tm.assert_categorical_equal(res, exp_cat) |
226 |
| - |
227 |
| - def test_unique_ordered(self): |
228 |
| - # keep categories order when ordered=True |
229 |
| - cat = Categorical(["b", "a", "b"], categories=["a", "b"], ordered=True) |
| 219 | + cat = Categorical(["b", np.nan, "b", np.nan, "a"], dtype=dtype) |
230 | 220 | res = cat.unique()
|
231 |
| - exp_cat = Categorical(["b", "a"], categories=["a", "b"], ordered=True) |
| 221 | + exp_cat = Categorical(["b", np.nan, "a"], dtype=dtype) |
232 | 222 | tm.assert_categorical_equal(res, exp_cat)
|
233 | 223 |
|
234 |
| - cat = Categorical( |
235 |
| - ["c", "b", "a", "a"], categories=["a", "b", "c"], ordered=True |
236 |
| - ) |
237 |
| - res = cat.unique() |
238 |
| - exp_cat = Categorical(["c", "b", "a"], categories=["a", "b", "c"], ordered=True) |
239 |
| - tm.assert_categorical_equal(res, exp_cat) |
240 |
| - |
241 |
| - cat = Categorical(["b", "a", "a"], categories=["a", "b", "c"], ordered=True) |
242 |
| - res = cat.unique() |
243 |
| - exp_cat = Categorical(["b", "a"], categories=["a", "b"], ordered=True) |
244 |
| - tm.assert_categorical_equal(res, exp_cat) |
| 224 | + def test_unique_index_series(self, ordered): |
| 225 | + # GH38140 |
| 226 | + dtype = CategoricalDtype([3, 2, 1], ordered=ordered) |
245 | 227 |
|
246 |
| - cat = Categorical( |
247 |
| - ["b", "b", np.nan, "a"], categories=["a", "b", "c"], ordered=True |
248 |
| - ) |
249 |
| - res = cat.unique() |
250 |
| - exp_cat = Categorical(["b", np.nan, "a"], categories=["a", "b"], ordered=True) |
251 |
| - tm.assert_categorical_equal(res, exp_cat) |
252 |
| - |
253 |
| - def test_unique_index_series(self): |
254 |
| - c = Categorical([3, 1, 2, 2, 1], categories=[3, 2, 1]) |
| 228 | + c = Categorical([3, 1, 2, 2, 1], dtype=dtype) |
255 | 229 | # Categorical.unique sorts categories by appearance order
|
256 | 230 | # if ordered=False
|
257 |
| - exp = Categorical([3, 1, 2], categories=[3, 1, 2]) |
| 231 | + exp = Categorical([3, 1, 2], dtype=dtype) |
258 | 232 | tm.assert_categorical_equal(c.unique(), exp)
|
259 | 233 |
|
260 | 234 | tm.assert_index_equal(Index(c).unique(), Index(exp))
|
261 | 235 | tm.assert_categorical_equal(Series(c).unique(), exp)
|
262 | 236 |
|
263 |
| - c = Categorical([1, 1, 2, 2], categories=[3, 2, 1]) |
264 |
| - exp = Categorical([1, 2], categories=[1, 2]) |
| 237 | + c = Categorical([1, 1, 2, 2], dtype=dtype) |
| 238 | + exp = Categorical([1, 2], dtype=dtype) |
265 | 239 | tm.assert_categorical_equal(c.unique(), exp)
|
266 | 240 | tm.assert_index_equal(Index(c).unique(), Index(exp))
|
267 | 241 | tm.assert_categorical_equal(Series(c).unique(), exp)
|
268 | 242 |
|
269 |
| - c = Categorical([3, 1, 2, 2, 1], categories=[3, 2, 1], ordered=True) |
270 |
| - # Categorical.unique keeps categories order if ordered=True |
271 |
| - exp = Categorical([3, 1, 2], categories=[3, 2, 1], ordered=True) |
272 |
| - tm.assert_categorical_equal(c.unique(), exp) |
273 |
| - |
274 |
| - tm.assert_index_equal(Index(c).unique(), Index(exp)) |
275 |
| - tm.assert_categorical_equal(Series(c).unique(), exp) |
276 |
| - |
277 | 243 | def test_shift(self):
|
278 | 244 | # GH 9416
|
279 | 245 | cat = Categorical(["a", "b", "c", "d", "a"])
|
|
0 commit comments