-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
array.py
781 lines (690 loc) · 27 KB
/
array.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
from __future__ import annotations
from typing import TYPE_CHECKING, Callable, Sequence
from polars._utils.parse_expr_input import parse_as_expression
from polars._utils.wrap import wrap_expr
if TYPE_CHECKING:
from datetime import date, datetime, time
from polars import Expr
from polars.type_aliases import IntoExpr, IntoExprColumn
class ExprArrayNameSpace:
"""Namespace for array related expressions."""
_accessor = "arr"
def __init__(self, expr: Expr):
self._pyexpr = expr._pyexpr
def min(self) -> Expr:
"""
Compute the min values of the sub-arrays.
Examples
--------
>>> df = pl.DataFrame(
... data={"a": [[1, 2], [4, 3]]},
... schema={"a": pl.Array(pl.Int64, 2)},
... )
>>> df.select(pl.col("a").arr.min())
shape: (2, 1)
┌─────┐
│ a │
│ --- │
│ i64 │
╞═════╡
│ 1 │
│ 3 │
└─────┘
"""
return wrap_expr(self._pyexpr.arr_min())
def max(self) -> Expr:
"""
Compute the max values of the sub-arrays.
Examples
--------
>>> df = pl.DataFrame(
... data={"a": [[1, 2], [4, 3]]},
... schema={"a": pl.Array(pl.Int64, 2)},
... )
>>> df.select(pl.col("a").arr.max())
shape: (2, 1)
┌─────┐
│ a │
│ --- │
│ i64 │
╞═════╡
│ 2 │
│ 4 │
└─────┘
"""
return wrap_expr(self._pyexpr.arr_max())
def sum(self) -> Expr:
"""
Compute the sum values of the sub-arrays.
Examples
--------
>>> df = pl.DataFrame(
... data={"a": [[1, 2], [4, 3]]},
... schema={"a": pl.Array(pl.Int64, 2)},
... )
>>> df.select(pl.col("a").arr.sum())
shape: (2, 1)
┌─────┐
│ a │
│ --- │
│ i64 │
╞═════╡
│ 3 │
│ 7 │
└─────┘
"""
return wrap_expr(self._pyexpr.arr_sum())
def std(self, ddof: int = 1) -> Expr:
"""
Compute the std of the values of the sub-arrays.
Examples
--------
>>> df = pl.DataFrame(
... data={"a": [[1, 2], [4, 3]]},
... schema={"a": pl.Array(pl.Int64, 2)},
... )
>>> df.select(pl.col("a").arr.std())
shape: (2, 1)
┌──────────┐
│ a │
│ --- │
│ f64 │
╞══════════╡
│ 0.707107 │
│ 0.707107 │
└──────────┘
"""
return wrap_expr(self._pyexpr.arr_std(ddof))
def var(self, ddof: int = 1) -> Expr:
"""
Compute the var of the values of the sub-arrays.
Examples
--------
>>> df = pl.DataFrame(
... data={"a": [[1, 2], [4, 3]]},
... schema={"a": pl.Array(pl.Int64, 2)},
... )
>>> df.select(pl.col("a").arr.var())
shape: (2, 1)
┌─────┐
│ a │
│ --- │
│ f64 │
╞═════╡
│ 0.5 │
│ 0.5 │
└─────┘
"""
return wrap_expr(self._pyexpr.arr_var(ddof))
def median(self) -> Expr:
"""
Compute the median of the values of the sub-arrays.
Examples
--------
>>> df = pl.DataFrame(
... data={"a": [[1, 2], [4, 3]]},
... schema={"a": pl.Array(pl.Int64, 2)},
... )
>>> df.select(pl.col("a").arr.median())
shape: (2, 1)
┌─────┐
│ a │
│ --- │
│ f64 │
╞═════╡
│ 1.5 │
│ 3.5 │
└─────┘
"""
return wrap_expr(self._pyexpr.arr_median())
def unique(self, *, maintain_order: bool = False) -> Expr:
"""
Get the unique/distinct values in the array.
Parameters
----------
maintain_order
Maintain order of data. This requires more work.
Examples
--------
>>> df = pl.DataFrame(
... {
... "a": [[1, 1, 2]],
... },
... schema={"a": pl.Array(pl.Int64, 3)},
... )
>>> df.select(pl.col("a").arr.unique())
shape: (1, 1)
┌───────────┐
│ a │
│ --- │
│ list[i64] │
╞═══════════╡
│ [1, 2] │
└───────────┘
"""
return wrap_expr(self._pyexpr.arr_unique(maintain_order))
def n_unique(self) -> Expr:
"""
Count the number of unique values in every sub-arrays.
Examples
--------
>>> df = pl.DataFrame(
... {
... "a": [[1, 1, 2], [2, 3, 4]],
... },
... schema={"a": pl.Array(pl.Int64, 3)},
... )
>>> df.with_columns(n_unique=pl.col("a").arr.n_unique())
shape: (2, 2)
┌───────────────┬──────────┐
│ a ┆ n_unique │
│ --- ┆ --- │
│ array[i64, 3] ┆ u32 │
╞═══════════════╪══════════╡
│ [1, 1, 2] ┆ 2 │
│ [2, 3, 4] ┆ 3 │
└───────────────┴──────────┘
"""
return wrap_expr(self._pyexpr.arr_n_unique())
def to_list(self) -> Expr:
"""
Convert an Array column into a List column with the same inner data type.
Returns
-------
Expr
Expression of data type :class:`List`.
Examples
--------
>>> df = pl.DataFrame(
... data={"a": [[1, 2], [3, 4]]},
... schema={"a": pl.Array(pl.Int8, 2)},
... )
>>> df.select(pl.col("a").arr.to_list())
shape: (2, 1)
┌──────────┐
│ a │
│ --- │
│ list[i8] │
╞══════════╡
│ [1, 2] │
│ [3, 4] │
└──────────┘
"""
return wrap_expr(self._pyexpr.arr_to_list())
def any(self) -> Expr:
"""
Evaluate whether any boolean value is true for every subarray.
Examples
--------
>>> df = pl.DataFrame(
... data={
... "a": [
... [True, True],
... [False, True],
... [False, False],
... [None, None],
... None,
... ]
... },
... schema={"a": pl.Array(pl.Boolean, 2)},
... )
>>> df.with_columns(any=pl.col("a").arr.any())
shape: (5, 2)
┌────────────────┬───────┐
│ a ┆ any │
│ --- ┆ --- │
│ array[bool, 2] ┆ bool │
╞════════════════╪═══════╡
│ [true, true] ┆ true │
│ [false, true] ┆ true │
│ [false, false] ┆ false │
│ [null, null] ┆ false │
│ null ┆ null │
└────────────────┴───────┘
"""
return wrap_expr(self._pyexpr.arr_any())
def all(self) -> Expr:
"""
Evaluate whether all boolean values are true for every subarray.
Examples
--------
>>> df = pl.DataFrame(
... data={
... "a": [
... [True, True],
... [False, True],
... [False, False],
... [None, None],
... None,
... ]
... },
... schema={"a": pl.Array(pl.Boolean, 2)},
... )
>>> df.with_columns(all=pl.col("a").arr.all())
shape: (5, 2)
┌────────────────┬───────┐
│ a ┆ all │
│ --- ┆ --- │
│ array[bool, 2] ┆ bool │
╞════════════════╪═══════╡
│ [true, true] ┆ true │
│ [false, true] ┆ false │
│ [false, false] ┆ false │
│ [null, null] ┆ true │
│ null ┆ null │
└────────────────┴───────┘
"""
return wrap_expr(self._pyexpr.arr_all())
def sort(self, *, descending: bool = False, nulls_last: bool = False) -> Expr:
"""
Sort the arrays in this column.
Parameters
----------
descending
Sort in descending order.
nulls_last
Place null values last.
Examples
--------
>>> df = pl.DataFrame(
... {
... "a": [[3, 2, 1], [9, 1, 2]],
... },
... schema={"a": pl.Array(pl.Int64, 3)},
... )
>>> df.with_columns(sort=pl.col("a").arr.sort())
shape: (2, 2)
┌───────────────┬───────────────┐
│ a ┆ sort │
│ --- ┆ --- │
│ array[i64, 3] ┆ array[i64, 3] │
╞═══════════════╪═══════════════╡
│ [3, 2, 1] ┆ [1, 2, 3] │
│ [9, 1, 2] ┆ [1, 2, 9] │
└───────────────┴───────────────┘
>>> df.with_columns(sort=pl.col("a").arr.sort(descending=True))
shape: (2, 2)
┌───────────────┬───────────────┐
│ a ┆ sort │
│ --- ┆ --- │
│ array[i64, 3] ┆ array[i64, 3] │
╞═══════════════╪═══════════════╡
│ [3, 2, 1] ┆ [3, 2, 1] │
│ [9, 1, 2] ┆ [9, 2, 1] │
└───────────────┴───────────────┘
"""
return wrap_expr(self._pyexpr.arr_sort(descending, nulls_last))
def reverse(self) -> Expr:
"""
Reverse the arrays in this column.
Examples
--------
>>> df = pl.DataFrame(
... {
... "a": [[3, 2, 1], [9, 1, 2]],
... },
... schema={"a": pl.Array(pl.Int64, 3)},
... )
>>> df.with_columns(reverse=pl.col("a").arr.reverse())
shape: (2, 2)
┌───────────────┬───────────────┐
│ a ┆ reverse │
│ --- ┆ --- │
│ array[i64, 3] ┆ array[i64, 3] │
╞═══════════════╪═══════════════╡
│ [3, 2, 1] ┆ [1, 2, 3] │
│ [9, 1, 2] ┆ [2, 1, 9] │
└───────────────┴───────────────┘
"""
return wrap_expr(self._pyexpr.arr_reverse())
def arg_min(self) -> Expr:
"""
Retrieve the index of the minimal value in every sub-array.
Returns
-------
Expr
Expression of data type :class:`UInt32` or :class:`UInt64`
(depending on compilation).
Examples
--------
>>> df = pl.DataFrame(
... {
... "a": [[1, 2], [2, 1]],
... },
... schema={"a": pl.Array(pl.Int64, 2)},
... )
>>> df.with_columns(arg_min=pl.col("a").arr.arg_min())
shape: (2, 2)
┌───────────────┬─────────┐
│ a ┆ arg_min │
│ --- ┆ --- │
│ array[i64, 2] ┆ u32 │
╞═══════════════╪═════════╡
│ [1, 2] ┆ 0 │
│ [2, 1] ┆ 1 │
└───────────────┴─────────┘
"""
return wrap_expr(self._pyexpr.arr_arg_min())
def arg_max(self) -> Expr:
"""
Retrieve the index of the maximum value in every sub-array.
Returns
-------
Expr
Expression of data type :class:`UInt32` or :class:`UInt64`
(depending on compilation).
Examples
--------
>>> df = pl.DataFrame(
... {
... "a": [[1, 2], [2, 1]],
... },
... schema={"a": pl.Array(pl.Int64, 2)},
... )
>>> df.with_columns(arg_max=pl.col("a").arr.arg_max())
shape: (2, 2)
┌───────────────┬─────────┐
│ a ┆ arg_max │
│ --- ┆ --- │
│ array[i64, 2] ┆ u32 │
╞═══════════════╪═════════╡
│ [1, 2] ┆ 1 │
│ [2, 1] ┆ 0 │
└───────────────┴─────────┘
"""
return wrap_expr(self._pyexpr.arr_arg_max())
def get(self, index: int | IntoExprColumn, *, null_on_oob: bool = True) -> Expr:
"""
Get the value by index in the sub-arrays.
So index `0` would return the first item of every sublist
and index `-1` would return the last item of every sublist
if an index is out of bounds, it will return a `None`.
Parameters
----------
index
Index to return per sub-array
null_on_oob
Behavior if an index is out of bounds:
True -> set as null
False -> raise an error
Examples
--------
>>> df = pl.DataFrame(
... {"arr": [[1, 2, 3], [4, 5, 6], [7, 8, 9]], "idx": [1, -2, 0]},
... schema={"arr": pl.Array(pl.Int32, 3), "idx": pl.Int32},
... )
>>> df.with_columns(get=pl.col("arr").arr.get("idx", null_on_oob=True))
shape: (3, 3)
┌───────────────┬─────┬─────┐
│ arr ┆ idx ┆ get │
│ --- ┆ --- ┆ --- │
│ array[i32, 3] ┆ i32 ┆ i32 │
╞═══════════════╪═════╪═════╡
│ [1, 2, 3] ┆ 1 ┆ 2 │
│ [4, 5, 6] ┆ -2 ┆ 5 │
│ [7, 8, 9] ┆ 0 ┆ 7 │
└───────────────┴─────┴─────┘
"""
index = parse_as_expression(index)
return wrap_expr(self._pyexpr.arr_get(index, null_on_oob))
def first(self) -> Expr:
"""
Get the first value of the sub-arrays.
Examples
--------
>>> df = pl.DataFrame(
... {"a": [[1, 2, 3], [4, 5, 6], [7, 8, 9]]},
... schema={"a": pl.Array(pl.Int32, 3)},
... )
>>> df.with_columns(first=pl.col("a").arr.first())
shape: (3, 2)
┌───────────────┬───────┐
│ a ┆ first │
│ --- ┆ --- │
│ array[i32, 3] ┆ i32 │
╞═══════════════╪═══════╡
│ [1, 2, 3] ┆ 1 │
│ [4, 5, 6] ┆ 4 │
│ [7, 8, 9] ┆ 7 │
└───────────────┴───────┘
"""
return self.get(0)
def last(self) -> Expr:
"""
Get the last value of the sub-arrays.
Examples
--------
>>> df = pl.DataFrame(
... {"a": [[1, 2, 3], [4, 5, 6], [7, 8, 9]]},
... schema={"a": pl.Array(pl.Int32, 3)},
... )
>>> df.with_columns(last=pl.col("a").arr.last())
shape: (3, 2)
┌───────────────┬──────┐
│ a ┆ last │
│ --- ┆ --- │
│ array[i32, 3] ┆ i32 │
╞═══════════════╪══════╡
│ [1, 2, 3] ┆ 3 │
│ [4, 5, 6] ┆ 6 │
│ [7, 8, 9] ┆ 9 │
└───────────────┴──────┘
"""
return self.get(-1)
def join(self, separator: IntoExprColumn, *, ignore_nulls: bool = True) -> Expr:
"""
Join all string items in a sub-array and place a separator between them.
This errors if inner type of array `!= String`.
Parameters
----------
separator
string to separate the items with
ignore_nulls
Ignore null values (default).
If set to ``False``, null values will be propagated.
If the sub-list contains any null values, the output is ``None``.
Returns
-------
Expr
Expression of data type :class:`String`.
Examples
--------
>>> df = pl.DataFrame(
... {"s": [["a", "b"], ["x", "y"]], "separator": ["*", "_"]},
... schema={
... "s": pl.Array(pl.String, 2),
... "separator": pl.String,
... },
... )
>>> df.with_columns(join=pl.col("s").arr.join(pl.col("separator")))
shape: (2, 3)
┌───────────────┬───────────┬──────┐
│ s ┆ separator ┆ join │
│ --- ┆ --- ┆ --- │
│ array[str, 2] ┆ str ┆ str │
╞═══════════════╪═══════════╪══════╡
│ ["a", "b"] ┆ * ┆ a*b │
│ ["x", "y"] ┆ _ ┆ x_y │
└───────────────┴───────────┴──────┘
"""
separator = parse_as_expression(separator, str_as_lit=True)
return wrap_expr(self._pyexpr.arr_join(separator, ignore_nulls))
def explode(self) -> Expr:
"""
Returns a column with a separate row for every array element.
Returns
-------
Expr
Expression with the data type of the array elements.
Examples
--------
>>> df = pl.DataFrame(
... {"a": [[1, 2, 3], [4, 5, 6]]}, schema={"a": pl.Array(pl.Int64, 3)}
... )
>>> df.select(pl.col("a").arr.explode())
shape: (6, 1)
┌─────┐
│ a │
│ --- │
│ i64 │
╞═════╡
│ 1 │
│ 2 │
│ 3 │
│ 4 │
│ 5 │
│ 6 │
└─────┘
"""
return wrap_expr(self._pyexpr.explode())
def contains(
self, item: float | str | bool | int | date | datetime | time | IntoExprColumn
) -> Expr:
"""
Check if sub-arrays contain the given item.
Parameters
----------
item
Item that will be checked for membership
Returns
-------
Expr
Expression of data type :class:`Boolean`.
Examples
--------
>>> df = pl.DataFrame(
... {"a": [["a", "b"], ["x", "y"], ["a", "c"]]},
... schema={"a": pl.Array(pl.String, 2)},
... )
>>> df.with_columns(contains=pl.col("a").arr.contains("a"))
shape: (3, 2)
┌───────────────┬──────────┐
│ a ┆ contains │
│ --- ┆ --- │
│ array[str, 2] ┆ bool │
╞═══════════════╪══════════╡
│ ["a", "b"] ┆ true │
│ ["x", "y"] ┆ false │
│ ["a", "c"] ┆ true │
└───────────────┴──────────┘
"""
item = parse_as_expression(item, str_as_lit=True)
return wrap_expr(self._pyexpr.arr_contains(item))
def count_matches(self, element: IntoExpr) -> Expr:
"""
Count how often the value produced by `element` occurs.
Parameters
----------
element
An expression that produces a single value
Examples
--------
>>> df = pl.DataFrame(
... {"a": [[1, 2], [1, 1], [2, 2]]}, schema={"a": pl.Array(pl.Int64, 2)}
... )
>>> df.with_columns(number_of_twos=pl.col("a").arr.count_matches(2))
shape: (3, 2)
┌───────────────┬────────────────┐
│ a ┆ number_of_twos │
│ --- ┆ --- │
│ array[i64, 2] ┆ u32 │
╞═══════════════╪════════════════╡
│ [1, 2] ┆ 1 │
│ [1, 1] ┆ 0 │
│ [2, 2] ┆ 2 │
└───────────────┴────────────────┘
"""
element = parse_as_expression(element, str_as_lit=True)
return wrap_expr(self._pyexpr.arr_count_matches(element))
def to_struct(
self, fields: Sequence[str] | Callable[[int], str] | None = None
) -> Expr:
"""
Convert the Series of type `Array` to a Series of type `Struct`.
Parameters
----------
fields
If the name and number of the desired fields is known in advance
a list of field names can be given, which will be assigned by index.
Otherwise, to dynamically assign field names, a custom function can be
used; if neither are set, fields will be `field_0, field_1 .. field_n`.
Examples
--------
Convert array to struct with default field name assignment:
>>> df = pl.DataFrame(
... {"n": [[0, 1, 2], [3, 4, 5]]}, schema={"n": pl.Array(pl.Int8, 3)}
... )
>>> df.with_columns(struct=pl.col("n").arr.to_struct())
shape: (2, 2)
┌──────────────┬───────────┐
│ n ┆ struct │
│ --- ┆ --- │
│ array[i8, 3] ┆ struct[3] │
╞══════════════╪═══════════╡
│ [0, 1, 2] ┆ {0,1,2} │
│ [3, 4, 5] ┆ {3,4,5} │
└──────────────┴───────────┘
Convert array to struct with field name assignment by function/index:
>>> df = pl.DataFrame(
... {"n": [[0, 1, 2], [3, 4, 5]]}, schema={"n": pl.Array(pl.Int8, 3)}
... )
>>> df.select(pl.col("n").arr.to_struct(fields=lambda idx: f"n{idx}")).rows(
... named=True
... )
[{'n': {'n0': 0, 'n1': 1, 'n2': 2}}, {'n': {'n0': 3, 'n1': 4, 'n2': 5}}]
Convert array to struct with field name assignment by
index from a list of names:
>>> df.select(pl.col("n").arr.to_struct(fields=["c1", "c2", "c3"])).rows(
... named=True
... )
[{'n': {'c1': 0, 'c2': 1, 'c3': 2}}, {'n': {'c1': 3, 'c2': 4, 'c3': 5}}]
"""
if isinstance(fields, Sequence):
field_names = list(fields)
pyexpr = self._pyexpr.arr_to_struct(None)
return wrap_expr(pyexpr).struct.rename_fields(field_names)
else:
pyexpr = self._pyexpr.arr_to_struct(fields)
return wrap_expr(pyexpr)
def shift(self, n: int | IntoExprColumn = 1) -> Expr:
"""
Shift array values by the given number of indices.
Parameters
----------
n
Number of indices to shift forward. If a negative value is passed, values
are shifted in the opposite direction instead.
Notes
-----
This method is similar to the `LAG` operation in SQL when the value for `n`
is positive. With a negative value for `n`, it is similar to `LEAD`.
Examples
--------
By default, array values are shifted forward by one index.
>>> df = pl.DataFrame(
... {"a": [[1, 2, 3], [4, 5, 6]]}, schema={"a": pl.Array(pl.Int64, 3)}
... )
>>> df.with_columns(shift=pl.col("a").arr.shift())
shape: (2, 2)
┌───────────────┬───────────────┐
│ a ┆ shift │
│ --- ┆ --- │
│ array[i64, 3] ┆ array[i64, 3] │
╞═══════════════╪═══════════════╡
│ [1, 2, 3] ┆ [null, 1, 2] │
│ [4, 5, 6] ┆ [null, 4, 5] │
└───────────────┴───────────────┘
Pass a negative value to shift in the opposite direction instead.
>>> df.with_columns(shift=pl.col("a").arr.shift(-2))
shape: (2, 2)
┌───────────────┬─────────────────┐
│ a ┆ shift │
│ --- ┆ --- │
│ array[i64, 3] ┆ array[i64, 3] │
╞═══════════════╪═════════════════╡
│ [1, 2, 3] ┆ [3, null, null] │
│ [4, 5, 6] ┆ [6, null, null] │
└───────────────┴─────────────────┘
"""
n = parse_as_expression(n)
return wrap_expr(self._pyexpr.arr_shift(n))