forked from Kyligence/spark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrings.py
2366 lines (1915 loc) · 70.5 KB
/
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
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""
String functions on pandas-on-Spark Series
"""
from typing import (
Any,
Callable,
Dict,
List,
Optional,
Union,
cast,
no_type_check,
)
import numpy as np
import pandas as pd
from pyspark.sql.types import StringType, BinaryType, ArrayType, LongType, MapType
from pyspark.sql import functions as F
from pyspark.sql.functions import pandas_udf
import pyspark.pandas as ps
from pyspark.pandas.spark import functions as SF
class StringMethods:
"""String methods for pandas-on-Spark Series"""
def __init__(self, series: "ps.Series"):
if not isinstance(series.spark.data_type, (StringType, BinaryType, ArrayType)):
raise ValueError("Cannot call StringMethods on type {}".format(series.spark.data_type))
self._data = series
# Methods
def capitalize(self) -> "ps.Series":
"""
Convert Strings in the series to be capitalized.
Examples
--------
>>> s = ps.Series(['lower', 'CAPITALS', 'this is a sentence', 'SwApCaSe'])
>>> s
0 lower
1 CAPITALS
2 this is a sentence
3 SwApCaSe
dtype: object
>>> s.str.capitalize()
0 Lower
1 Capitals
2 This is a sentence
3 Swapcase
dtype: object
"""
def pandas_capitalize(s) -> ps.Series[str]: # type: ignore[no-untyped-def]
return s.str.capitalize()
return self._data.pandas_on_spark.transform_batch(pandas_capitalize)
def title(self) -> "ps.Series":
"""
Convert Strings in the series to be title case.
Examples
--------
>>> s = ps.Series(['lower', 'CAPITALS', 'this is a sentence', 'SwApCaSe'])
>>> s
0 lower
1 CAPITALS
2 this is a sentence
3 SwApCaSe
dtype: object
>>> s.str.title()
0 Lower
1 Capitals
2 This Is A Sentence
3 Swapcase
dtype: object
"""
def pandas_title(s) -> ps.Series[str]: # type: ignore[no-untyped-def]
return s.str.title()
return self._data.pandas_on_spark.transform_batch(pandas_title)
def lower(self) -> "ps.Series":
"""
Convert strings in the Series/Index to all lowercase.
Examples
--------
>>> s = ps.Series(['lower', 'CAPITALS', 'this is a sentence', 'SwApCaSe'])
>>> s
0 lower
1 CAPITALS
2 this is a sentence
3 SwApCaSe
dtype: object
>>> s.str.lower()
0 lower
1 capitals
2 this is a sentence
3 swapcase
dtype: object
"""
return self._data.spark.transform(F.lower)
def upper(self) -> "ps.Series":
"""
Convert strings in the Series/Index to all uppercase.
Examples
--------
>>> s = ps.Series(['lower', 'CAPITALS', 'this is a sentence', 'SwApCaSe'])
>>> s
0 lower
1 CAPITALS
2 this is a sentence
3 SwApCaSe
dtype: object
>>> s.str.upper()
0 LOWER
1 CAPITALS
2 THIS IS A SENTENCE
3 SWAPCASE
dtype: object
"""
return self._data.spark.transform(F.upper)
def swapcase(self) -> "ps.Series":
"""
Convert strings in the Series/Index to be swap cased.
Examples
--------
>>> s = ps.Series(['lower', 'CAPITALS', 'this is a sentence', 'SwApCaSe'])
>>> s
0 lower
1 CAPITALS
2 this is a sentence
3 SwApCaSe
dtype: object
>>> s.str.swapcase()
0 LOWER
1 capitals
2 THIS IS A SENTENCE
3 sWaPcAsE
dtype: object
"""
def pandas_swapcase(s) -> ps.Series[str]: # type: ignore[no-untyped-def]
return s.str.swapcase()
return self._data.pandas_on_spark.transform_batch(pandas_swapcase)
def startswith(self, pattern: str, na: Optional[Any] = None) -> "ps.Series":
"""
Test if the start of each string element matches a pattern.
Equivalent to :func:`str.startswith`.
Parameters
----------
pattern : str
Character sequence. Regular expressions are not accepted.
na : object, default None
Object shown if element is not a string. NaN converted to None.
Returns
-------
Series of bool or object
pandas-on-Spark Series of booleans indicating whether the given pattern
matches the start of each string element.
Examples
--------
>>> s = ps.Series(['bat', 'Bear', 'cat', np.nan])
>>> s
0 bat
1 Bear
2 cat
3 None
dtype: object
>>> s.str.startswith('b')
0 True
1 False
2 False
3 None
dtype: object
Specifying na to be False instead of None.
>>> s.str.startswith('b', na=False)
0 True
1 False
2 False
3 False
dtype: bool
"""
def pandas_startswith(s) -> ps.Series[bool]: # type: ignore[no-untyped-def]
return s.str.startswith(pattern, na)
return self._data.pandas_on_spark.transform_batch(pandas_startswith)
def endswith(self, pattern: str, na: Optional[Any] = None) -> "ps.Series":
"""
Test if the end of each string element matches a pattern.
Equivalent to :func:`str.endswith`.
Parameters
----------
pattern : str
Character sequence. Regular expressions are not accepted.
na : object, default None
Object shown if element is not a string. NaN converted to None.
Returns
-------
Series of bool or object
pandas-on-Spark Series of booleans indicating whether the given pattern
matches the end of each string element.
Examples
--------
>>> s = ps.Series(['bat', 'Bear', 'cat', np.nan])
>>> s
0 bat
1 Bear
2 cat
3 None
dtype: object
>>> s.str.endswith('t')
0 True
1 False
2 True
3 None
dtype: object
Specifying na to be False instead of None.
>>> s.str.endswith('t', na=False)
0 True
1 False
2 True
3 False
dtype: bool
"""
def pandas_endswith(s) -> ps.Series[bool]: # type: ignore[no-untyped-def]
return s.str.endswith(pattern, na)
return self._data.pandas_on_spark.transform_batch(pandas_endswith)
def strip(self, to_strip: Optional[str] = None) -> "ps.Series":
"""
Remove leading and trailing characters.
Strip whitespaces (including newlines) or a set of specified
characters from each string in the Series/Index from left and
right sides. Equivalent to :func:`str.strip`.
Parameters
----------
to_strip : str
Specifying the set of characters to be removed. All combinations
of this set of characters will be stripped. If None then
whitespaces are removed.
Returns
-------
Series of objects
Examples
--------
>>> s = ps.Series(['1. Ant.', '2. Bee!\\t', None])
>>> s
0 1. Ant.
1 2. Bee!\\t
2 None
dtype: object
>>> s.str.strip()
0 1. Ant.
1 2. Bee!
2 None
dtype: object
>>> s.str.strip('12.')
0 Ant
1 Bee!\\t
2 None
dtype: object
>>> s.str.strip('.!\\t')
0 1. Ant
1 2. Bee
2 None
dtype: object
"""
def pandas_strip(s) -> ps.Series[str]: # type: ignore[no-untyped-def]
return s.str.strip(to_strip)
return self._data.pandas_on_spark.transform_batch(pandas_strip)
def lstrip(self, to_strip: Optional[str] = None) -> "ps.Series":
"""
Remove leading characters.
Strip whitespaces (including newlines) or a set of specified
characters from each string in the Series/Index from left side.
Equivalent to :func:`str.lstrip`.
Parameters
----------
to_strip : str
Specifying the set of characters to be removed. All combinations
of this set of characters will be stripped. If None then
whitespaces are removed.
Returns
-------
Series of object
Examples
--------
>>> s = ps.Series(['1. Ant.', '2. Bee!\\t', None])
>>> s
0 1. Ant.
1 2. Bee!\\t
2 None
dtype: object
>>> s.str.lstrip('12.')
0 Ant.
1 Bee!\\t
2 None
dtype: object
"""
def pandas_lstrip(s) -> ps.Series[str]: # type: ignore[no-untyped-def]
return s.str.lstrip(to_strip)
return self._data.pandas_on_spark.transform_batch(pandas_lstrip)
def rstrip(self, to_strip: Optional[str] = None) -> "ps.Series":
"""
Remove trailing characters.
Strip whitespaces (including newlines) or a set of specified
characters from each string in the Series/Index from right side.
Equivalent to :func:`str.rstrip`.
Parameters
----------
to_strip : str
Specifying the set of characters to be removed. All combinations
of this set of characters will be stripped. If None then
whitespaces are removed.
Returns
-------
Series of object
Examples
--------
>>> s = ps.Series(['1. Ant.', '2. Bee!\\t', None])
>>> s
0 1. Ant.
1 2. Bee!\\t
2 None
dtype: object
>>> s.str.rstrip('.!\\t')
0 1. Ant
1 2. Bee
2 None
dtype: object
"""
def pandas_rstrip(s) -> ps.Series[str]: # type: ignore[no-untyped-def]
return s.str.rstrip(to_strip)
return self._data.pandas_on_spark.transform_batch(pandas_rstrip)
def get(self, i: int) -> "ps.Series":
"""
Extract element from each string or string list/tuple in the Series
at the specified position.
Parameters
----------
i : int
Position of element to extract.
Returns
-------
Series of objects
Examples
--------
>>> s1 = ps.Series(["String", "123"])
>>> s1
0 String
1 123
dtype: object
>>> s1.str.get(1)
0 t
1 2
dtype: object
>>> s1.str.get(-1)
0 g
1 3
dtype: object
>>> s2 = ps.Series([["a", "b", "c"], ["x", "y"]])
>>> s2
0 [a, b, c]
1 [x, y]
dtype: object
>>> s2.str.get(0)
0 a
1 x
dtype: object
>>> s2.str.get(2)
0 c
1 None
dtype: object
"""
def pandas_get(s) -> ps.Series[str]: # type: ignore[no-untyped-def]
return s.str.get(i)
return self._data.pandas_on_spark.transform_batch(pandas_get)
def isalnum(self) -> "ps.Series":
"""
Check whether all characters in each string are alphanumeric.
This is equivalent to running the Python string method
:func:`str.isalnum` for each element of the Series/Index.
If a string has zero characters, False is returned for that check.
Examples
--------
>>> s1 = ps.Series(['one', 'one1', '1', ''])
>>> s1.str.isalnum()
0 True
1 True
2 True
3 False
dtype: bool
Note that checks against characters mixed with any additional
punctuation or whitespace will evaluate too false for an alphanumeric
check.
>>> s2 = ps.Series(['A B', '1.5', '3,000'])
>>> s2.str.isalnum()
0 False
1 False
2 False
dtype: bool
"""
def pandas_isalnum(s) -> ps.Series[bool]: # type: ignore[no-untyped-def]
return s.str.isalnum()
return self._data.pandas_on_spark.transform_batch(pandas_isalnum)
def isalpha(self) -> "ps.Series":
"""
Check whether all characters in each string are alphabetic.
This is equivalent to running the Python string method
:func:`str.isalpha` for each element of the Series/Index.
If a string has zero characters, False is returned for that check.
Examples
--------
>>> s1 = ps.Series(['one', 'one1', '1', ''])
>>> s1.str.isalpha()
0 True
1 False
2 False
3 False
dtype: bool
"""
def pandas_isalpha(s) -> ps.Series[bool]: # type: ignore[no-untyped-def]
return s.str.isalpha()
return self._data.pandas_on_spark.transform_batch(pandas_isalpha)
def isdigit(self) -> "ps.Series":
"""
Check whether all characters in each string are digits.
This is equivalent to running the Python string method
:func:`str.isdigit` for each element of the Series/Index.
If a string has zero characters, False is returned for that check.
Examples
--------
>>> s = ps.Series(['23', '³', '⅕', ''])
The s.str.isdecimal method checks for characters used to form numbers
in base 10.
>>> s.str.isdecimal()
0 True
1 False
2 False
3 False
dtype: bool
The s.str.isdigit method is the same as s.str.isdecimal but also
includes special digits, like superscripted and subscripted digits in
Unicode.
>>> s.str.isdigit()
0 True
1 True
2 False
3 False
dtype: bool
The s.str.isnumeric method is the same as s.str.isdigit but also
includes other characters that can represent quantities such as unicode
fractions.
>>> s.str.isnumeric()
0 True
1 True
2 True
3 False
dtype: bool
"""
def pandas_isdigit(s) -> ps.Series[bool]: # type: ignore[no-untyped-def]
return s.str.isdigit()
return self._data.pandas_on_spark.transform_batch(pandas_isdigit)
def isspace(self) -> "ps.Series":
"""
Check whether all characters in each string are whitespaces.
This is equivalent to running the Python string method
:func:`str.isspace` for each element of the Series/Index.
If a string has zero characters, False is returned for that check.
Examples
--------
>>> s = ps.Series([' ', '\\t\\r\\n ', ''])
>>> s.str.isspace()
0 True
1 True
2 False
dtype: bool
"""
def pandas_isspace(s) -> ps.Series[bool]: # type: ignore[no-untyped-def]
return s.str.isspace()
return self._data.pandas_on_spark.transform_batch(pandas_isspace)
def islower(self) -> "ps.Series":
"""
Check whether all characters in each string are lowercase.
This is equivalent to running the Python string method
:func:`str.islower` for each element of the Series/Index.
If a string has zero characters, False is returned for that check.
Examples
--------
>>> s = ps.Series(['leopard', 'Golden Eagle', 'SNAKE', ''])
>>> s.str.islower()
0 True
1 False
2 False
3 False
dtype: bool
"""
def pandas_isspace(s) -> ps.Series[bool]: # type: ignore[no-untyped-def]
return s.str.islower()
return self._data.pandas_on_spark.transform_batch(pandas_isspace)
def isupper(self) -> "ps.Series":
"""
Check whether all characters in each string are uppercase.
This is equivalent to running the Python string method
:func:`str.isupper` for each element of the Series/Index.
If a string has zero characters, False is returned for that check.
Examples
--------
>>> s = ps.Series(['leopard', 'Golden Eagle', 'SNAKE', ''])
>>> s.str.isupper()
0 False
1 False
2 True
3 False
dtype: bool
"""
def pandas_isspace(s) -> ps.Series[bool]: # type: ignore[no-untyped-def]
return s.str.isupper()
return self._data.pandas_on_spark.transform_batch(pandas_isspace)
def istitle(self) -> "ps.Series":
"""
Check whether all characters in each string are title case.
This is equivalent to running the Python string method
:func:`str.istitle` for each element of the Series/Index.
If a string has zero characters, False is returned for that check.
Examples
--------
>>> s = ps.Series(['leopard', 'Golden Eagle', 'SNAKE', ''])
The s.str.istitle method checks for whether all words are in title
case (whether only the first letter of each word is capitalized).
Words are assumed to be as any sequence of non-numeric characters
separated by whitespace characters.
>>> s.str.istitle()
0 False
1 True
2 False
3 False
dtype: bool
"""
def pandas_istitle(s) -> ps.Series[bool]: # type: ignore[no-untyped-def]
return s.str.istitle()
return self._data.pandas_on_spark.transform_batch(pandas_istitle)
def isnumeric(self) -> "ps.Series":
"""
Check whether all characters in each string are numeric.
This is equivalent to running the Python string method
:func:`str.isnumeric` for each element of the Series/Index.
If a string has zero characters, False is returned for that check.
Examples
--------
>>> s1 = ps.Series(['one', 'one1', '1', ''])
>>> s1.str.isnumeric()
0 False
1 False
2 True
3 False
dtype: bool
>>> s2 = ps.Series(['23', '³', '⅕', ''])
The s2.str.isdecimal method checks for characters used to form numbers
in base 10.
>>> s2.str.isdecimal()
0 True
1 False
2 False
3 False
dtype: bool
The s2.str.isdigit method is the same as s2.str.isdecimal but also
includes special digits, like superscripted and subscripted digits in
Unicode.
>>> s2.str.isdigit()
0 True
1 True
2 False
3 False
dtype: bool
The s2.str.isnumeric method is the same as s2.str.isdigit but also
includes other characters that can represent quantities such as unicode
fractions.
>>> s2.str.isnumeric()
0 True
1 True
2 True
3 False
dtype: bool
"""
def pandas_isnumeric(s) -> ps.Series[bool]: # type: ignore[no-untyped-def]
return s.str.isnumeric()
return self._data.pandas_on_spark.transform_batch(pandas_isnumeric)
def isdecimal(self) -> "ps.Series":
"""
Check whether all characters in each string are decimals.
This is equivalent to running the Python string method
:func:`str.isdecimal` for each element of the Series/Index.
If a string has zero characters, False is returned for that check.
Examples
--------
>>> s = ps.Series(['23', '³', '⅕', ''])
The s.str.isdecimal method checks for characters used to form numbers
in base 10.
>>> s.str.isdecimal()
0 True
1 False
2 False
3 False
dtype: bool
The s.str.isdigit method is the same as s.str.isdecimal but also
includes special digits, like superscripted and subscripted digits in
Unicode.
>>> s.str.isdigit()
0 True
1 True
2 False
3 False
dtype: bool
The s.str.isnumeric method is the same as s.str.isdigit but also
includes other characters that can represent quantities such as unicode
fractions.
>>> s.str.isnumeric()
0 True
1 True
2 True
3 False
dtype: bool
"""
def pandas_isdecimal(s) -> ps.Series[bool]: # type: ignore[no-untyped-def]
return s.str.isdecimal()
return self._data.pandas_on_spark.transform_batch(pandas_isdecimal)
@no_type_check
def cat(self, others=None, sep=None, na_rep=None, join=None) -> "ps.Series":
"""
Not supported.
"""
raise NotImplementedError()
def center(self, width: int, fillchar: str = " ") -> "ps.Series":
"""
Filling left and right side of strings in the Series/Index with an
additional character. Equivalent to :func:`str.center`.
Parameters
----------
width : int
Minimum width of resulting string; additional characters will be
filled with fillchar.
fillchar : str
Additional character for filling, default is whitespace.
Returns
-------
Series of objects
Examples
--------
>>> s = ps.Series(["caribou", "tiger"])
>>> s
0 caribou
1 tiger
dtype: object
>>> s.str.center(width=10, fillchar='-')
0 -caribou--
1 --tiger---
dtype: object
"""
def pandas_center(s) -> ps.Series[str]: # type: ignore[no-untyped-def]
return s.str.center(width, fillchar)
return self._data.pandas_on_spark.transform_batch(pandas_center)
def contains(
self, pat: str, case: bool = True, flags: int = 0, na: Any = None, regex: bool = True
) -> "ps.Series":
"""
Test if pattern or regex is contained within a string of a Series.
Return boolean Series based on whether a given pattern or regex is
contained within a string of a Series.
Analogous to :func:`match`, but less strict, relying on
:func:`re.search` instead of :func:`re.match`.
Parameters
----------
pat : str
Character sequence or regular expression.
case : bool, default True
If True, case sensitive.
flags : int, default 0 (no flags)
Flags to pass through to the re module, e.g. re.IGNORECASE.
na : default None
Fill value for missing values. NaN converted to None.
regex : bool, default True
If True, assumes the pat is a regular expression.
If False, treats the pat as a literal string.
Returns
-------
Series of boolean values or object
A Series of boolean values indicating whether the given pattern is
contained within the string of each element of the Series.
Examples
--------
Returning a Series of booleans using only a literal pattern.
>>> s1 = ps.Series(['Mouse', 'dog', 'house and parrot', '23', np.NaN])
>>> s1.str.contains('og', regex=False)
0 False
1 True
2 False
3 False
4 None
dtype: object
Specifying case sensitivity using case.
>>> s1.str.contains('oG', case=True, regex=True)
0 False
1 False
2 False
3 False
4 None
dtype: object
Specifying na to be False instead of NaN replaces NaN values with
False. If Series does not contain NaN values the resultant dtype will
be bool, otherwise, an object dtype.
>>> s1.str.contains('og', na=False, regex=True)
0 False
1 True
2 False
3 False
4 False
dtype: bool
Returning ‘house’ or ‘dog’ when either expression occurs in a string.
>>> s1.str.contains('house|dog', regex=True)
0 False
1 True
2 True
3 False
4 None
dtype: object
Ignoring case sensitivity using flags with regex.
>>> import re
>>> s1.str.contains('PARROT', flags=re.IGNORECASE, regex=True)
0 False
1 False
2 True
3 False
4 None
dtype: object
Returning any digit using regular expression.
>>> s1.str.contains('[0-9]', regex=True)
0 False
1 False
2 False
3 True
4 None
dtype: object
Ensure pat is a not a literal pattern when regex is set to True.
Note in the following example one might expect only s2[1] and s2[3]
to return True. However, ‘.0’ as a regex matches any character followed
by a 0.
>>> s2 = ps.Series(['40','40.0','41','41.0','35'])
>>> s2.str.contains('.0', regex=True)
0 True
1 True
2 False
3 True
4 False
dtype: bool
"""
def pandas_contains(s) -> ps.Series[bool]: # type: ignore[no-untyped-def]
return s.str.contains(pat, case, flags, na, regex)
return self._data.pandas_on_spark.transform_batch(pandas_contains)
def count(self, pat: str, flags: int = 0) -> "ps.Series":
"""
Count occurrences of pattern in each string of the Series.
This function is used to count the number of times a particular regex
pattern is repeated in each of the string elements of the Series.
Parameters
----------
pat : str
Valid regular expression.
flags : int, default 0 (no flags)
Flags for the re module.
Returns
-------
Series of int
A Series containing the integer counts of pattern matches.
Examples
--------
>>> s = ps.Series(['A', 'B', 'Aaba', 'Baca', np.NaN, 'CABA', 'cat'])
>>> s.str.count('a')
0 0.0
1 0.0
2 2.0
3 2.0
4 NaN
5 0.0
6 1.0
dtype: float64
Escape '$' to find the literal dollar sign.
>>> s = ps.Series(['$', 'B', 'Aab$', '$$ca', 'C$B$', 'cat'])
>>> s.str.count('\\$')
0 1
1 0
2 1
3 2
4 2
5 0
dtype: int64
"""
def pandas_count(s) -> ps.Series[int]: # type: ignore[no-untyped-def]
return s.str.count(pat, flags)
return self._data.pandas_on_spark.transform_batch(pandas_count)
@no_type_check
def decode(self, encoding, errors="strict") -> "ps.Series":