polars-df 0.2.0-x86_64-linux
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.yardopts +3 -0
- data/CHANGELOG.md +33 -0
- data/Cargo.lock +2230 -0
- data/Cargo.toml +10 -0
- data/LICENSE-THIRD-PARTY.txt +38828 -0
- data/LICENSE.txt +20 -0
- data/README.md +91 -0
- data/lib/polars/3.0/polars.so +0 -0
- data/lib/polars/3.1/polars.so +0 -0
- data/lib/polars/3.2/polars.so +0 -0
- data/lib/polars/batched_csv_reader.rb +96 -0
- data/lib/polars/cat_expr.rb +52 -0
- data/lib/polars/cat_name_space.rb +54 -0
- data/lib/polars/convert.rb +100 -0
- data/lib/polars/data_frame.rb +4833 -0
- data/lib/polars/data_types.rb +122 -0
- data/lib/polars/date_time_expr.rb +1418 -0
- data/lib/polars/date_time_name_space.rb +1484 -0
- data/lib/polars/dynamic_group_by.rb +52 -0
- data/lib/polars/exceptions.rb +20 -0
- data/lib/polars/expr.rb +5307 -0
- data/lib/polars/expr_dispatch.rb +22 -0
- data/lib/polars/functions.rb +453 -0
- data/lib/polars/group_by.rb +558 -0
- data/lib/polars/io.rb +814 -0
- data/lib/polars/lazy_frame.rb +2442 -0
- data/lib/polars/lazy_functions.rb +1195 -0
- data/lib/polars/lazy_group_by.rb +93 -0
- data/lib/polars/list_expr.rb +610 -0
- data/lib/polars/list_name_space.rb +346 -0
- data/lib/polars/meta_expr.rb +54 -0
- data/lib/polars/rolling_group_by.rb +35 -0
- data/lib/polars/series.rb +3730 -0
- data/lib/polars/slice.rb +104 -0
- data/lib/polars/string_expr.rb +972 -0
- data/lib/polars/string_name_space.rb +690 -0
- data/lib/polars/struct_expr.rb +100 -0
- data/lib/polars/struct_name_space.rb +64 -0
- data/lib/polars/utils.rb +192 -0
- data/lib/polars/version.rb +4 -0
- data/lib/polars/when.rb +16 -0
- data/lib/polars/when_then.rb +19 -0
- data/lib/polars-df.rb +1 -0
- data/lib/polars.rb +50 -0
- metadata +89 -0
data/lib/polars/slice.rb
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
module Polars
|
2
|
+
# @private
|
3
|
+
class Slice
|
4
|
+
def initialize(obj)
|
5
|
+
@obj = obj
|
6
|
+
end
|
7
|
+
|
8
|
+
# Apply a slice operation, taking advantage of any potential fast paths.
|
9
|
+
def apply(s)
|
10
|
+
# normalize slice
|
11
|
+
_slice_setup(s)
|
12
|
+
|
13
|
+
# check for fast-paths / single-operation calls
|
14
|
+
if @slice_length == 0
|
15
|
+
@obj.cleared
|
16
|
+
elsif @is_unbounded && [-1, 1].include?(@stride)
|
17
|
+
@stride < 0 ? @obj.reverse : @obj.clone
|
18
|
+
elsif @start >= 0 && @stop >= 0 && @stride == 1
|
19
|
+
@obj.slice(@start, @slice_length)
|
20
|
+
elsif @stride < 0 && @slice_length == 1
|
21
|
+
@obj.slice(@stop + 1, 1)
|
22
|
+
else
|
23
|
+
# multi-operation calls; make lazy
|
24
|
+
lazyobj = _lazify(@obj)
|
25
|
+
sliced = @stride > 0 ? _slice_positive(lazyobj) : _slice_negative(lazyobj)
|
26
|
+
_as_original(sliced, @obj)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
# Return lazy variant back to its original type.
|
33
|
+
def _as_original(lazy, original)
|
34
|
+
frame = lazy.collect
|
35
|
+
original.is_a?(DataFrame) ? frame : frame.to_series
|
36
|
+
end
|
37
|
+
|
38
|
+
# Make lazy to ensure efficient/consistent handling.
|
39
|
+
def _lazify(obj)
|
40
|
+
obj.is_a?(DataFrame) ? obj.lazy : obj.to_frame.lazy
|
41
|
+
end
|
42
|
+
|
43
|
+
# Logic for slices with positive stride.
|
44
|
+
def _slice_positive(obj)
|
45
|
+
# note: at this point stride is guaranteed to be > 1
|
46
|
+
obj.slice(@start, @slice_length).take_every(@stride)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Logic for slices with negative stride.
|
50
|
+
def _slice_negative(obj)
|
51
|
+
stride = @stride.abs
|
52
|
+
lazyslice = obj.slice(@stop + 1, @slice_length).reverse
|
53
|
+
stride > 1 ? lazyslice.take_every(stride) : lazyslice
|
54
|
+
end
|
55
|
+
|
56
|
+
# Normalize slice bounds, identify unbounded and/or zero-length slices.
|
57
|
+
def _slice_setup(s)
|
58
|
+
# can normalize slice indices as we know object size
|
59
|
+
obj_len = @obj.len
|
60
|
+
start = if s.begin
|
61
|
+
if s.begin < 0
|
62
|
+
[s.begin + obj_len, 0].max
|
63
|
+
else
|
64
|
+
s.begin
|
65
|
+
end
|
66
|
+
else
|
67
|
+
0
|
68
|
+
end
|
69
|
+
stop = if s.end
|
70
|
+
if s.end < 0
|
71
|
+
s.end + (s.exclude_end? ? 0 : 1) + obj_len
|
72
|
+
else
|
73
|
+
s.end + (s.exclude_end? ? 0 : 1)
|
74
|
+
end
|
75
|
+
else
|
76
|
+
obj_len
|
77
|
+
end
|
78
|
+
stride = 1
|
79
|
+
|
80
|
+
# check if slice is actually unbounded
|
81
|
+
if stride >= 1
|
82
|
+
@is_unbounded = start <= 0 && stop >= obj_len
|
83
|
+
else
|
84
|
+
@is_unbounded = stop == -1 && start >= obj_len - 1
|
85
|
+
end
|
86
|
+
|
87
|
+
# determine slice length
|
88
|
+
if @obj.is_empty
|
89
|
+
@slice_length = 0
|
90
|
+
elsif @is_unbounded
|
91
|
+
@slice_length = obj_len
|
92
|
+
else
|
93
|
+
@slice_length = if start == stop || (stride > 0 && start > stop) || (stride < 0 && start < stop)
|
94
|
+
0
|
95
|
+
else
|
96
|
+
(stop - start).abs
|
97
|
+
end
|
98
|
+
end
|
99
|
+
@start = start
|
100
|
+
@stop = stop
|
101
|
+
@stride = stride
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|