13
13
14
14
15
15
DATA = ["applep" , "bananap" , "Cherryp" , "DATEp" , "eGGpLANTp" , "123p" , "23.45p" ]
16
+ DATA_BYTES = [b"applep" , b"bananap" ]
16
17
17
18
18
19
def test_string_accessors_type_preserving_series () -> None :
19
- s = pd .Series (DATA )
20
- _check = functools .partial (check , klass = pd .Series , dtype = str )
21
- _check (assert_type (s .str .capitalize (), "pd.Series[str]" ))
22
- _check (assert_type (s .str .casefold (), "pd.Series[str]" ))
23
- check (assert_type (s .str .cat (sep = "X" ), str ), str )
24
- _check (assert_type (s .str .center (10 ), "pd.Series[str]" ))
25
- _check (assert_type (s .str .get (2 ), "pd.Series[str]" ))
26
- _check (assert_type (s .str .ljust (80 ), "pd.Series[str]" ))
27
- _check (assert_type (s .str .lower (), "pd.Series[str]" ))
28
- _check (assert_type (s .str .lstrip ("a" ), "pd.Series[str]" ))
29
- _check (assert_type (s .str .normalize ("NFD" ), "pd.Series[str]" ))
30
- _check (assert_type (s .str .pad (80 , "right" ), "pd.Series[str]" ))
31
- _check (assert_type (s .str .removeprefix ("a" ), "pd.Series[str]" ))
32
- _check (assert_type (s .str .removesuffix ("e" ), "pd.Series[str]" ))
33
- _check (assert_type (s .str .repeat (2 ), "pd.Series[str]" ))
34
- _check (assert_type (s .str .replace ("a" , "X" ), "pd.Series[str]" ))
35
- _check (assert_type (s .str .rjust (80 ), "pd.Series[str]" ))
36
- _check (assert_type (s .str .rstrip (), "pd.Series[str]" ))
37
- _check (assert_type (s .str .slice (0 , 4 , 2 ), "pd.Series[str]" ))
38
- _check (assert_type (s .str .slice_replace (0 , 2 , "XX" ), "pd.Series[str]" ))
39
- _check (assert_type (s .str .strip (), "pd.Series[str]" ))
40
- _check (assert_type (s .str .swapcase (), "pd.Series[str]" ))
41
- _check (assert_type (s .str .title (), "pd.Series[str]" ))
42
- _check (
43
- assert_type (s .str .translate ({241 : "n" }), "pd.Series[str]" ),
44
- )
45
- _check (assert_type (s .str .upper (), "pd.Series[str]" ))
46
- _check (assert_type (s .str .wrap (80 ), "pd.Series[str]" ))
47
- _check (assert_type (s .str .zfill (10 ), "pd.Series[str]" ))
20
+ s_str = pd .Series (DATA )
21
+ s_bytes = pd .Series (DATA_BYTES )
22
+ check (assert_type (s_str .str .slice (0 , 4 , 2 ), "pd.Series[str]" ), pd .Series , str )
23
+ check (assert_type (s_bytes .str .slice (0 , 4 , 2 ), "pd.Series[bytes]" ), pd .Series , bytes )
48
24
49
25
50
26
def test_string_accessors_type_preserving_index () -> None :
51
- idx = pd .Index (DATA )
52
- _check = functools .partial (check , klass = pd .Index , dtype = str )
53
- _check (assert_type (idx .str .capitalize (), "pd.Index[str]" ))
54
- _check (assert_type (idx .str .casefold (), "pd.Index[str]" ))
55
- check (assert_type (idx .str .cat (sep = "X" ), str ), str )
56
- _check (assert_type (idx .str .center (10 ), "pd.Index[str]" ))
57
- _check (assert_type (idx .str .get (2 ), "pd.Index[str]" ))
58
- _check (assert_type (idx .str .ljust (80 ), "pd.Index[str]" ))
59
- _check (assert_type (idx .str .lower (), "pd.Index[str]" ))
60
- _check (assert_type (idx .str .lstrip ("a" ), "pd.Index[str]" ))
61
- _check (assert_type (idx .str .normalize ("NFD" ), "pd.Index[str]" ))
62
- _check (assert_type (idx .str .pad (80 , "right" ), "pd.Index[str]" ))
63
- _check (assert_type (idx .str .removeprefix ("a" ), "pd.Index[str]" ))
64
- _check (assert_type (idx .str .removesuffix ("e" ), "pd.Index[str]" ))
65
- _check (assert_type (idx .str .repeat (2 ), "pd.Index[str]" ))
66
- _check (assert_type (idx .str .replace ("a" , "X" ), "pd.Index[str]" ))
67
- _check (assert_type (idx .str .rjust (80 ), "pd.Index[str]" ))
68
- _check (assert_type (idx .str .rstrip (), "pd.Index[str]" ))
69
- _check (assert_type (idx .str .slice (0 , 4 , 2 ), "pd.Index[str]" ))
70
- _check (assert_type (idx .str .slice_replace (0 , 2 , "XX" ), "pd.Index[str]" ))
71
- _check (assert_type (idx .str .strip (), "pd.Index[str]" ))
72
- _check (assert_type (idx .str .swapcase (), "pd.Index[str]" ))
73
- _check (assert_type (idx .str .title (), "pd.Index[str]" ))
74
- _check (
75
- assert_type (idx .str .translate ({241 : "n" }), "pd.Index[str]" ),
76
- )
77
- _check (assert_type (idx .str .upper (), "pd.Index[str]" ))
78
- _check (assert_type (idx .str .wrap (80 ), "pd.Index[str]" ))
79
- _check (assert_type (idx .str .zfill (10 ), "pd.Index[str]" ))
27
+ idx_str = pd .Index (DATA )
28
+ idx_bytes = pd .Index (DATA_BYTES )
29
+ check (assert_type (idx_str .str .slice (0 , 4 , 2 ), "pd.Index[str]" ), pd .Index , str )
30
+ check (assert_type (idx_bytes .str .slice (0 , 4 , 2 ), "pd.Index[bytes]" ), pd .Index , bytes )
80
31
81
32
82
33
def test_string_accessors_boolean_series ():
@@ -158,21 +109,73 @@ def test_string_accessors_integer_index():
158
109
159
110
160
111
def test_string_accessors_string_series ():
161
- s = pd .Series ([ b"a1" , b"b2" , b"c3" ] )
112
+ s = pd .Series (DATA )
162
113
_check = functools .partial (check , klass = pd .Series , dtype = str )
163
- _check (assert_type (s .str .decode ("utf-8" ), "pd.Series[str]" ))
164
- s2 = pd .Series ([["apple" , "banana" ], ["cherry" , "date" ], [1 , "eggplant" ]])
165
- _check (assert_type (s2 .str .join ("-" ), "pd.Series[str]" ))
114
+ _check (assert_type (s .str .capitalize (), "pd.Series[str]" ))
115
+ _check (assert_type (s .str .casefold (), "pd.Series[str]" ))
116
+ check (assert_type (s .str .cat (sep = "X" ), str ), str )
117
+ _check (assert_type (s .str .center (10 ), "pd.Series[str]" ))
118
+ _check (assert_type (s .str .get (2 ), "pd.Series[str]" ))
119
+ _check (assert_type (s .str .ljust (80 ), "pd.Series[str]" ))
120
+ _check (assert_type (s .str .lower (), "pd.Series[str]" ))
121
+ _check (assert_type (s .str .lstrip ("a" ), "pd.Series[str]" ))
122
+ _check (assert_type (s .str .normalize ("NFD" ), "pd.Series[str]" ))
123
+ _check (assert_type (s .str .pad (80 , "right" ), "pd.Series[str]" ))
124
+ _check (assert_type (s .str .removeprefix ("a" ), "pd.Series[str]" ))
125
+ _check (assert_type (s .str .removesuffix ("e" ), "pd.Series[str]" ))
126
+ _check (assert_type (s .str .repeat (2 ), "pd.Series[str]" ))
127
+ _check (assert_type (s .str .replace ("a" , "X" ), "pd.Series[str]" ))
128
+ _check (assert_type (s .str .rjust (80 ), "pd.Series[str]" ))
129
+ _check (assert_type (s .str .rstrip (), "pd.Series[str]" ))
130
+ _check (assert_type (s .str .slice_replace (0 , 2 , "XX" ), "pd.Series[str]" ))
131
+ _check (assert_type (s .str .strip (), "pd.Series[str]" ))
132
+ _check (assert_type (s .str .swapcase (), "pd.Series[str]" ))
133
+ _check (assert_type (s .str .title (), "pd.Series[str]" ))
134
+ _check (
135
+ assert_type (s .str .translate ({241 : "n" }), "pd.Series[str]" ),
136
+ )
137
+ _check (assert_type (s .str .upper (), "pd.Series[str]" ))
138
+ _check (assert_type (s .str .wrap (80 ), "pd.Series[str]" ))
139
+ _check (assert_type (s .str .zfill (10 ), "pd.Series[str]" ))
140
+ s_bytes = pd .Series ([b"a1" , b"b2" , b"c3" ])
141
+ _check (assert_type (s_bytes .str .decode ("utf-8" ), "pd.Series[str]" ))
142
+ s_list = pd .Series ([["apple" , "banana" ], ["cherry" , "date" ], [1 , "eggplant" ]])
143
+ _check (assert_type (s_list .str .join ("-" ), "pd.Series[str]" ))
166
144
167
145
168
146
def test_string_accessors_string_index ():
169
- idx = pd .Index ([ b"a1" , b"b2" , b"c3" ] )
147
+ idx = pd .Index (DATA )
170
148
_check = functools .partial (check , klass = pd .Index , dtype = str )
171
- _check (assert_type (idx .str .decode ("utf-8" ), "pd.Index[str]" ))
172
- idx2 : "pd.Index[list]" = pd .Index (
173
- [["apple" , "banana" ], ["cherry" , "date" ], [1 , "eggplant" ]]
149
+ _check (assert_type (idx .str .capitalize (), "pd.Index[str]" ))
150
+ _check (assert_type (idx .str .casefold (), "pd.Index[str]" ))
151
+ check (assert_type (idx .str .cat (sep = "X" ), str ), str )
152
+ _check (assert_type (idx .str .center (10 ), "pd.Index[str]" ))
153
+ _check (assert_type (idx .str .get (2 ), "pd.Index[str]" ))
154
+ _check (assert_type (idx .str .ljust (80 ), "pd.Index[str]" ))
155
+ _check (assert_type (idx .str .lower (), "pd.Index[str]" ))
156
+ _check (assert_type (idx .str .lstrip ("a" ), "pd.Index[str]" ))
157
+ _check (assert_type (idx .str .normalize ("NFD" ), "pd.Index[str]" ))
158
+ _check (assert_type (idx .str .pad (80 , "right" ), "pd.Index[str]" ))
159
+ _check (assert_type (idx .str .removeprefix ("a" ), "pd.Index[str]" ))
160
+ _check (assert_type (idx .str .removesuffix ("e" ), "pd.Index[str]" ))
161
+ _check (assert_type (idx .str .repeat (2 ), "pd.Index[str]" ))
162
+ _check (assert_type (idx .str .replace ("a" , "X" ), "pd.Index[str]" ))
163
+ _check (assert_type (idx .str .rjust (80 ), "pd.Index[str]" ))
164
+ _check (assert_type (idx .str .rstrip (), "pd.Index[str]" ))
165
+ _check (assert_type (idx .str .slice_replace (0 , 2 , "XX" ), "pd.Index[str]" ))
166
+ _check (assert_type (idx .str .strip (), "pd.Index[str]" ))
167
+ _check (assert_type (idx .str .swapcase (), "pd.Index[str]" ))
168
+ _check (assert_type (idx .str .title (), "pd.Index[str]" ))
169
+ _check (
170
+ assert_type (idx .str .translate ({241 : "n" }), "pd.Index[str]" ),
174
171
)
175
- _check (assert_type (idx2 .str .join ("-" ), "pd.Index[str]" ))
172
+ _check (assert_type (idx .str .upper (), "pd.Index[str]" ))
173
+ _check (assert_type (idx .str .wrap (80 ), "pd.Index[str]" ))
174
+ _check (assert_type (idx .str .zfill (10 ), "pd.Index[str]" ))
175
+ idx_bytes = pd .Index ([b"a1" , b"b2" , b"c3" ])
176
+ _check (assert_type (idx_bytes .str .decode ("utf-8" ), "pd.Index[str]" ))
177
+ idx_list = pd .Index ([["apple" , "banana" ], ["cherry" , "date" ], [1 , "eggplant" ]])
178
+ _check (assert_type (idx_list .str .join ("-" ), "pd.Index[str]" ))
176
179
177
180
178
181
def test_string_accessors_bytes_series ():
@@ -325,6 +328,12 @@ def test_series_overloads_cat():
325
328
)
326
329
unknown_s = pd .DataFrame ({"a" : list ("abcdefg" )})["a" ]
327
330
check (assert_type (s .str .cat (unknown_s , sep = ";" ), "pd.Series[str]" ), pd .Series , str )
331
+ check (assert_type (unknown_s .str .cat (s , sep = ";" ), "pd.Series[str]" ), pd .Series , str )
332
+ check (
333
+ assert_type (unknown_s .str .cat (unknown_s , sep = ";" ), "pd.Series[str]" ),
334
+ pd .Series ,
335
+ str ,
336
+ )
328
337
329
338
330
339
def test_index_overloads_cat ():
@@ -351,6 +360,14 @@ def test_index_overloads_cat():
351
360
check (
352
361
assert_type (idx .str .cat (unknown_idx , sep = ";" ), "pd.Index[str]" ), pd .Index , str
353
362
)
363
+ check (
364
+ assert_type (unknown_idx .str .cat (idx , sep = ";" ), "pd.Index[str]" ), pd .Index , str
365
+ )
366
+ check (
367
+ assert_type (unknown_idx .str .cat (unknown_idx , sep = ";" ), "pd.Index[str]" ),
368
+ pd .Index ,
369
+ str ,
370
+ )
354
371
355
372
356
373
def test_series_overloads_extract ():
0 commit comments