-
Notifications
You must be signed in to change notification settings - Fork 605
/
strings.py
1685 lines (1473 loc) · 56.3 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
from __future__ import annotations
import functools
import operator
from typing import TYPE_CHECKING, Any, Literal
from public import public
import ibis.expr.operations as ops
from ibis import util
from ibis.expr.types.core import _binop
from ibis.expr.types.generic import Column, Scalar, Value
if TYPE_CHECKING:
from collections.abc import Iterable, Sequence
import ibis.expr.types as ir
@public
class StringValue(Value):
def __getitem__(self, key: slice | int | ir.IntegerScalar) -> StringValue:
"""Index or slice a string expression.
Parameters
----------
key
[](`int`), [](`slice`) or integer scalar expression
Returns
-------
StringValue
Indexed or sliced string value
Examples
--------
>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"food": ["bread", "cheese", "rice"], "idx": [1, 2, 4]})
>>> t
┏━━━━━━━━┳━━━━━━━┓
┃ food ┃ idx ┃
┡━━━━━━━━╇━━━━━━━┩
│ string │ int64 │
├────────┼───────┤
│ bread │ 1 │
│ cheese │ 2 │
│ rice │ 4 │
└────────┴───────┘
>>> t.food[0]
┏━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Substring(food, 0, 1) ┃
┡━━━━━━━━━━━━━━━━━━━━━━━┩
│ string │
├───────────────────────┤
│ b │
│ c │
│ r │
└───────────────────────┘
>>> t.food[:3]
┏━━━━━━━━━━━━━━━━━━━━━━┓
┃ StringSlice(food, 3) ┃
┡━━━━━━━━━━━━━━━━━━━━━━┩
│ string │
├──────────────────────┤
│ bre │
│ che │
│ ric │
└──────────────────────┘
>>> t.food[3:5]
┏━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ StringSlice(food, 3, 5) ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ string │
├─────────────────────────┤
│ ad │
│ es │
│ e │
└─────────────────────────┘
>>> t.food[7]
┏━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Substring(food, 7, 1) ┃
┡━━━━━━━━━━━━━━━━━━━━━━━┩
│ string │
├───────────────────────┤
│ ~ │
│ ~ │
│ ~ │
└───────────────────────┘
"""
from ibis.expr import types as ir
if isinstance(key, slice):
start, stop, step = key.start, key.stop, key.step
if step is not None and not isinstance(step, ir.Expr) and step != 1:
raise ValueError("Step can only be 1")
if start is not None and not isinstance(start, ir.Expr) and start < 0:
raise ValueError(
"Negative slicing not yet supported, got start value "
f"of {start:d}"
)
if stop is not None and not isinstance(stop, ir.Expr) and stop < 0:
raise ValueError(
"Negative slicing not yet supported, got stop value " f"of {stop:d}"
)
if start is None and stop is None:
return self
return ops.StringSlice(self, start, stop).to_expr()
elif isinstance(key, int):
return self.substr(key, 1)
raise NotImplementedError(f"string __getitem__[{key.__class__.__name__}]")
def length(self) -> ir.IntegerValue:
"""Compute the length of a string.
Returns
-------
IntegerValue
The length of each string in the expression
Examples
--------
>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"s": ["aaa", "a", "aa"]})
>>> t.s.length()
┏━━━━━━━━━━━━━━━━━┓
┃ StringLength(s) ┃
┡━━━━━━━━━━━━━━━━━┩
│ int32 │
├─────────────────┤
│ 3 │
│ 1 │
│ 2 │
└─────────────────┘
"""
return ops.StringLength(self).to_expr()
def lower(self) -> StringValue:
"""Convert string to all lowercase.
Returns
-------
StringValue
Lowercase string
Examples
--------
>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"s": ["AAA", "a", "AA"]})
>>> t
┏━━━━━━━━┓
┃ s ┃
┡━━━━━━━━┩
│ string │
├────────┤
│ AAA │
│ a │
│ AA │
└────────┘
>>> t.s.lower()
┏━━━━━━━━━━━━━━┓
┃ Lowercase(s) ┃
┡━━━━━━━━━━━━━━┩
│ string │
├──────────────┤
│ aaa │
│ a │
│ aa │
└──────────────┘
"""
return ops.Lowercase(self).to_expr()
def upper(self) -> StringValue:
"""Convert string to all uppercase.
Returns
-------
StringValue
Uppercase string
Examples
--------
>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"s": ["aaa", "A", "aa"]})
>>> t
┏━━━━━━━━┓
┃ s ┃
┡━━━━━━━━┩
│ string │
├────────┤
│ aaa │
│ A │
│ aa │
└────────┘
>>> t.s.upper()
┏━━━━━━━━━━━━━━┓
┃ Uppercase(s) ┃
┡━━━━━━━━━━━━━━┩
│ string │
├──────────────┤
│ AAA │
│ A │
│ AA │
└──────────────┘
"""
return ops.Uppercase(self).to_expr()
def reverse(self) -> StringValue:
"""Reverse the characters of a string.
Returns
-------
StringValue
Reversed string
Examples
--------
>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"s": ["abc", "def", "ghi"]})
>>> t
┏━━━━━━━━┓
┃ s ┃
┡━━━━━━━━┩
│ string │
├────────┤
│ abc │
│ def │
│ ghi │
└────────┘
>>> t.s.reverse()
┏━━━━━━━━━━━━┓
┃ Reverse(s) ┃
┡━━━━━━━━━━━━┩
│ string │
├────────────┤
│ cba │
│ fed │
│ ihg │
└────────────┘
"""
return ops.Reverse(self).to_expr()
def ascii_str(self) -> ir.IntegerValue:
"""Return the numeric ASCII code of the first character of a string.
Returns
-------
IntegerValue
ASCII code of the first character of the input
Examples
--------
>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"s": ["abc", "def", "ghi"]})
>>> t.s.ascii_str()
┏━━━━━━━━━━━━━━━━┓
┃ StringAscii(s) ┃
┡━━━━━━━━━━━━━━━━┩
│ int32 │
├────────────────┤
│ 97 │
│ 100 │
│ 103 │
└────────────────┘
"""
return ops.StringAscii(self).to_expr()
def strip(self) -> StringValue:
r"""Remove whitespace from left and right sides of a string.
Returns
-------
StringValue
Stripped string
Examples
--------
>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"s": ["\ta\t", "\nb\n", "\vc\t"]})
>>> t
┏━━━━━━━━┓
┃ s ┃
┡━━━━━━━━┩
│ string │
├────────┤
│ \ta\t │
│ \nb\n │
│ \vc\t │
└────────┘
>>> t.s.strip()
┏━━━━━━━━━━┓
┃ Strip(s) ┃
┡━━━━━━━━━━┩
│ string │
├──────────┤
│ a │
│ b │
│ c │
└──────────┘
"""
return ops.Strip(self).to_expr()
def lstrip(self) -> StringValue:
r"""Remove whitespace from the left side of string.
Returns
-------
StringValue
Left-stripped string
Examples
--------
>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"s": ["\ta\t", "\nb\n", "\vc\t"]})
>>> t
┏━━━━━━━━┓
┃ s ┃
┡━━━━━━━━┩
│ string │
├────────┤
│ \ta\t │
│ \nb\n │
│ \vc\t │
└────────┘
>>> t.s.lstrip()
┏━━━━━━━━━━━┓
┃ LStrip(s) ┃
┡━━━━━━━━━━━┩
│ string │
├───────────┤
│ a\t │
│ b\n │
│ c\t │
└───────────┘
"""
return ops.LStrip(self).to_expr()
def rstrip(self) -> StringValue:
r"""Remove whitespace from the right side of string.
Returns
-------
StringValue
Right-stripped string
Examples
--------
>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"s": ["\ta\t", "\nb\n", "\vc\t"]})
>>> t
┏━━━━━━━━┓
┃ s ┃
┡━━━━━━━━┩
│ string │
├────────┤
│ \ta\t │
│ \nb\n │
│ \vc\t │
└────────┘
>>> t.s.rstrip()
┏━━━━━━━━━━━┓
┃ RStrip(s) ┃
┡━━━━━━━━━━━┩
│ string │
├───────────┤
│ \ta │
│ \nb │
│ \vc │
└───────────┘
"""
return ops.RStrip(self).to_expr()
def capitalize(self) -> StringValue:
"""Uppercase the first letter, lowercase the rest.
This API matches the semantics of the Python [](`str.capitalize`)
method.
Returns
-------
StringValue
Capitalized string
Examples
--------
>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"s": ["aBC", " abc", "ab cd", None]})
>>> t.s.capitalize()
┏━━━━━━━━━━━━━━━┓
┃ Capitalize(s) ┃
┡━━━━━━━━━━━━━━━┩
│ string │
├───────────────┤
│ Abc │
│ abc │
│ Ab cd │
│ NULL │
└───────────────┘
"""
return ops.Capitalize(self).to_expr()
initcap = capitalize
@util.deprecated(
instead="use the `capitalize` method", as_of="9.0", removed_in="10.0"
)
def initcap(self) -> StringValue:
"""Deprecated. Use `capitalize` instead."""
return self.capitalize()
def __contains__(self, *_: Any) -> bool:
raise TypeError("Use string_expr.contains(arg)")
def contains(self, substr: str | StringValue) -> ir.BooleanValue:
"""Return whether the expression contains `substr`.
Parameters
----------
substr
Substring for which to check
Returns
-------
BooleanValue
Boolean indicating the presence of `substr` in the expression
Examples
--------
>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"s": ["bab", "ddd", "eaf"]})
>>> t.s.contains("a")
┏━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ StringContains(s, 'a') ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━┩
│ boolean │
├────────────────────────┤
│ True │
│ False │
│ True │
└────────────────────────┘
"""
return ops.StringContains(self, substr).to_expr()
def hashbytes(
self,
how: Literal["md5", "sha1", "sha256", "sha512"] = "sha256",
) -> ir.BinaryValue:
"""Compute the binary hash value of the input.
Parameters
----------
how
Hash algorithm to use
Returns
-------
BinaryValue
Binary expression
"""
return ops.HashBytes(self, how).to_expr()
def hexdigest(
self,
how: Literal["md5", "sha1", "sha256", "sha512"] = "sha256",
) -> ir.StringValue:
"""Return the hash digest of the input as a hex encoded string.
Parameters
----------
how
Hash algorithm to use
Returns
-------
StringValue
Hexadecimal representation of the hash as a string
Examples
--------
>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"species": ["Adelie", "Chinstrap", "Gentoo"]})
>>> t.species.hexdigest()
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ HexDigest(species) ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ string │
├──────────────────────────────────────────────────────────────────┤
│ a4d7d46b27480037bc1e513e0e157cbf258baae6ee69e3110d0f9ff418b57a3c │
│ cb97d113ca69899ae4f1fb581f4a90d86989db77b4a33873d604b0ee412b4cc9 │
│ b5e90cdff65949fe6bc226823245f7698110e563a12363fc57b3eed3e4a0a612 │
└──────────────────────────────────────────────────────────────────┘
"""
return ops.HexDigest(self, how.lower()).to_expr()
def substr(
self,
start: int | ir.IntegerValue,
length: int | ir.IntegerValue | None = None,
) -> StringValue:
"""Extract a substring.
Parameters
----------
start
First character to start splitting, indices start at 0
length
Maximum length of each substring. If not supplied, searches the
entire string
Returns
-------
StringValue
Found substring
Examples
--------
>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"s": ["abc", "defg", "hijlk"]})
>>> t.s.substr(2)
┏━━━━━━━━━━━━━━━━━┓
┃ Substring(s, 2) ┃
┡━━━━━━━━━━━━━━━━━┩
│ string │
├─────────────────┤
│ c │
│ fg │
│ jlk │
└─────────────────┘
"""
return ops.Substring(self, start, length).to_expr()
def left(self, nchars: int | ir.IntegerValue) -> StringValue:
"""Return the `nchars` left-most characters.
Parameters
----------
nchars
Maximum number of characters to return
Returns
-------
StringValue
Characters from the start
Examples
--------
>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"s": ["abc", "defg", "hijlk"]})
>>> t.s.left(2)
┏━━━━━━━━━━━━━━━━━━━━┓
┃ Substring(s, 0, 2) ┃
┡━━━━━━━━━━━━━━━━━━━━┩
│ string │
├────────────────────┤
│ ab │
│ de │
│ hi │
└────────────────────┘
"""
return self.substr(0, length=nchars)
def right(self, nchars: int | ir.IntegerValue) -> StringValue:
"""Return up to `nchars` from the end of each string.
Parameters
----------
nchars
Maximum number of characters to return
Returns
-------
StringValue
Characters from the end
Examples
--------
>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"s": ["abc", "defg", "hijlk"]})
>>> t.s.right(2)
┏━━━━━━━━━━━━━━━━┓
┃ StrRight(s, 2) ┃
┡━━━━━━━━━━━━━━━━┩
│ string │
├────────────────┤
│ bc │
│ fg │
│ lk │
└────────────────┘
"""
return ops.StrRight(self, nchars).to_expr()
def repeat(self, n: int | ir.IntegerValue) -> StringValue:
"""Repeat a string `n` times.
Parameters
----------
n
Number of repetitions
Returns
-------
StringValue
Repeated string
Examples
--------
>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"s": ["a", "bb", "c"]})
>>> t.s.repeat(5)
┏━━━━━━━━━━━━━━┓
┃ Repeat(s, 5) ┃
┡━━━━━━━━━━━━━━┩
│ string │
├──────────────┤
│ aaaaa │
│ bbbbbbbbbb │
│ ccccc │
└──────────────┘
"""
return ops.Repeat(self, n).to_expr()
__mul__ = __rmul__ = repeat
def translate(self, from_str: StringValue, to_str: StringValue) -> StringValue:
"""Replace `from_str` characters in `self` characters in `to_str`.
To avoid unexpected behavior, `from_str` should be shorter than
`to_str`.
Parameters
----------
from_str
Characters in `arg` to replace
to_str
Characters to use for replacement
Returns
-------
StringValue
Translated string
Examples
--------
>>> import ibis
>>> table = ibis.table(dict(string_col="string"))
>>> result = table.string_col.translate("a", "b")
"""
return ops.Translate(self, from_str, to_str).to_expr()
def find(
self,
substr: str | StringValue,
start: int | ir.IntegerValue | None = None,
end: int | ir.IntegerValue | None = None,
) -> ir.IntegerValue:
"""Return the position of the first occurrence of substring.
Parameters
----------
substr
Substring to search for
start
Zero based index of where to start the search
end
Zero based index of where to stop the search. Currently not
implemented.
Returns
-------
IntegerValue
Position of `substr` in `arg` starting from `start`
Examples
--------
>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"s": ["abc", "bac", "bca"]})
>>> t.s.find("a")
┏━━━━━━━━━━━━━━━━━━━━┓
┃ StringFind(s, 'a') ┃
┡━━━━━━━━━━━━━━━━━━━━┩
│ int64 │
├────────────────────┤
│ 0 │
│ 1 │
│ 2 │
└────────────────────┘
>>> t.s.find("z")
┏━━━━━━━━━━━━━━━━━━━━┓
┃ StringFind(s, 'z') ┃
┡━━━━━━━━━━━━━━━━━━━━┩
│ int64 │
├────────────────────┤
│ -1 │
│ -1 │
│ -1 │
└────────────────────┘
"""
if end is not None:
raise NotImplementedError("`end` parameter is not yet implemented")
return ops.StringFind(self, substr, start, end).to_expr()
def lpad(
self,
length: int | ir.IntegerValue,
pad: str | StringValue = " ",
) -> StringValue:
"""Pad `arg` by truncating on the right or padding on the left.
Parameters
----------
length
Length of output string
pad
Pad character
Returns
-------
StringValue
Left-padded string
Examples
--------
>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"s": ["abc", "def", "ghij"]})
>>> t.s.lpad(5, "-")
┏━━━━━━━━━━━━━━━━━┓
┃ LPad(s, 5, '-') ┃
┡━━━━━━━━━━━━━━━━━┩
│ string │
├─────────────────┤
│ --abc │
│ --def │
│ -ghij │
└─────────────────┘
"""
return ops.LPad(self, length, pad).to_expr()
def rpad(
self,
length: int | ir.IntegerValue,
pad: str | StringValue = " ",
) -> StringValue:
"""Pad `self` by truncating or padding on the right.
Parameters
----------
self
String to pad
length
Length of output string
pad
Pad character
Returns
-------
StringValue
Right-padded string
Examples
--------
>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"s": ["abc", "def", "ghij"]})
>>> t.s.rpad(5, "-")
┏━━━━━━━━━━━━━━━━━┓
┃ RPad(s, 5, '-') ┃
┡━━━━━━━━━━━━━━━━━┩
│ string │
├─────────────────┤
│ abc-- │
│ def-- │
│ ghij- │
└─────────────────┘
"""
return ops.RPad(self, length, pad).to_expr()
def find_in_set(self, str_list: Sequence[str]) -> ir.IntegerValue:
"""Find the first occurrence of `str_list` within a list of strings.
No string in `str_list` can have a comma.
Parameters
----------
str_list
Sequence of strings
Returns
-------
IntegerValue
Position of `str_list` in `self`. Returns -1 if `self` isn't found
or if `self` contains `','`.
Examples
--------
>>> import ibis
>>> table = ibis.table(dict(string_col="string"))
>>> result = table.string_col.find_in_set(["a", "b"])
"""
return ops.FindInSet(self, str_list).to_expr()
def join(self, strings: Sequence[str | StringValue] | ir.ArrayValue) -> StringValue:
"""Join a list of strings using `self` as the separator.
Parameters
----------
strings
Strings to join with `arg`
Returns
-------
StringValue
Joined string
Examples
--------
>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"arr": [["a", "b", "c"], None, [], ["b", None]]})
>>> t
┏━━━━━━━━━━━━━━━━━━━━━━┓
┃ arr ┃
┡━━━━━━━━━━━━━━━━━━━━━━┩
│ array<string> │
├──────────────────────┤
│ ['a', 'b', ... +1] │
│ NULL │
│ [] │
│ ['b', None] │
└──────────────────────┘
>>> ibis.literal("|").join(t.arr)
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ ArrayStringJoin(arr, '|') ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ string │
├───────────────────────────┤
│ a|b|c │
│ NULL │
│ NULL │
│ b │
└───────────────────────────┘
See Also
--------
[`ArrayValue.join`](./expression-collections.qmd#ibis.expr.types.arrays.ArrayValue.join)
"""
import ibis.expr.types as ir
if isinstance(strings, ir.ArrayValue):
cls = ops.ArrayStringJoin
else:
cls = ops.StringJoin
return cls(strings, sep=self).to_expr()
def startswith(self, start: str | StringValue) -> ir.BooleanValue:
"""Determine whether `self` starts with `end`.
Parameters
----------
start
prefix to check for
Returns
-------
BooleanValue
Boolean indicating whether `self` starts with `start`
Examples
--------
>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"s": ["Ibis project", "GitHub"]})
>>> t.s.startswith("Ibis")
┏━━━━━━━━━━━━━━━━━━━━━━━┓
┃ StartsWith(s, 'Ibis') ┃
┡━━━━━━━━━━━━━━━━━━━━━━━┩
│ boolean │
├───────────────────────┤
│ True │
│ False │
└───────────────────────┘
"""
return ops.StartsWith(self, start).to_expr()
def endswith(self, end: str | StringValue) -> ir.BooleanValue:
"""Determine if `self` ends with `end`.
Parameters
----------
end
Suffix to check for
Returns
-------
BooleanValue
Boolean indicating whether `self` ends with `end`
Examples
--------
>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"s": ["Ibis project", "GitHub"]})
>>> t.s.endswith("project")
┏━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ EndsWith(s, 'project') ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━┩
│ boolean │
├────────────────────────┤
│ True │
│ False │
└────────────────────────┘
"""
return ops.EndsWith(self, end).to_expr()
def like(
self,
patterns: str | StringValue | Iterable[str | StringValue],
) -> ir.BooleanValue:
"""Match `patterns` against `self`, case-sensitive.
This function is modeled after the SQL `LIKE` directive. Use `%` as a
multiple-character wildcard or `_` as a single-character wildcard.
Use `re_search` or `rlike` for regular expression-based matching.
Parameters
----------
patterns
If `pattern` is a list, then if any pattern matches the input then
the corresponding row in the output is `True`.
Returns
-------
BooleanValue
Column indicating matches
Examples
--------
>>> import ibis
>>> ibis.options.interactive = True
>>> t = ibis.memtable({"s": ["Ibis project", "GitHub"]})
>>> t.s.like("%project")
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ StringSQLLike(s, '%project') ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ boolean │
├──────────────────────────────┤
│ True │
│ False │
└──────────────────────────────┘
"""
return functools.reduce(
operator.or_,
(
ops.StringSQLLike(self, pattern).to_expr()
for pattern in util.promote_list(patterns)
),
)
def ilike(
self,
patterns: str | StringValue | Iterable[str | StringValue],
) -> ir.BooleanValue:
"""Match `patterns` against `self`, case-insensitive.
This function is modeled after SQL's `ILIKE` directive. Use `%` as a
multiple-character wildcard or `_` as a single-character wildcard.
Use `re_search` or `rlike` for regular expression-based matching.
Parameters
----------
patterns
If `pattern` is a list, then if any pattern matches the input then
the corresponding row in the output is `True`.
Returns
-------
BooleanValue
Column indicating matches
Examples
--------
>>> import ibis