forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_strings.py
793 lines (618 loc) · 25.9 KB
/
test_strings.py
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
from datetime import (
datetime,
timedelta,
)
import numpy as np
import pytest
from pandas import (
DataFrame,
Index,
MultiIndex,
Series,
isna,
)
import pandas._testing as tm
def assert_series_or_index_equal(left, right):
if isinstance(left, Series):
tm.assert_series_equal(left, right)
else: # Index
tm.assert_index_equal(left, right)
def test_iter():
# GH3638
strs = "google", "wikimedia", "wikipedia", "wikitravel"
ds = Series(strs)
with tm.assert_produces_warning(FutureWarning):
for s in ds.str:
# iter must yield a Series
assert isinstance(s, Series)
# indices of each yielded Series should be equal to the index of
# the original Series
tm.assert_index_equal(s.index, ds.index)
for el in s:
# each element of the series is either a basestring/str or nan
assert isinstance(el, str) or isna(el)
# desired behavior is to iterate until everything would be nan on the
# next iter so make sure the last element of the iterator was 'l' in
# this case since 'wikitravel' is the longest string
assert s.dropna().values.item() == "l"
def test_iter_empty():
ds = Series([], dtype=object)
i, s = 100, 1
with tm.assert_produces_warning(FutureWarning):
for i, s in enumerate(ds.str):
pass
# nothing to iterate over so nothing defined values should remain
# unchanged
assert i == 100
assert s == 1
def test_iter_single_element():
ds = Series(["a"])
with tm.assert_produces_warning(FutureWarning):
for i, s in enumerate(ds.str):
pass
assert not i
tm.assert_series_equal(ds, s)
def test_iter_object_try_string():
ds = Series(
[
slice(None, np.random.randint(10), np.random.randint(10, 20))
for _ in range(4)
]
)
i, s = 100, "h"
with tm.assert_produces_warning(FutureWarning):
for i, s in enumerate(ds.str):
pass
assert i == 100
assert s == "h"
# test integer/float dtypes (inferred by constructor) and mixed
def test_count():
values = np.array(["foo", "foofoo", np.nan, "foooofooofommmfoo"], dtype=np.object_)
result = Series(values).str.count("f[o]+")
exp = Series([1, 2, np.nan, 4])
assert isinstance(result, Series)
tm.assert_series_equal(result, exp)
# mixed
mixed = np.array(
["a", np.nan, "b", True, datetime.today(), "foo", None, 1, 2.0],
dtype=object,
)
rs = Series(mixed).str.count("a")
xp = Series([1, np.nan, 0, np.nan, np.nan, 0, np.nan, np.nan, np.nan])
assert isinstance(rs, Series)
tm.assert_series_equal(rs, xp)
def test_repeat():
values = Series(["a", "b", np.nan, "c", np.nan, "d"])
result = values.str.repeat(3)
exp = Series(["aaa", "bbb", np.nan, "ccc", np.nan, "ddd"])
tm.assert_series_equal(result, exp)
result = values.str.repeat([1, 2, 3, 4, 5, 6])
exp = Series(["a", "bb", np.nan, "cccc", np.nan, "dddddd"])
tm.assert_series_equal(result, exp)
# mixed
mixed = Series(["a", np.nan, "b", True, datetime.today(), "foo", None, 1, 2.0])
rs = Series(mixed).str.repeat(3)
xp = Series(
["aaa", np.nan, "bbb", np.nan, np.nan, "foofoofoo", np.nan, np.nan, np.nan]
)
assert isinstance(rs, Series)
tm.assert_series_equal(rs, xp)
def test_repeat_with_null(nullable_string_dtype):
# GH: 31632
ser = Series(["a", None], dtype=nullable_string_dtype)
result = ser.str.repeat([3, 4])
expected = Series(["aaa", None], dtype=nullable_string_dtype)
tm.assert_series_equal(result, expected)
ser = Series(["a", "b"], dtype=nullable_string_dtype)
result = ser.str.repeat([3, None])
expected = Series(["aaa", None], dtype=nullable_string_dtype)
tm.assert_series_equal(result, expected)
def test_empty_str_methods(any_string_dtype):
empty_str = empty = Series(dtype=any_string_dtype)
if any_string_dtype == "object":
empty_int = Series(dtype="int64")
empty_bool = Series(dtype=bool)
else:
empty_int = Series(dtype="Int64")
empty_bool = Series(dtype="boolean")
empty_object = Series(dtype=object)
empty_bytes = Series(dtype=object)
# GH7241
# (extract) on empty series
tm.assert_series_equal(empty_str, empty.str.cat(empty))
assert "" == empty.str.cat()
tm.assert_series_equal(empty_str, empty.str.title())
tm.assert_series_equal(empty_int, empty.str.count("a"))
tm.assert_series_equal(empty_bool, empty.str.contains("a"))
tm.assert_series_equal(empty_bool, empty.str.startswith("a"))
tm.assert_series_equal(empty_bool, empty.str.endswith("a"))
tm.assert_series_equal(empty_str, empty.str.lower())
tm.assert_series_equal(empty_str, empty.str.upper())
tm.assert_series_equal(empty_str, empty.str.replace("a", "b"))
tm.assert_series_equal(empty_str, empty.str.repeat(3))
tm.assert_series_equal(empty_bool, empty.str.match("^a"))
tm.assert_frame_equal(
DataFrame(columns=[0], dtype=any_string_dtype),
empty.str.extract("()", expand=True),
)
tm.assert_frame_equal(
DataFrame(columns=[0, 1], dtype=any_string_dtype),
empty.str.extract("()()", expand=True),
)
tm.assert_series_equal(empty_str, empty.str.extract("()", expand=False))
tm.assert_frame_equal(
DataFrame(columns=[0, 1], dtype=any_string_dtype),
empty.str.extract("()()", expand=False),
)
tm.assert_frame_equal(DataFrame(), empty.str.get_dummies())
tm.assert_series_equal(empty_str, empty_str.str.join(""))
tm.assert_series_equal(empty_int, empty.str.len())
tm.assert_series_equal(empty_object, empty_str.str.findall("a"))
tm.assert_series_equal(empty_int, empty.str.find("a"))
tm.assert_series_equal(empty_int, empty.str.rfind("a"))
tm.assert_series_equal(empty_str, empty.str.pad(42))
tm.assert_series_equal(empty_str, empty.str.center(42))
tm.assert_series_equal(empty_object, empty.str.split("a"))
tm.assert_series_equal(empty_object, empty.str.rsplit("a"))
tm.assert_series_equal(empty_object, empty.str.partition("a", expand=False))
tm.assert_series_equal(empty_object, empty.str.rpartition("a", expand=False))
tm.assert_series_equal(empty_str, empty.str.slice(stop=1))
tm.assert_series_equal(empty_str, empty.str.slice(step=1))
tm.assert_series_equal(empty_str, empty.str.strip())
tm.assert_series_equal(empty_str, empty.str.lstrip())
tm.assert_series_equal(empty_str, empty.str.rstrip())
tm.assert_series_equal(empty_str, empty.str.wrap(42))
tm.assert_series_equal(empty_str, empty.str.get(0))
tm.assert_series_equal(empty_object, empty_bytes.str.decode("ascii"))
tm.assert_series_equal(empty_bytes, empty.str.encode("ascii"))
# ismethods should always return boolean (GH 29624)
tm.assert_series_equal(empty_bool, empty.str.isalnum())
tm.assert_series_equal(empty_bool, empty.str.isalpha())
tm.assert_series_equal(empty_bool, empty.str.isdigit())
tm.assert_series_equal(empty_bool, empty.str.isspace())
tm.assert_series_equal(empty_bool, empty.str.islower())
tm.assert_series_equal(empty_bool, empty.str.isupper())
tm.assert_series_equal(empty_bool, empty.str.istitle())
tm.assert_series_equal(empty_bool, empty.str.isnumeric())
tm.assert_series_equal(empty_bool, empty.str.isdecimal())
tm.assert_series_equal(empty_str, empty.str.capitalize())
tm.assert_series_equal(empty_str, empty.str.swapcase())
tm.assert_series_equal(empty_str, empty.str.normalize("NFC"))
table = str.maketrans("a", "b")
tm.assert_series_equal(empty_str, empty.str.translate(table))
def test_empty_str_methods_to_frame():
empty = Series(dtype=str)
empty_df = DataFrame()
tm.assert_frame_equal(empty_df, empty.str.partition("a"))
tm.assert_frame_equal(empty_df, empty.str.rpartition("a"))
def test_ismethods(any_string_dtype):
values = ["A", "b", "Xy", "4", "3A", "", "TT", "55", "-", " "]
str_s = Series(values, dtype=any_string_dtype)
alnum_e = [True, True, True, True, True, False, True, True, False, False]
alpha_e = [True, True, True, False, False, False, True, False, False, False]
digit_e = [False, False, False, True, False, False, False, True, False, False]
# TODO: unused
num_e = [ # noqa
False,
False,
False,
True,
False,
False,
False,
True,
False,
False,
]
space_e = [False, False, False, False, False, False, False, False, False, True]
lower_e = [False, True, False, False, False, False, False, False, False, False]
upper_e = [True, False, False, False, True, False, True, False, False, False]
title_e = [True, False, True, False, True, False, False, False, False, False]
dtype = "bool" if any_string_dtype == "object" else "boolean"
tm.assert_series_equal(str_s.str.isalnum(), Series(alnum_e, dtype=dtype))
tm.assert_series_equal(str_s.str.isalpha(), Series(alpha_e, dtype=dtype))
tm.assert_series_equal(str_s.str.isdigit(), Series(digit_e, dtype=dtype))
tm.assert_series_equal(str_s.str.isspace(), Series(space_e, dtype=dtype))
tm.assert_series_equal(str_s.str.islower(), Series(lower_e, dtype=dtype))
tm.assert_series_equal(str_s.str.isupper(), Series(upper_e, dtype=dtype))
tm.assert_series_equal(str_s.str.istitle(), Series(title_e, dtype=dtype))
assert str_s.str.isalnum().tolist() == [v.isalnum() for v in values]
assert str_s.str.isalpha().tolist() == [v.isalpha() for v in values]
assert str_s.str.isdigit().tolist() == [v.isdigit() for v in values]
assert str_s.str.isspace().tolist() == [v.isspace() for v in values]
assert str_s.str.islower().tolist() == [v.islower() for v in values]
assert str_s.str.isupper().tolist() == [v.isupper() for v in values]
assert str_s.str.istitle().tolist() == [v.istitle() for v in values]
def test_isnumeric(any_string_dtype):
# 0x00bc: ¼ VULGAR FRACTION ONE QUARTER
# 0x2605: ★ not number
# 0x1378: ፸ ETHIOPIC NUMBER SEVENTY
# 0xFF13: 3 Em 3
values = ["A", "3", "¼", "★", "፸", "3", "four"]
s = Series(values, dtype=any_string_dtype)
numeric_e = [False, True, True, False, True, True, False]
decimal_e = [False, True, False, False, False, True, False]
dtype = "bool" if any_string_dtype == "object" else "boolean"
tm.assert_series_equal(s.str.isnumeric(), Series(numeric_e, dtype=dtype))
tm.assert_series_equal(s.str.isdecimal(), Series(decimal_e, dtype=dtype))
unicodes = ["A", "3", "¼", "★", "፸", "3", "four"]
assert s.str.isnumeric().tolist() == [v.isnumeric() for v in unicodes]
assert s.str.isdecimal().tolist() == [v.isdecimal() for v in unicodes]
values = ["A", np.nan, "¼", "★", np.nan, "3", "four"]
s = Series(values, dtype=any_string_dtype)
numeric_e = [False, np.nan, True, False, np.nan, True, False]
decimal_e = [False, np.nan, False, False, np.nan, True, False]
dtype = "object" if any_string_dtype == "object" else "boolean"
tm.assert_series_equal(s.str.isnumeric(), Series(numeric_e, dtype=dtype))
tm.assert_series_equal(s.str.isdecimal(), Series(decimal_e, dtype=dtype))
def test_get_dummies():
s = Series(["a|b", "a|c", np.nan])
result = s.str.get_dummies("|")
expected = DataFrame([[1, 1, 0], [1, 0, 1], [0, 0, 0]], columns=list("abc"))
tm.assert_frame_equal(result, expected)
s = Series(["a;b", "a", 7])
result = s.str.get_dummies(";")
expected = DataFrame([[0, 1, 1], [0, 1, 0], [1, 0, 0]], columns=list("7ab"))
tm.assert_frame_equal(result, expected)
# GH9980, GH8028
idx = Index(["a|b", "a|c", "b|c"])
result = idx.str.get_dummies("|")
expected = MultiIndex.from_tuples(
[(1, 1, 0), (1, 0, 1), (0, 1, 1)], names=("a", "b", "c")
)
tm.assert_index_equal(result, expected)
def test_get_dummies_with_name_dummy():
# GH 12180
# Dummies named 'name' should work as expected
s = Series(["a", "b,name", "b"])
result = s.str.get_dummies(",")
expected = DataFrame([[1, 0, 0], [0, 1, 1], [0, 1, 0]], columns=["a", "b", "name"])
tm.assert_frame_equal(result, expected)
idx = Index(["a|b", "name|c", "b|name"])
result = idx.str.get_dummies("|")
expected = MultiIndex.from_tuples(
[(1, 1, 0, 0), (0, 0, 1, 1), (0, 1, 0, 1)], names=("a", "b", "c", "name")
)
tm.assert_index_equal(result, expected)
def test_join():
values = Series(["a_b_c", "c_d_e", np.nan, "f_g_h"])
result = values.str.split("_").str.join("_")
tm.assert_series_equal(values, result)
# mixed
mixed = Series(
[
"a_b",
np.nan,
"asdf_cas_asdf",
True,
datetime.today(),
"foo",
None,
1,
2.0,
]
)
rs = Series(mixed).str.split("_").str.join("_")
xp = Series(
[
"a_b",
np.nan,
"asdf_cas_asdf",
np.nan,
np.nan,
"foo",
np.nan,
np.nan,
np.nan,
]
)
assert isinstance(rs, Series)
tm.assert_almost_equal(rs, xp)
def test_len(any_string_dtype):
values = Series(
["foo", "fooo", "fooooo", np.nan, "fooooooo", "foo\n", "あ"],
dtype=any_string_dtype,
)
result = values.str.len()
expected_dtype = "float64" if any_string_dtype == "object" else "Int64"
expected = Series([3, 4, 6, np.nan, 8, 4, 1], dtype=expected_dtype)
tm.assert_series_equal(result, expected)
def test_len_mixed():
mixed = Series(
[
"a_b",
np.nan,
"asdf_cas_asdf",
True,
datetime.today(),
"foo",
None,
1,
2.0,
]
)
rs = Series(mixed).str.len()
xp = Series([3, np.nan, 13, np.nan, np.nan, 3, np.nan, np.nan, np.nan])
assert isinstance(rs, Series)
tm.assert_almost_equal(rs, xp)
def test_index():
def _check(result, expected):
if isinstance(result, Series):
tm.assert_series_equal(result, expected)
else:
tm.assert_index_equal(result, expected)
for klass in [Series, Index]:
s = klass(["ABCDEFG", "BCDEFEF", "DEFGHIJEF", "EFGHEF"])
result = s.str.index("EF")
_check(result, klass([4, 3, 1, 0]))
expected = np.array([v.index("EF") for v in s.values], dtype=np.int64)
tm.assert_numpy_array_equal(result.values, expected)
result = s.str.rindex("EF")
_check(result, klass([4, 5, 7, 4]))
expected = np.array([v.rindex("EF") for v in s.values], dtype=np.int64)
tm.assert_numpy_array_equal(result.values, expected)
result = s.str.index("EF", 3)
_check(result, klass([4, 3, 7, 4]))
expected = np.array([v.index("EF", 3) for v in s.values], dtype=np.int64)
tm.assert_numpy_array_equal(result.values, expected)
result = s.str.rindex("EF", 3)
_check(result, klass([4, 5, 7, 4]))
expected = np.array([v.rindex("EF", 3) for v in s.values], dtype=np.int64)
tm.assert_numpy_array_equal(result.values, expected)
result = s.str.index("E", 4, 8)
_check(result, klass([4, 5, 7, 4]))
expected = np.array([v.index("E", 4, 8) for v in s.values], dtype=np.int64)
tm.assert_numpy_array_equal(result.values, expected)
result = s.str.rindex("E", 0, 5)
_check(result, klass([4, 3, 1, 4]))
expected = np.array([v.rindex("E", 0, 5) for v in s.values], dtype=np.int64)
tm.assert_numpy_array_equal(result.values, expected)
with pytest.raises(ValueError, match="substring not found"):
result = s.str.index("DE")
msg = "expected a string object, not int"
with pytest.raises(TypeError, match=msg):
result = s.str.index(0)
with pytest.raises(TypeError, match=msg):
result = s.str.rindex(0)
# test with nan
s = Series(["abcb", "ab", "bcbe", np.nan])
result = s.str.index("b")
tm.assert_series_equal(result, Series([1, 1, 0, np.nan]))
result = s.str.rindex("b")
tm.assert_series_equal(result, Series([3, 1, 2, np.nan]))
def test_pipe_failures():
# #2119
s = Series(["A|B|C"])
result = s.str.split("|")
exp = Series([["A", "B", "C"]])
tm.assert_series_equal(result, exp)
result = s.str.replace("|", " ", regex=False)
exp = Series(["A B C"])
tm.assert_series_equal(result, exp)
@pytest.mark.parametrize(
"start, stop, step, expected",
[
(2, 5, None, Series(["foo", "bar", np.nan, "baz"])),
(0, 3, -1, Series(["", "", np.nan, ""])),
(None, None, -1, Series(["owtoofaa", "owtrabaa", np.nan, "xuqzabaa"])),
(3, 10, 2, Series(["oto", "ato", np.nan, "aqx"])),
(3, 0, -1, Series(["ofa", "aba", np.nan, "aba"])),
],
)
def test_slice(start, stop, step, expected):
values = Series(["aafootwo", "aabartwo", np.nan, "aabazqux"])
result = values.str.slice(start, stop, step)
tm.assert_series_equal(result, expected)
# mixed
mixed = Series(
["aafootwo", np.nan, "aabartwo", True, datetime.today(), None, 1, 2.0]
)
rs = Series(mixed).str.slice(2, 5)
xp = Series(["foo", np.nan, "bar", np.nan, np.nan, np.nan, np.nan, np.nan])
assert isinstance(rs, Series)
tm.assert_almost_equal(rs, xp)
rs = Series(mixed).str.slice(2, 5, -1)
xp = Series(["oof", np.nan, "rab", np.nan, np.nan, np.nan, np.nan, np.nan])
def test_slice_replace():
values = Series(["short", "a bit longer", "evenlongerthanthat", "", np.nan])
exp = Series(["shrt", "a it longer", "evnlongerthanthat", "", np.nan])
result = values.str.slice_replace(2, 3)
tm.assert_series_equal(result, exp)
exp = Series(["shzrt", "a zit longer", "evznlongerthanthat", "z", np.nan])
result = values.str.slice_replace(2, 3, "z")
tm.assert_series_equal(result, exp)
exp = Series(["shzort", "a zbit longer", "evzenlongerthanthat", "z", np.nan])
result = values.str.slice_replace(2, 2, "z")
tm.assert_series_equal(result, exp)
exp = Series(["shzort", "a zbit longer", "evzenlongerthanthat", "z", np.nan])
result = values.str.slice_replace(2, 1, "z")
tm.assert_series_equal(result, exp)
exp = Series(["shorz", "a bit longez", "evenlongerthanthaz", "z", np.nan])
result = values.str.slice_replace(-1, None, "z")
tm.assert_series_equal(result, exp)
exp = Series(["zrt", "zer", "zat", "z", np.nan])
result = values.str.slice_replace(None, -2, "z")
tm.assert_series_equal(result, exp)
exp = Series(["shortz", "a bit znger", "evenlozerthanthat", "z", np.nan])
result = values.str.slice_replace(6, 8, "z")
tm.assert_series_equal(result, exp)
exp = Series(["zrt", "a zit longer", "evenlongzerthanthat", "z", np.nan])
result = values.str.slice_replace(-10, 3, "z")
tm.assert_series_equal(result, exp)
def test_strip_lstrip_rstrip(any_string_dtype):
values = Series([" aa ", " bb \n", np.nan, "cc "], dtype=any_string_dtype)
result = values.str.strip()
exp = Series(["aa", "bb", np.nan, "cc"], dtype=any_string_dtype)
tm.assert_series_equal(result, exp)
result = values.str.lstrip()
exp = Series(["aa ", "bb \n", np.nan, "cc "], dtype=any_string_dtype)
tm.assert_series_equal(result, exp)
result = values.str.rstrip()
exp = Series([" aa", " bb", np.nan, "cc"], dtype=any_string_dtype)
tm.assert_series_equal(result, exp)
def test_strip_lstrip_rstrip_mixed():
# mixed
mixed = Series([" aa ", np.nan, " bb \t\n", True, datetime.today(), None, 1, 2.0])
rs = Series(mixed).str.strip()
xp = Series(["aa", np.nan, "bb", np.nan, np.nan, np.nan, np.nan, np.nan])
assert isinstance(rs, Series)
tm.assert_almost_equal(rs, xp)
rs = Series(mixed).str.lstrip()
xp = Series(["aa ", np.nan, "bb \t\n", np.nan, np.nan, np.nan, np.nan, np.nan])
assert isinstance(rs, Series)
tm.assert_almost_equal(rs, xp)
rs = Series(mixed).str.rstrip()
xp = Series([" aa", np.nan, " bb", np.nan, np.nan, np.nan, np.nan, np.nan])
assert isinstance(rs, Series)
tm.assert_almost_equal(rs, xp)
def test_strip_lstrip_rstrip_args(any_string_dtype):
values = Series(["xxABCxx", "xx BNSD", "LDFJH xx"], dtype=any_string_dtype)
rs = values.str.strip("x")
xp = Series(["ABC", " BNSD", "LDFJH "], dtype=any_string_dtype)
tm.assert_series_equal(rs, xp)
rs = values.str.lstrip("x")
xp = Series(["ABCxx", " BNSD", "LDFJH xx"], dtype=any_string_dtype)
tm.assert_series_equal(rs, xp)
rs = values.str.rstrip("x")
xp = Series(["xxABC", "xx BNSD", "LDFJH "], dtype=any_string_dtype)
tm.assert_series_equal(rs, xp)
def test_string_slice_get_syntax():
s = Series(
[
"YYY",
"B",
"C",
"YYYYYYbYYY",
"BYYYcYYY",
np.nan,
"CYYYBYYY",
"dog",
"cYYYt",
]
)
result = s.str[0]
expected = s.str.get(0)
tm.assert_series_equal(result, expected)
result = s.str[:3]
expected = s.str.slice(stop=3)
tm.assert_series_equal(result, expected)
result = s.str[2::-1]
expected = s.str.slice(start=2, step=-1)
tm.assert_series_equal(result, expected)
def test_string_slice_out_of_bounds():
s = Series([(1, 2), (1,), (3, 4, 5)])
result = s.str[1]
expected = Series([2, np.nan, 4])
tm.assert_series_equal(result, expected)
s = Series(["foo", "b", "ba"])
result = s.str[1]
expected = Series(["o", np.nan, "a"])
tm.assert_series_equal(result, expected)
def test_encode_decode():
base = Series(["a", "b", "a\xe4"])
series = base.str.encode("utf-8")
f = lambda x: x.decode("utf-8")
result = series.str.decode("utf-8")
exp = series.map(f)
tm.assert_series_equal(result, exp)
def test_encode_decode_errors():
encodeBase = Series(["a", "b", "a\x9d"])
msg = (
r"'charmap' codec can't encode character '\\x9d' in position 1: "
"character maps to <undefined>"
)
with pytest.raises(UnicodeEncodeError, match=msg):
encodeBase.str.encode("cp1252")
f = lambda x: x.encode("cp1252", "ignore")
result = encodeBase.str.encode("cp1252", "ignore")
exp = encodeBase.map(f)
tm.assert_series_equal(result, exp)
decodeBase = Series([b"a", b"b", b"a\x9d"])
msg = (
"'charmap' codec can't decode byte 0x9d in position 1: "
"character maps to <undefined>"
)
with pytest.raises(UnicodeDecodeError, match=msg):
decodeBase.str.decode("cp1252")
f = lambda x: x.decode("cp1252", "ignore")
result = decodeBase.str.decode("cp1252", "ignore")
exp = decodeBase.map(f)
tm.assert_series_equal(result, exp)
def test_normalize():
values = ["ABC", "ABC", "123", np.nan, "アイエ"]
s = Series(values, index=["a", "b", "c", "d", "e"])
normed = ["ABC", "ABC", "123", np.nan, "アイエ"]
expected = Series(normed, index=["a", "b", "c", "d", "e"])
result = s.str.normalize("NFKC")
tm.assert_series_equal(result, expected)
expected = Series(
["ABC", "ABC", "123", np.nan, "アイエ"], index=["a", "b", "c", "d", "e"]
)
result = s.str.normalize("NFC")
tm.assert_series_equal(result, expected)
with pytest.raises(ValueError, match="invalid normalization form"):
s.str.normalize("xxx")
s = Index(["ABC", "123", "アイエ"])
expected = Index(["ABC", "123", "アイエ"])
result = s.str.normalize("NFKC")
tm.assert_index_equal(result, expected)
def test_index_str_accessor_visibility():
from pandas.core.strings import StringMethods
cases = [
(["a", "b"], "string"),
(["a", "b", 1], "mixed-integer"),
(["a", "b", 1.3], "mixed"),
(["a", "b", 1.3, 1], "mixed-integer"),
(["aa", datetime(2011, 1, 1)], "mixed"),
]
for values, tp in cases:
idx = Index(values)
assert isinstance(Series(values).str, StringMethods)
assert isinstance(idx.str, StringMethods)
assert idx.inferred_type == tp
for values, tp in cases:
idx = Index(values)
assert isinstance(Series(values).str, StringMethods)
assert isinstance(idx.str, StringMethods)
assert idx.inferred_type == tp
cases = [
([1, np.nan], "floating"),
([datetime(2011, 1, 1)], "datetime64"),
([timedelta(1)], "timedelta64"),
]
for values, tp in cases:
idx = Index(values)
message = "Can only use .str accessor with string values"
with pytest.raises(AttributeError, match=message):
Series(values).str
with pytest.raises(AttributeError, match=message):
idx.str
assert idx.inferred_type == tp
# MultiIndex has mixed dtype, but not allow to use accessor
idx = MultiIndex.from_tuples([("a", "b"), ("a", "b")])
assert idx.inferred_type == "mixed"
message = "Can only use .str accessor with Index, not MultiIndex"
with pytest.raises(AttributeError, match=message):
idx.str
def test_str_accessor_no_new_attributes():
# https://github.com/pandas-dev/pandas/issues/10673
s = Series(list("aabbcde"))
with pytest.raises(AttributeError, match="You cannot add any new attribute"):
s.str.xlabel = "a"
def test_method_on_bytes():
lhs = Series(np.array(list("abc"), "S1").astype(object))
rhs = Series(np.array(list("def"), "S1").astype(object))
with pytest.raises(TypeError, match="Cannot use .str.cat with values of.*"):
lhs.str.cat(rhs)
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_str_accessor_in_apply_func():
# https://github.com/pandas-dev/pandas/issues/38979
df = DataFrame(zip("abc", "def"))
expected = Series(["A/D", "B/E", "C/F"])
result = df.apply(lambda f: "/".join(f.str.upper()), axis=1)
tm.assert_series_equal(result, expected)