1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
| from datetime import datetime
| import operator
|
| import numpy as np
| import pytest
|
| from pandas import (
| Series,
| _testing as tm,
| )
|
|
| def test_title(any_string_dtype):
| s = Series(["FOO", "BAR", np.nan, "Blah", "blurg"], dtype=any_string_dtype)
| result = s.str.title()
| expected = Series(["Foo", "Bar", np.nan, "Blah", "Blurg"], dtype=any_string_dtype)
| tm.assert_series_equal(result, expected)
|
|
| def test_title_mixed_object():
| s = Series(["FOO", np.nan, "bar", True, datetime.today(), "blah", None, 1, 2.0])
| result = s.str.title()
| expected = Series(
| ["Foo", np.nan, "Bar", np.nan, np.nan, "Blah", np.nan, np.nan, np.nan]
| )
| tm.assert_almost_equal(result, expected)
|
|
| def test_lower_upper(any_string_dtype):
| s = Series(["om", np.nan, "nom", "nom"], dtype=any_string_dtype)
|
| result = s.str.upper()
| expected = Series(["OM", np.nan, "NOM", "NOM"], dtype=any_string_dtype)
| tm.assert_series_equal(result, expected)
|
| result = result.str.lower()
| tm.assert_series_equal(result, s)
|
|
| def test_lower_upper_mixed_object():
| s = Series(["a", np.nan, "b", True, datetime.today(), "foo", None, 1, 2.0])
|
| result = s.str.upper()
| expected = Series(["A", np.nan, "B", np.nan, np.nan, "FOO", np.nan, np.nan, np.nan])
| tm.assert_series_equal(result, expected)
|
| result = s.str.lower()
| expected = Series(["a", np.nan, "b", np.nan, np.nan, "foo", np.nan, np.nan, np.nan])
| tm.assert_series_equal(result, expected)
|
|
| @pytest.mark.parametrize(
| "data, expected",
| [
| (
| ["FOO", "BAR", np.nan, "Blah", "blurg"],
| ["Foo", "Bar", np.nan, "Blah", "Blurg"],
| ),
| (["a", "b", "c"], ["A", "B", "C"]),
| (["a b", "a bc. de"], ["A b", "A bc. de"]),
| ],
| )
| def test_capitalize(data, expected, any_string_dtype):
| s = Series(data, dtype=any_string_dtype)
| result = s.str.capitalize()
| expected = Series(expected, dtype=any_string_dtype)
| tm.assert_series_equal(result, expected)
|
|
| def test_capitalize_mixed_object():
| s = Series(["FOO", np.nan, "bar", True, datetime.today(), "blah", None, 1, 2.0])
| result = s.str.capitalize()
| expected = Series(
| ["Foo", np.nan, "Bar", np.nan, np.nan, "Blah", np.nan, np.nan, np.nan]
| )
| tm.assert_series_equal(result, expected)
|
|
| def test_swapcase(any_string_dtype):
| s = Series(["FOO", "BAR", np.nan, "Blah", "blurg"], dtype=any_string_dtype)
| result = s.str.swapcase()
| expected = Series(["foo", "bar", np.nan, "bLAH", "BLURG"], dtype=any_string_dtype)
| tm.assert_series_equal(result, expected)
|
|
| def test_swapcase_mixed_object():
| s = Series(["FOO", np.nan, "bar", True, datetime.today(), "Blah", None, 1, 2.0])
| result = s.str.swapcase()
| expected = Series(
| ["foo", np.nan, "BAR", np.nan, np.nan, "bLAH", np.nan, np.nan, np.nan]
| )
| tm.assert_series_equal(result, expected)
|
|
| def test_casefold():
| # GH25405
| expected = Series(["ss", np.nan, "case", "ssd"])
| s = Series(["ß", np.nan, "case", "ßd"])
| result = s.str.casefold()
|
| tm.assert_series_equal(result, expected)
|
|
| def test_casemethods(any_string_dtype):
| values = ["aaa", "bbb", "CCC", "Dddd", "eEEE"]
| s = Series(values, dtype=any_string_dtype)
| assert s.str.lower().tolist() == [v.lower() for v in values]
| assert s.str.upper().tolist() == [v.upper() for v in values]
| assert s.str.title().tolist() == [v.title() for v in values]
| assert s.str.capitalize().tolist() == [v.capitalize() for v in values]
| assert s.str.swapcase().tolist() == [v.swapcase() for v in values]
|
|
| def test_pad(any_string_dtype):
| s = Series(["a", "b", np.nan, "c", np.nan, "eeeeee"], dtype=any_string_dtype)
|
| result = s.str.pad(5, side="left")
| expected = Series(
| [" a", " b", np.nan, " c", np.nan, "eeeeee"], dtype=any_string_dtype
| )
| tm.assert_series_equal(result, expected)
|
| result = s.str.pad(5, side="right")
| expected = Series(
| ["a ", "b ", np.nan, "c ", np.nan, "eeeeee"], dtype=any_string_dtype
| )
| tm.assert_series_equal(result, expected)
|
| result = s.str.pad(5, side="both")
| expected = Series(
| [" a ", " b ", np.nan, " c ", np.nan, "eeeeee"], dtype=any_string_dtype
| )
| tm.assert_series_equal(result, expected)
|
|
| def test_pad_mixed_object():
| s = Series(["a", np.nan, "b", True, datetime.today(), "ee", None, 1, 2.0])
|
| result = s.str.pad(5, side="left")
| expected = Series(
| [" a", np.nan, " b", np.nan, np.nan, " ee", np.nan, np.nan, np.nan]
| )
| tm.assert_series_equal(result, expected)
|
| result = s.str.pad(5, side="right")
| expected = Series(
| ["a ", np.nan, "b ", np.nan, np.nan, "ee ", np.nan, np.nan, np.nan]
| )
| tm.assert_series_equal(result, expected)
|
| result = s.str.pad(5, side="both")
| expected = Series(
| [" a ", np.nan, " b ", np.nan, np.nan, " ee ", np.nan, np.nan, np.nan]
| )
| tm.assert_series_equal(result, expected)
|
|
| def test_pad_fillchar(any_string_dtype):
| s = Series(["a", "b", np.nan, "c", np.nan, "eeeeee"], dtype=any_string_dtype)
|
| result = s.str.pad(5, side="left", fillchar="X")
| expected = Series(
| ["XXXXa", "XXXXb", np.nan, "XXXXc", np.nan, "eeeeee"], dtype=any_string_dtype
| )
| tm.assert_series_equal(result, expected)
|
| result = s.str.pad(5, side="right", fillchar="X")
| expected = Series(
| ["aXXXX", "bXXXX", np.nan, "cXXXX", np.nan, "eeeeee"], dtype=any_string_dtype
| )
| tm.assert_series_equal(result, expected)
|
| result = s.str.pad(5, side="both", fillchar="X")
| expected = Series(
| ["XXaXX", "XXbXX", np.nan, "XXcXX", np.nan, "eeeeee"], dtype=any_string_dtype
| )
| tm.assert_series_equal(result, expected)
|
|
| def test_pad_fillchar_bad_arg_raises(any_string_dtype):
| s = Series(["a", "b", np.nan, "c", np.nan, "eeeeee"], dtype=any_string_dtype)
|
| msg = "fillchar must be a character, not str"
| with pytest.raises(TypeError, match=msg):
| s.str.pad(5, fillchar="XY")
|
| msg = "fillchar must be a character, not int"
| with pytest.raises(TypeError, match=msg):
| s.str.pad(5, fillchar=5)
|
|
| @pytest.mark.parametrize("method_name", ["center", "ljust", "rjust", "zfill", "pad"])
| def test_pad_width_bad_arg_raises(method_name, any_string_dtype):
| # see gh-13598
| s = Series(["1", "22", "a", "bb"], dtype=any_string_dtype)
| op = operator.methodcaller(method_name, "f")
|
| msg = "width must be of integer type, not str"
| with pytest.raises(TypeError, match=msg):
| op(s.str)
|
|
| def test_center_ljust_rjust(any_string_dtype):
| s = Series(["a", "b", np.nan, "c", np.nan, "eeeeee"], dtype=any_string_dtype)
|
| result = s.str.center(5)
| expected = Series(
| [" a ", " b ", np.nan, " c ", np.nan, "eeeeee"], dtype=any_string_dtype
| )
| tm.assert_series_equal(result, expected)
|
| result = s.str.ljust(5)
| expected = Series(
| ["a ", "b ", np.nan, "c ", np.nan, "eeeeee"], dtype=any_string_dtype
| )
| tm.assert_series_equal(result, expected)
|
| result = s.str.rjust(5)
| expected = Series(
| [" a", " b", np.nan, " c", np.nan, "eeeeee"], dtype=any_string_dtype
| )
| tm.assert_series_equal(result, expected)
|
|
| def test_center_ljust_rjust_mixed_object():
| s = Series(["a", np.nan, "b", True, datetime.today(), "c", "eee", None, 1, 2.0])
|
| result = s.str.center(5)
| expected = Series(
| [
| " a ",
| np.nan,
| " b ",
| np.nan,
| np.nan,
| " c ",
| " eee ",
| np.nan,
| np.nan,
| np.nan,
| ]
| )
| tm.assert_series_equal(result, expected)
|
| result = s.str.ljust(5)
| expected = Series(
| [
| "a ",
| np.nan,
| "b ",
| np.nan,
| np.nan,
| "c ",
| "eee ",
| np.nan,
| np.nan,
| np.nan,
| ]
| )
| tm.assert_series_equal(result, expected)
|
| result = s.str.rjust(5)
| expected = Series(
| [
| " a",
| np.nan,
| " b",
| np.nan,
| np.nan,
| " c",
| " eee",
| np.nan,
| np.nan,
| np.nan,
| ]
| )
| tm.assert_series_equal(result, expected)
|
|
| def test_center_ljust_rjust_fillchar(any_string_dtype):
| s = Series(["a", "bb", "cccc", "ddddd", "eeeeee"], dtype=any_string_dtype)
|
| result = s.str.center(5, fillchar="X")
| expected = Series(
| ["XXaXX", "XXbbX", "Xcccc", "ddddd", "eeeeee"], dtype=any_string_dtype
| )
| tm.assert_series_equal(result, expected)
| expected = np.array([v.center(5, "X") for v in np.array(s)], dtype=np.object_)
| tm.assert_numpy_array_equal(np.array(result, dtype=np.object_), expected)
|
| result = s.str.ljust(5, fillchar="X")
| expected = Series(
| ["aXXXX", "bbXXX", "ccccX", "ddddd", "eeeeee"], dtype=any_string_dtype
| )
| tm.assert_series_equal(result, expected)
| expected = np.array([v.ljust(5, "X") for v in np.array(s)], dtype=np.object_)
| tm.assert_numpy_array_equal(np.array(result, dtype=np.object_), expected)
|
| result = s.str.rjust(5, fillchar="X")
| expected = Series(
| ["XXXXa", "XXXbb", "Xcccc", "ddddd", "eeeeee"], dtype=any_string_dtype
| )
| tm.assert_series_equal(result, expected)
| expected = np.array([v.rjust(5, "X") for v in np.array(s)], dtype=np.object_)
| tm.assert_numpy_array_equal(np.array(result, dtype=np.object_), expected)
|
|
| def test_center_ljust_rjust_fillchar_bad_arg_raises(any_string_dtype):
| s = Series(["a", "bb", "cccc", "ddddd", "eeeeee"], dtype=any_string_dtype)
|
| # If fillchar is not a character, normal str raises TypeError
| # 'aaa'.ljust(5, 'XY')
| # TypeError: must be char, not str
| template = "fillchar must be a character, not {dtype}"
|
| with pytest.raises(TypeError, match=template.format(dtype="str")):
| s.str.center(5, fillchar="XY")
|
| with pytest.raises(TypeError, match=template.format(dtype="str")):
| s.str.ljust(5, fillchar="XY")
|
| with pytest.raises(TypeError, match=template.format(dtype="str")):
| s.str.rjust(5, fillchar="XY")
|
| with pytest.raises(TypeError, match=template.format(dtype="int")):
| s.str.center(5, fillchar=1)
|
| with pytest.raises(TypeError, match=template.format(dtype="int")):
| s.str.ljust(5, fillchar=1)
|
| with pytest.raises(TypeError, match=template.format(dtype="int")):
| s.str.rjust(5, fillchar=1)
|
|
| def test_zfill(any_string_dtype):
| s = Series(["1", "22", "aaa", "333", "45678"], dtype=any_string_dtype)
|
| result = s.str.zfill(5)
| expected = Series(
| ["00001", "00022", "00aaa", "00333", "45678"], dtype=any_string_dtype
| )
| tm.assert_series_equal(result, expected)
| expected = np.array([v.zfill(5) for v in np.array(s)], dtype=np.object_)
| tm.assert_numpy_array_equal(np.array(result, dtype=np.object_), expected)
|
| result = s.str.zfill(3)
| expected = Series(["001", "022", "aaa", "333", "45678"], dtype=any_string_dtype)
| tm.assert_series_equal(result, expected)
| expected = np.array([v.zfill(3) for v in np.array(s)], dtype=np.object_)
| tm.assert_numpy_array_equal(np.array(result, dtype=np.object_), expected)
|
| s = Series(["1", np.nan, "aaa", np.nan, "45678"], dtype=any_string_dtype)
| result = s.str.zfill(5)
| expected = Series(
| ["00001", np.nan, "00aaa", np.nan, "45678"], dtype=any_string_dtype
| )
| tm.assert_series_equal(result, expected)
|
|
| def test_wrap(any_string_dtype):
| # test values are: two words less than width, two words equal to width,
| # two words greater than width, one word less than width, one word
| # equal to width, one word greater than width, multiple tokens with
| # trailing whitespace equal to width
| s = Series(
| [
| "hello world",
| "hello world!",
| "hello world!!",
| "abcdefabcde",
| "abcdefabcdef",
| "abcdefabcdefa",
| "ab ab ab ab ",
| "ab ab ab ab a",
| "\t",
| ],
| dtype=any_string_dtype,
| )
|
| # expected values
| expected = Series(
| [
| "hello world",
| "hello world!",
| "hello\nworld!!",
| "abcdefabcde",
| "abcdefabcdef",
| "abcdefabcdef\na",
| "ab ab ab ab",
| "ab ab ab ab\na",
| "",
| ],
| dtype=any_string_dtype,
| )
|
| result = s.str.wrap(12, break_long_words=True)
| tm.assert_series_equal(result, expected)
|
|
| def test_wrap_unicode(any_string_dtype):
| # test with pre and post whitespace (non-unicode), NaN, and non-ascii Unicode
| s = Series(
| [" pre ", np.nan, "\xac\u20ac\U00008000 abadcafe"], dtype=any_string_dtype
| )
| expected = Series(
| [" pre", np.nan, "\xac\u20ac\U00008000 ab\nadcafe"], dtype=any_string_dtype
| )
| result = s.str.wrap(6)
| tm.assert_series_equal(result, expected)
|
|