-
Notifications
You must be signed in to change notification settings - Fork 111
/
lazy_series.ex
1110 lines (867 loc) · 26.8 KB
/
lazy_series.ex
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
defmodule Explorer.Backend.LazySeries do
@moduledoc """
This is an opaque implementation of a Series.
It represents an operation with its arguments.
"""
alias Explorer.Series
alias Explorer.Backend
@behaviour Explorer.Backend.Series
defstruct op: nil, args: [], dtype: nil, aggregation: false
@type t :: %__MODULE__{op: atom(), args: list(), dtype: any(), aggregation: boolean()}
@operations [
# Element-wise
all_equal: 2,
equal: 2,
not_equal: 2,
greater: 2,
greater_equal: 2,
less: 2,
less_equal: 2,
is_nil: 1,
is_not_nil: 1,
is_finite: 1,
is_infinite: 1,
is_nan: 1,
binary_and: 2,
binary_or: 2,
binary_in: 2,
add: 2,
subtract: 2,
multiply: 2,
divide: 2,
quotient: 2,
remainder: 2,
pow: 2,
log: 1,
log: 2,
exp: 1,
fill_missing_with_value: 2,
fill_missing_with_strategy: 2,
format: 1,
concat: 1,
coalesce: 2,
cast: 2,
select: 3,
abs: 1,
strptime: 2,
strftime: 2,
clip_float: 3,
clip_integer: 3,
# Trigonometric functions
acos: 1,
asin: 1,
atan: 1,
cos: 1,
sin: 1,
tan: 1,
# Window functions
cumulative_max: 2,
cumulative_min: 2,
cumulative_sum: 2,
cumulative_product: 2,
window_max: 5,
window_mean: 5,
window_median: 5,
window_min: 5,
window_sum: 5,
window_standard_deviation: 5,
ewm_mean: 5,
ewm_standard_deviation: 6,
ewm_variance: 6,
# Transformation
column: 1,
reverse: 1,
argsort: 5,
sort: 5,
distinct: 1,
unordered_distinct: 1,
slice: 2,
slice: 3,
sample_n: 5,
sample_frac: 5,
head: 2,
tail: 2,
shift: 3,
peaks: 2,
unary_not: 1,
# Aggregations
sum: 1,
min: 1,
max: 1,
argmin: 1,
argmax: 1,
mean: 1,
median: 1,
mode: 1,
n_distinct: 1,
variance: 2,
standard_deviation: 2,
quantile: 2,
rank: 4,
product: 1,
first: 1,
last: 1,
count: 1,
nil_count: 1,
skew: 2,
correlation: 4,
covariance: 3,
all: 1,
any: 1,
# Strings
contains: 2,
replace: 3,
lstrip: 2,
rstrip: 2,
strip: 2,
upcase: 1,
downcase: 1,
substring: 3,
split: 2,
# Float round
round: 2,
floor: 1,
ceil: 1,
# Date functions
day_of_week: 1,
day_of_year: 1,
week_of_year: 1,
month: 1,
year: 1,
hour: 1,
minute: 1,
second: 1,
# List functions
join: 2,
lengths: 1,
member: 3
]
@comparison_operations [:equal, :not_equal, :greater, :greater_equal, :less, :less_equal]
@basic_arithmetic_operations [:add, :subtract, :multiply, :divide, :pow]
@other_arithmetic_operations [:quotient, :remainder]
@aggregation_operations [
:sum,
:min,
:max,
:argmin,
:argmax,
:mean,
:median,
:mode,
:count,
:product,
:nil_count,
:size,
:first,
:last,
:n_distinct
]
@window_fun_operations [
:window_max,
:window_mean,
:window_median,
:window_min,
:window_sum
]
@cumulative_operations [
:cumulative_max,
:cumulative_min,
:cumulative_sum,
:cumulative_product
]
@float_predicates [:is_finite, :is_infinite, :is_nan]
@doc false
def new(op, args, dtype, aggregation \\ false) do
%__MODULE__{op: op, args: args, dtype: dtype, aggregation: aggregation}
end
@doc false
def operations, do: @operations
@impl true
def dtype(%Series{} = s), do: s.dtype
@impl true
def cast(%Series{} = s, dtype) do
args = [lazy_series!(s), dtype]
data = new(:cast, args, dtype, aggregations?(args))
Backend.Series.new(data, dtype)
end
@impl true
def from_list(list, dtype) when is_list(list) do
data = new(:from_list, [list, dtype], dtype, false)
Backend.Series.new(data, dtype)
end
@impl true
def from_binary(binary, dtype) when is_binary(binary) do
data = new(:from_binary, [binary, dtype], dtype, false)
Backend.Series.new(data, dtype)
end
@impl true
def reverse(%Series{} = s) do
args = [lazy_series!(s)]
data = new(:reverse, args, s.dtype, aggregations?(args))
Backend.Series.new(data, s.dtype)
end
@impl true
def argsort(%Series{} = s, descending?, maintain_order?, multithreaded?, nulls_last?) do
args = [lazy_series!(s), descending?, maintain_order?, multithreaded?, nulls_last?]
data = new(:argsort, args, {:u, 32}, aggregations?(args))
Backend.Series.new(data, {:u, 32})
end
@impl true
def sort(%Series{} = s, descending?, maintain_order?, multithreaded?, nulls_last?) do
args = [lazy_series!(s), descending?, maintain_order?, multithreaded?, nulls_last?]
data = new(:sort, args, s.dtype, aggregations?(args))
Backend.Series.new(data, s.dtype)
end
@impl true
def distinct(%Series{} = s) do
args = [lazy_series!(s)]
data = new(:distinct, args, s.dtype, aggregations?(args))
Backend.Series.new(data, s.dtype)
end
@impl true
def unordered_distinct(%Series{} = s) do
args = [lazy_series!(s)]
data = new(:unordered_distinct, args, s.dtype, aggregations?(args))
Backend.Series.new(data, s.dtype)
end
@impl true
def slice(%Series{} = s, offset, length) do
args = [lazy_series!(s), offset, length]
data = new(:slice, args, s.dtype, aggregations?(args))
Backend.Series.new(data, s.dtype)
end
@impl true
def slice(%Series{} = s, indices) when is_list(indices) do
args = [lazy_series!(s), indices]
data = new(:slice, args, s.dtype, aggregations?(args))
Backend.Series.new(data, s.dtype)
end
@impl true
def slice(%Series{} = s, %Series{} = indices) do
args = [lazy_series!(s), series_or_lazy_series!(indices)]
data = new(:slice, args, s.dtype, aggregations?(args))
Backend.Series.new(data, s.dtype)
end
@impl true
def head(%Series{} = s, length) do
args = [lazy_series!(s), length]
data = new(:head, args, s.dtype, aggregations?(args))
Backend.Series.new(data, s.dtype)
end
@impl true
def tail(%Series{} = s, length) do
args = [lazy_series!(s), length]
data = new(:tail, args, s.dtype, aggregations?(args))
Backend.Series.new(data, s.dtype)
end
@impl true
def shift(%Series{} = s, offset, default) do
args = [lazy_series!(s), offset, default]
data = new(:shift, args, s.dtype, aggregations?(args))
Backend.Series.new(data, s.dtype)
end
@impl true
def peaks(%Series{} = s, min_or_max) when is_atom(min_or_max) do
args = [lazy_series!(s), Atom.to_string(min_or_max)]
data = new(:peaks, args, :boolean, aggregations?(args))
Backend.Series.new(data, :boolean)
end
@impl true
def fill_missing_with_strategy(%Series{} = s, strategy) do
args = [lazy_series!(s), strategy]
dtype = if strategy == :mean, do: {:f, 64}, else: s.dtype
data = new(:fill_missing_with_strategy, args, dtype, aggregations?(args))
Backend.Series.new(data, dtype)
end
@impl true
def fill_missing_with_value(%Series{} = s, value) do
args = [lazy_series!(s), value]
data = new(:fill_missing_with_value, args, s.dtype, aggregations?(args))
Backend.Series.new(data, s.dtype)
end
@impl true
def sample(%Series{} = s, n, replacement, shuffle, seed) when is_integer(n) do
args = [lazy_series!(s), n, replacement, shuffle, seed]
data = new(:sample_n, args, s.dtype, aggregations?(args))
Backend.Series.new(data, s.dtype)
end
@impl true
def sample(%Series{} = s, frac, replacement, shuffle, seed) when is_float(frac) do
args = [lazy_series!(s), frac, replacement, shuffle, seed]
data = new(:sample_frac, args, s.dtype, aggregations?(args))
Backend.Series.new(data, s.dtype)
end
# Implements all the comparison operations that
# accepts Series or number on both sides.
#
# It also handles the case for binaries and strings, creating
# a one element series when binaries are present on either sides.
for op <- @comparison_operations do
@impl true
def unquote(op)(left, right) do
args = binary_args(left, right)
data = new(unquote(op), args, :boolean, aggregations?(args))
Backend.Series.new(data, :boolean)
end
end
defp binary_args(left, right) do
case {left, right} do
{%Series{}, %Series{}} ->
[left.data, right.data]
{%Series{dtype: dtype}, value}
when dtype in [:binary, :string, :category] and is_binary(value) ->
[left.data, from_list([value], dtype).data]
{value, %Series{dtype: dtype}}
when dtype in [:binary, :string, :category] and is_binary(value) ->
[from_list([value], dtype).data, right.data]
{%Series{}, other} ->
[left.data, other]
{other, %Series{}} ->
[other, right.data]
end
end
# These are also comparison operations, but they only accept `Series`.
for op <- [:binary_and, :binary_or, :binary_in] do
@impl true
def unquote(op)(%Series{} = left, %Series{} = right) do
args = [series_or_lazy_series!(left), series_or_lazy_series!(right)]
data = new(unquote(op), args, :boolean, aggregations?(args))
Backend.Series.new(data, :boolean)
end
end
for op <- @basic_arithmetic_operations do
@impl true
def unquote(op)(dtype, %Series{} = left, %Series{} = right) do
args = [data!(left), data!(right)]
data = new(unquote(op), args, dtype, aggregations?(args))
Backend.Series.new(data, dtype)
end
end
for op <- @other_arithmetic_operations do
@impl true
def unquote(op)(left, right) do
dtype = resolve_numeric_dtype([left, right])
args = [data!(left), data!(right)]
data = new(unquote(op), args, dtype, aggregations?(args))
Backend.Series.new(data, dtype)
end
end
for op <- @aggregation_operations do
@impl true
def unquote(op)(%Series{} = series) do
args = [lazy_series!(series)]
dtype = dtype_for_agg_operation(unquote(op), series)
data = new(unquote(op), args, dtype, true)
Backend.Series.new(data, dtype)
end
end
for op <- @window_fun_operations do
@impl true
def unquote(op)(%Series{} = series, window_size, weights, min_periods, center) do
args = [lazy_series!(series), window_size, weights, min_periods, center]
if aggregations?(args), do: raise_agg_inside_window(unquote(op))
dtype = resolve_numeric_dtype(unquote(op), [series | List.wrap(weights)])
data = new(unquote(op), args, dtype, false)
Backend.Series.new(data, dtype)
end
end
@impl true
def window_standard_deviation(%Series{} = series, window_size, weights, min_periods, center) do
args = [lazy_series!(series), window_size, weights, min_periods, center]
if aggregations?(args), do: raise_agg_inside_window(:window_standard_deviation)
data = new(:window_standard_deviation, args, {:f, 64}, false)
Backend.Series.new(data, {:f, 64})
end
for op <- @cumulative_operations do
@impl true
def unquote(op)(%Series{} = series, reverse) do
args = [lazy_series!(series), reverse]
if aggregations?(args), do: raise_agg_inside_window(unquote(op))
data = new(unquote(op), args, series.dtype, false)
Backend.Series.new(data, series.dtype)
end
end
for predicate <- @float_predicates do
@impl true
def unquote(predicate)(%Series{} = series) do
data = new(unquote(predicate), [lazy_series!(series)], :boolean)
Backend.Series.new(data, :boolean)
end
end
defp raise_agg_inside_window(op) do
raise "it's not possible to have an aggregation operation inside #{inspect(op)}, " <>
"which is a window function"
end
@impl true
def quantile(%Series{} = series, float) when is_float(float) do
args = [lazy_series!(series), float]
# According to https://github.com/pola-rs/polars/issues/4796,
# it's expected that quantile returns float for integer columns.
dtype =
if series.dtype in Explorer.Shared.integer_types() do
{:f, 64}
else
series.dtype
end
data = new(:quantile, args, dtype, true)
Backend.Series.new(data, dtype)
end
@impl true
def rank(%Series{} = series, method, descending, seed) do
args = [series_or_lazy_series!(series), method, descending, seed]
target_dtype =
if method == :ordinal do
{:u, 32}
else
series.dtype
end
data = new(:rank, args, target_dtype, true)
Backend.Series.new(data, target_dtype)
end
@impl true
def skew(%Series{} = series, bias) do
args = [series_or_lazy_series!(series), bias]
data = new(:skew, args, {:f, 64}, true)
Backend.Series.new(data, {:f, 64})
end
@impl true
def correlation(%Series{} = left, %Series{} = right, ddof, method) do
args = [series_or_lazy_series!(left), series_or_lazy_series!(right), ddof, method]
data = new(:correlation, args, {:f, 64}, true)
Backend.Series.new(data, {:f, 64})
end
@impl true
def covariance(%Series{} = left, %Series{} = right, ddof \\ 1) do
args = [series_or_lazy_series!(left), series_or_lazy_series!(right), ddof]
data = new(:covariance, args, {:f, 64}, true)
Backend.Series.new(data, {:f, 64})
end
@impl true
def variance(%Series{} = s, ddof \\ 1) do
args = [series_or_lazy_series!(s), ddof]
data = new(:variance, args, {:f, 64}, true)
Backend.Series.new(data, {:f, 64})
end
@impl true
def standard_deviation(%Series{} = s, ddof \\ 1) do
args = [series_or_lazy_series!(s), ddof]
data = new(:standard_deviation, args, {:f, 64}, true)
Backend.Series.new(data, {:f, 64})
end
@impl true
def all?(%Series{} = s) do
args = [series_or_lazy_series!(s)]
data = new(:all, args, :boolean, true)
Backend.Series.new(data, :boolean)
end
@impl true
def any?(%Series{} = s) do
args = [series_or_lazy_series!(s)]
data = new(:any, args, :boolean, true)
Backend.Series.new(data, :boolean)
end
@impl true
def coalesce(%Series{} = left, %Series{} = right) do
args = [series_or_lazy_series!(left), series_or_lazy_series!(right)]
dtype =
if left.dtype in Explorer.Shared.numeric_types() do
resolve_numeric_dtype([left, right])
else
left.dtype
end
data = new(:coalesce, args, dtype, aggregations?(args))
Backend.Series.new(data, dtype)
end
@impl true
def select(%Series{} = predicate, %{dtype: dtype} = on_true, on_false) do
args = [series_or_lazy_series!(predicate) | binary_args(on_true, on_false)]
dtype =
if dtype in Explorer.Shared.numeric_types() do
resolve_numeric_dtype([on_true, on_false])
else
dtype
end
data = new(:select, args, dtype, aggregations?(args))
Backend.Series.new(data, dtype)
end
@impl true
def format(list) do
series_list = Enum.map(list, &series_or_lazy_series!/1)
data = new(:format, [series_list], :string, aggregations?(series_list))
Backend.Series.new(data, :string)
end
@impl true
def concat([%Series{} = head | _tail] = series) do
series_list = Enum.map(series, &series_or_lazy_series!/1)
data = new(:concat, [series_list], head.dtype, aggregations?(series_list))
Backend.Series.new(data, head.dtype)
end
@impl true
def day_of_week(%Series{} = s) do
data = new(:day_of_week, [lazy_series!(s)], {:s, 8})
Backend.Series.new(data, {:s, 8})
end
@impl true
def day_of_year(%Series{} = s) do
data = new(:day_of_year, [lazy_series!(s)], {:s, 16})
Backend.Series.new(data, {:s, 16})
end
@impl true
def week_of_year(%Series{} = s) do
data = new(:week_of_year, [lazy_series!(s)], {:s, 8})
Backend.Series.new(data, {:s, 8})
end
@impl true
def month(%Series{} = s) do
data = new(:month, [lazy_series!(s)], {:s, 8})
Backend.Series.new(data, {:s, 8})
end
@impl true
def year(%Series{} = s) do
data = new(:year, [lazy_series!(s)], {:s, 32})
Backend.Series.new(data, {:s, 32})
end
@impl true
def hour(%Series{} = s) do
data = new(:hour, [lazy_series!(s)], {:s, 8})
Backend.Series.new(data, {:s, 8})
end
@impl true
def minute(%Series{} = s) do
data = new(:minute, [lazy_series!(s)], {:s, 8})
Backend.Series.new(data, {:s, 8})
end
@impl true
def second(%Series{} = s) do
data = new(:second, [lazy_series!(s)], {:s, 8})
Backend.Series.new(data, {:s, 8})
end
@impl true
def ewm_mean(%Series{} = series, alpha, adjust, min_periods, ignore_nils) do
args = [lazy_series!(series), alpha, adjust, min_periods, ignore_nils]
if aggregations?(args), do: raise_agg_inside_window(:ewm_mean)
data = new(:ewm_mean, args, {:f, 64}, false)
Backend.Series.new(data, {:f, 64})
end
@impl true
def ewm_standard_deviation(%Series{} = series, alpha, adjust, bias, min_periods, ignore_nils) do
args = [lazy_series!(series), alpha, adjust, bias, min_periods, ignore_nils]
if aggregations?(args), do: raise_agg_inside_window(:ewm_standard_deviation)
data = new(:ewm_standard_deviation, args, {:f, 64}, false)
Backend.Series.new(data, {:f, 64})
end
@impl true
def ewm_variance(%Series{} = series, alpha, adjust, bias, min_periods, ignore_nils) do
args = [lazy_series!(series), alpha, adjust, bias, min_periods, ignore_nils]
if aggregations?(args), do: raise_agg_inside_window(:ewm_variance)
data = new(:ewm_variance, args, {:f, 64}, false)
Backend.Series.new(data, {:f, 64})
end
defp dtype_for_agg_operation(op, _)
when op in [:count, :nil_count, :n_distinct, :argmin, :argmax],
do: {:u, 32}
defp dtype_for_agg_operation(op, series)
when op in [:first, :last, :sum, :min, :max],
do: series.dtype
defp dtype_for_agg_operation(op, _) when op in [:all?, :any?], do: :boolean
defp dtype_for_agg_operation(:mode, series), do: {:list, series.dtype}
defp dtype_for_agg_operation(_, _), do: {:f, 64}
defp resolve_numeric_dtype(items) do
dtypes =
for item <- items, uniq: true do
case item do
%Series{dtype: dtype} -> dtype
other -> Explorer.Shared.dtype_from_list!([other])
end
end
case dtypes do
[dtype] ->
if dtype in Explorer.Shared.numeric_types() do
dtype
else
raise "invalid dtype for numeric items: #{inspect(dtype)}"
end
[_, _] ->
{:f, 64}
end
end
defp resolve_numeric_dtype(:window_mean, _items), do: {:f, 64}
defp resolve_numeric_dtype(_op, items), do: resolve_numeric_dtype(items)
# Returns the inner `data` if it's a lazy series. Otherwise raises an error.
defp lazy_series!(series) do
case series do
%Series{data: %__MODULE__{}} ->
data!(series)
%Series{} ->
raise ArgumentError, "expecting a LazySeries, but instead got #{inspect(series)}"
end
end
defp series_or_lazy_series!(%Series{} = series), do: data!(series)
defp data!(%Series{data: data}), do: data
defp data!(value), do: value
defp aggregations?(args) do
Enum.any?(args, fn
%__MODULE__{aggregation: is_agg} -> is_agg
_other -> false
end)
end
@impl true
def is_nil(%Series{} = series) do
data = new(:is_nil, [lazy_series!(series)], :boolean)
Backend.Series.new(data, :boolean)
end
@impl true
def is_not_nil(%Series{} = series) do
data = new(:is_not_nil, [lazy_series!(series)], :boolean)
Backend.Series.new(data, :boolean)
end
@impl true
def all_equal(%Series{} = left, %Series{} = right) do
args = [lazy_series!(left), lazy_series!(right)]
data = new(:all_equal, args, :boolean, aggregations?(args))
Backend.Series.new(data, :boolean)
end
@impl true
def unary_not(%Series{} = series) do
data = new(:unary_not, [lazy_series!(series)], :boolean)
Backend.Series.new(data, :boolean)
end
@impl true
def log(%Series{} = series) do
data = new(:log, [lazy_series!(series)], {:f, 64})
Backend.Series.new(data, {:f, 64})
end
@impl true
def log(%Series{} = series, base) do
data = new(:log, [lazy_series!(series), base], {:f, 64})
Backend.Series.new(data, {:f, 64})
end
@impl true
def exp(%Series{} = series) do
data = new(:exp, [lazy_series!(series)], {:f, 64})
Backend.Series.new(data, {:f, 64})
end
@impl true
def abs(%Series{} = series) do
data = new(:abs, [lazy_series!(series)], series.dtype)
Backend.Series.new(data, series.dtype)
end
@impl true
def strptime(%Series{} = series, format_string) do
dtype = {:datetime, :microsecond}
data = new(:strptime, [lazy_series!(series), format_string], dtype)
Backend.Series.new(data, dtype)
end
@impl true
def strftime(%Series{} = series, format_string) do
dtype = :string
data = new(:strftime, [lazy_series!(series), format_string], dtype)
Backend.Series.new(data, dtype)
end
@impl true
def clip(%Series{dtype: {s_or_u, _} = dtype} = series, min, max)
when s_or_u in [:s, :u] and is_integer(min) and is_integer(max) do
data = new(:clip_integer, [lazy_series!(series), min, max], dtype)
Backend.Series.new(data, dtype)
end
def clip(%Series{} = series, min, max) do
data = new(:clip_float, [lazy_series!(series), min * 1.0, max * 1.0], {:f, 64})
Backend.Series.new(data, {:f, 64})
end
@impl true
def sin(%Series{} = series) do
data = new(:sin, [lazy_series!(series)], {:f, 64})
Backend.Series.new(data, {:f, 64})
end
@impl true
def cos(%Series{} = series) do
data = new(:cos, [lazy_series!(series)], {:f, 64})
Backend.Series.new(data, {:f, 64})
end
@impl true
def tan(%Series{} = series) do
data = new(:tan, [lazy_series!(series)], {:f, 64})
Backend.Series.new(data, {:f, 64})
end
@impl true
def asin(%Series{} = series) do
data = new(:asin, [lazy_series!(series)], {:f, 64})
Backend.Series.new(data, {:f, 64})
end
@impl true
def acos(%Series{} = series) do
data = new(:acos, [lazy_series!(series)], {:f, 64})
Backend.Series.new(data, {:f, 64})
end
@impl true
def atan(%Series{} = series) do
data = new(:atan, [lazy_series!(series)], {:f, 64})
Backend.Series.new(data, {:f, 64})
end
@impl true
def inspect(series, opts) do
alias Inspect.Algebra, as: A
open = A.color("(", :list, opts)
close = A.color(")", :list, opts)
dtype =
series
|> Series.dtype()
|> Explorer.Shared.dtype_to_string()
|> A.color(:atom, opts)
A.concat([
A.color("LazySeries[???]", :atom, opts),
A.line(),
dtype,
" ",
open,
Code.quoted_to_algebra(to_elixir_ast(series.data)),
close
])
end
@to_elixir_op %{
add: :+,
subtract: :-,
multiply: :*,
divide: :/,
pow: :**,
equal: :==,
not_equal: :!=,
greater: :>,
greater_equal: :>=,
less: :<,
less_equal: :<=,
binary_and: :and,
binary_or: :or,
binary_in: :in,
unary_not: :not
}
defp to_elixir_ast(%__MODULE__{op: :from_list, args: [[single], _]}) do
single
end
defp to_elixir_ast(%__MODULE__{op: op, args: args}) do
{Map.get(@to_elixir_op, op, op), [], Enum.map(args, &to_elixir_ast/1)}
end
defp to_elixir_ast(%Explorer.PolarsBackend.Series{} = series) do
series = Explorer.PolarsBackend.Shared.create_series(series)
case Explorer.Series.size(series) do
1 -> series[0]
_ -> series
end
end
defp to_elixir_ast(other), do: other
@impl true
def size(_series) do
raise """
cannot retrieve the size of a lazy series, use count/1 instead
"""
end
@impl true
def transform(_series, _fun) do
raise """
#{unsupported(:transform, 2)}
If you want to transform a column, you must do so outside of a query.
For example, instead of:
Explorer.DataFrame.mutate(df, new_column: transform(column, &String.upcase/1))
You must write:
Explorer.DataFrame.put(df, :new_column, Explorer.Series.transform(column, &String.upcase/1))
However, keep in mind that in such cases you are loading the data into Elixir and
serializing it back, which may be expensive for large datasets
"""
end
@impl true
def contains(series, pattern) do
data = new(:contains, [lazy_series!(series), pattern], :boolean)
Backend.Series.new(data, :boolean)
end
@impl true
def upcase(series) do
data = new(:upcase, [lazy_series!(series)], :string)
Backend.Series.new(data, :string)
end
@impl true
def downcase(series) do
data = new(:downcase, [lazy_series!(series)], :string)