1
1
import numpy as np
2
+ import pytest
3
+
4
+ from pandas ._config import using_pyarrow_string_dtype
2
5
3
6
from pandas import (
4
7
Categorical ,
5
8
CategoricalDtype ,
6
9
CategoricalIndex ,
10
+ Index ,
7
11
Series ,
8
12
date_range ,
9
13
option_context ,
13
17
14
18
15
19
class TestCategoricalReprWithFactor :
16
- def test_print (self , factor ):
17
- expected = [
18
- "['a', 'b', 'b', 'a', 'a', 'c', 'c', 'c']" ,
19
- "Categories (3, object): ['a' < 'b' < 'c']" ,
20
- ]
20
+ def test_print (self , factor , using_infer_string ):
21
+ if using_infer_string :
22
+ expected = [
23
+ "['a', 'b', 'b', 'a', 'a', 'c', 'c', 'c']" ,
24
+ "Categories (3, string): [a < b < c]" ,
25
+ ]
26
+ else :
27
+ expected = [
28
+ "['a', 'b', 'b', 'a', 'a', 'c', 'c', 'c']" ,
29
+ "Categories (3, object): ['a' < 'b' < 'c']" ,
30
+ ]
21
31
expected = "\n " .join (expected )
22
32
actual = repr (factor )
23
33
assert actual == expected
@@ -26,7 +36,7 @@ def test_print(self, factor):
26
36
class TestCategoricalRepr :
27
37
def test_big_print (self ):
28
38
codes = np .array ([0 , 1 , 2 , 0 , 1 , 2 ] * 100 )
29
- dtype = CategoricalDtype (categories = ["a" , "b" , "c" ])
39
+ dtype = CategoricalDtype (categories = Index ( ["a" , "b" , "c" ], dtype = object ) )
30
40
factor = Categorical .from_codes (codes , dtype = dtype )
31
41
expected = [
32
42
"['a', 'b', 'c', 'a', 'b', ..., 'b', 'c', 'a', 'b', 'c']" ,
@@ -40,13 +50,13 @@ def test_big_print(self):
40
50
assert actual == expected
41
51
42
52
def test_empty_print (self ):
43
- factor = Categorical ([], ["a" , "b" , "c" ])
53
+ factor = Categorical ([], Index ( ["a" , "b" , "c" ], dtype = object ) )
44
54
expected = "[], Categories (3, object): ['a', 'b', 'c']"
45
55
actual = repr (factor )
46
56
assert actual == expected
47
57
48
58
assert expected == actual
49
- factor = Categorical ([], ["a" , "b" , "c" ], ordered = True )
59
+ factor = Categorical ([], Index ( ["a" , "b" , "c" ], dtype = object ) , ordered = True )
50
60
expected = "[], Categories (3, object): ['a' < 'b' < 'c']"
51
61
actual = repr (factor )
52
62
assert expected == actual
@@ -66,6 +76,10 @@ def test_print_none_width(self):
66
76
with option_context ("display.width" , None ):
67
77
assert exp == repr (a )
68
78
79
+ @pytest .mark .skipif (
80
+ using_pyarrow_string_dtype (),
81
+ reason = "Change once infer_string is set to True by default" ,
82
+ )
69
83
def test_unicode_print (self ):
70
84
c = Categorical (["aaaaa" , "bb" , "cccc" ] * 20 )
71
85
expected = """\
0 commit comments