@@ -28,81 +28,6 @@ def test_can_hold_identifiers(self):
28
28
key = idx [0 ]
29
29
assert idx ._can_hold_identifiers_and_holds_name (key ) is True
30
30
31
- @pytest .mark .parametrize (
32
- "func,op_name" ,
33
- [
34
- (lambda idx : idx - idx , "__sub__" ),
35
- (lambda idx : idx + idx , "__add__" ),
36
- (lambda idx : idx - ["a" , "b" ], "__sub__" ),
37
- (lambda idx : idx + ["a" , "b" ], "__add__" ),
38
- (lambda idx : ["a" , "b" ] - idx , "__rsub__" ),
39
- (lambda idx : ["a" , "b" ] + idx , "__radd__" ),
40
- ],
41
- )
42
- def test_disallow_addsub_ops (self , func , op_name ):
43
- # GH 10039
44
- # set ops (+/-) raise TypeError
45
- idx = Index (Categorical (["a" , "b" ]))
46
- cat_or_list = "'(Categorical|list)' and '(Categorical|list)'"
47
- msg = "|" .join (
48
- [
49
- f"cannot perform { op_name } with this index type: CategoricalIndex" ,
50
- "can only concatenate list" ,
51
- rf"unsupported operand type\(s\) for [\+-]: { cat_or_list } " ,
52
- ]
53
- )
54
- with pytest .raises (TypeError , match = msg ):
55
- func (idx )
56
-
57
- def test_method_delegation (self ):
58
-
59
- ci = CategoricalIndex (list ("aabbca" ), categories = list ("cabdef" ))
60
- result = ci .set_categories (list ("cab" ))
61
- tm .assert_index_equal (
62
- result , CategoricalIndex (list ("aabbca" ), categories = list ("cab" ))
63
- )
64
-
65
- ci = CategoricalIndex (list ("aabbca" ), categories = list ("cab" ))
66
- result = ci .rename_categories (list ("efg" ))
67
- tm .assert_index_equal (
68
- result , CategoricalIndex (list ("ffggef" ), categories = list ("efg" ))
69
- )
70
-
71
- # GH18862 (let rename_categories take callables)
72
- result = ci .rename_categories (lambda x : x .upper ())
73
- tm .assert_index_equal (
74
- result , CategoricalIndex (list ("AABBCA" ), categories = list ("CAB" ))
75
- )
76
-
77
- ci = CategoricalIndex (list ("aabbca" ), categories = list ("cab" ))
78
- result = ci .add_categories (["d" ])
79
- tm .assert_index_equal (
80
- result , CategoricalIndex (list ("aabbca" ), categories = list ("cabd" ))
81
- )
82
-
83
- ci = CategoricalIndex (list ("aabbca" ), categories = list ("cab" ))
84
- result = ci .remove_categories (["c" ])
85
- tm .assert_index_equal (
86
- result ,
87
- CategoricalIndex (list ("aabb" ) + [np .nan ] + ["a" ], categories = list ("ab" )),
88
- )
89
-
90
- ci = CategoricalIndex (list ("aabbca" ), categories = list ("cabdef" ))
91
- result = ci .as_unordered ()
92
- tm .assert_index_equal (result , ci )
93
-
94
- ci = CategoricalIndex (list ("aabbca" ), categories = list ("cabdef" ))
95
- result = ci .as_ordered ()
96
- tm .assert_index_equal (
97
- result ,
98
- CategoricalIndex (list ("aabbca" ), categories = list ("cabdef" ), ordered = True ),
99
- )
100
-
101
- # invalid
102
- msg = "cannot use inplace with CategoricalIndex"
103
- with pytest .raises (ValueError , match = msg ):
104
- ci .set_categories (list ("cab" ), inplace = True )
105
-
106
31
def test_append (self ):
107
32
108
33
ci = self .create_index ()
@@ -387,6 +312,24 @@ def test_frame_repr(self):
387
312
expected = " A\n a 1\n b 2\n c 3"
388
313
assert result == expected
389
314
315
+ def test_reindex_base (self ):
316
+ # See test_reindex.py
317
+ pass
318
+
319
+ def test_map_str (self ):
320
+ # See test_map.py
321
+ pass
322
+
323
+
324
+ class TestCategoricalIndex2 :
325
+ # Tests that are not overriding a test in Base
326
+
327
+ def test_format_different_scalar_lengths (self ):
328
+ # GH35439
329
+ idx = CategoricalIndex (["aaaaaaaaa" , "b" ])
330
+ expected = ["aaaaaaaaa" , "b" ]
331
+ assert idx .format () == expected
332
+
390
333
@pytest .mark .parametrize (
391
334
"dtype, engine_type" ,
392
335
[
@@ -410,16 +353,77 @@ def test_engine_type(self, dtype, engine_type):
410
353
assert np .issubdtype (ci .codes .dtype , dtype )
411
354
assert isinstance (ci ._engine , engine_type )
412
355
413
- def test_reindex_base (self ):
414
- # See test_reindex.py
415
- pass
356
+ @pytest .mark .parametrize (
357
+ "func,op_name" ,
358
+ [
359
+ (lambda idx : idx - idx , "__sub__" ),
360
+ (lambda idx : idx + idx , "__add__" ),
361
+ (lambda idx : idx - ["a" , "b" ], "__sub__" ),
362
+ (lambda idx : idx + ["a" , "b" ], "__add__" ),
363
+ (lambda idx : ["a" , "b" ] - idx , "__rsub__" ),
364
+ (lambda idx : ["a" , "b" ] + idx , "__radd__" ),
365
+ ],
366
+ )
367
+ def test_disallow_addsub_ops (self , func , op_name ):
368
+ # GH 10039
369
+ # set ops (+/-) raise TypeError
370
+ idx = Index (Categorical (["a" , "b" ]))
371
+ cat_or_list = "'(Categorical|list)' and '(Categorical|list)'"
372
+ msg = "|" .join (
373
+ [
374
+ f"cannot perform { op_name } with this index type: CategoricalIndex" ,
375
+ "can only concatenate list" ,
376
+ rf"unsupported operand type\(s\) for [\+-]: { cat_or_list } " ,
377
+ ]
378
+ )
379
+ with pytest .raises (TypeError , match = msg ):
380
+ func (idx )
416
381
417
- def test_map_str (self ):
418
- # See test_map.py
419
- pass
382
+ def test_method_delegation (self ):
420
383
421
- def test_format_different_scalar_lengths (self ):
422
- # GH35439
423
- idx = CategoricalIndex (["aaaaaaaaa" , "b" ])
424
- expected = ["aaaaaaaaa" , "b" ]
425
- assert idx .format () == expected
384
+ ci = CategoricalIndex (list ("aabbca" ), categories = list ("cabdef" ))
385
+ result = ci .set_categories (list ("cab" ))
386
+ tm .assert_index_equal (
387
+ result , CategoricalIndex (list ("aabbca" ), categories = list ("cab" ))
388
+ )
389
+
390
+ ci = CategoricalIndex (list ("aabbca" ), categories = list ("cab" ))
391
+ result = ci .rename_categories (list ("efg" ))
392
+ tm .assert_index_equal (
393
+ result , CategoricalIndex (list ("ffggef" ), categories = list ("efg" ))
394
+ )
395
+
396
+ # GH18862 (let rename_categories take callables)
397
+ result = ci .rename_categories (lambda x : x .upper ())
398
+ tm .assert_index_equal (
399
+ result , CategoricalIndex (list ("AABBCA" ), categories = list ("CAB" ))
400
+ )
401
+
402
+ ci = CategoricalIndex (list ("aabbca" ), categories = list ("cab" ))
403
+ result = ci .add_categories (["d" ])
404
+ tm .assert_index_equal (
405
+ result , CategoricalIndex (list ("aabbca" ), categories = list ("cabd" ))
406
+ )
407
+
408
+ ci = CategoricalIndex (list ("aabbca" ), categories = list ("cab" ))
409
+ result = ci .remove_categories (["c" ])
410
+ tm .assert_index_equal (
411
+ result ,
412
+ CategoricalIndex (list ("aabb" ) + [np .nan ] + ["a" ], categories = list ("ab" )),
413
+ )
414
+
415
+ ci = CategoricalIndex (list ("aabbca" ), categories = list ("cabdef" ))
416
+ result = ci .as_unordered ()
417
+ tm .assert_index_equal (result , ci )
418
+
419
+ ci = CategoricalIndex (list ("aabbca" ), categories = list ("cabdef" ))
420
+ result = ci .as_ordered ()
421
+ tm .assert_index_equal (
422
+ result ,
423
+ CategoricalIndex (list ("aabbca" ), categories = list ("cabdef" ), ordered = True ),
424
+ )
425
+
426
+ # invalid
427
+ msg = "cannot use inplace with CategoricalIndex"
428
+ with pytest .raises (ValueError , match = msg ):
429
+ ci .set_categories (list ("cab" ), inplace = True )
0 commit comments