Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-python/src/expr/list.rs
7889 views
1
use polars::prelude::*;
2
use polars::series::ops::NullBehavior;
3
use polars_utils::pl_str::PlSmallStr;
4
use pyo3::prelude::*;
5
use pyo3::types::PySequence;
6
7
use crate::PyExpr;
8
use crate::conversion::Wrap;
9
10
#[pymethods]
11
impl PyExpr {
12
#[cfg(feature = "list_any_all")]
13
fn list_all(&self) -> Self {
14
self.inner.clone().list().all().into()
15
}
16
17
#[cfg(feature = "list_any_all")]
18
fn list_any(&self) -> Self {
19
self.inner.clone().list().any().into()
20
}
21
22
fn list_arg_max(&self) -> Self {
23
self.inner.clone().list().arg_max().into()
24
}
25
26
fn list_arg_min(&self) -> Self {
27
self.inner.clone().list().arg_min().into()
28
}
29
30
#[cfg(feature = "is_in")]
31
fn list_contains(&self, other: PyExpr, nulls_equal: bool) -> Self {
32
self.inner
33
.clone()
34
.list()
35
.contains(other.inner, nulls_equal)
36
.into()
37
}
38
39
#[cfg(feature = "list_count")]
40
fn list_count_matches(&self, expr: PyExpr) -> Self {
41
self.inner.clone().list().count_matches(expr.inner).into()
42
}
43
44
fn list_diff(&self, n: i64, null_behavior: Wrap<NullBehavior>) -> PyResult<Self> {
45
Ok(self.inner.clone().list().diff(n, null_behavior.0).into())
46
}
47
48
fn list_eval(&self, expr: PyExpr, _parallel: bool) -> Self {
49
self.inner.clone().list().eval(expr.inner).into()
50
}
51
52
fn list_agg(&self, expr: PyExpr) -> Self {
53
self.inner.clone().list().agg(expr.inner).into()
54
}
55
56
#[cfg(feature = "list_filter")]
57
fn list_filter(&self, predicate: PyExpr) -> Self {
58
self.inner
59
.clone()
60
.list()
61
.eval(Expr::Column(PlSmallStr::EMPTY).filter(predicate.inner))
62
.into()
63
}
64
65
fn list_get(&self, index: PyExpr, null_on_oob: bool) -> Self {
66
self.inner
67
.clone()
68
.list()
69
.get(index.inner, null_on_oob)
70
.into()
71
}
72
73
fn list_join(&self, separator: PyExpr, ignore_nulls: bool) -> Self {
74
self.inner
75
.clone()
76
.list()
77
.join(separator.inner, ignore_nulls)
78
.into()
79
}
80
81
fn list_len(&self) -> Self {
82
self.inner.clone().list().len().into()
83
}
84
85
fn list_max(&self) -> Self {
86
self.inner.clone().list().max().into()
87
}
88
89
fn list_mean(&self) -> Self {
90
self.inner.clone().list().mean().into()
91
}
92
93
fn list_median(&self) -> Self {
94
self.inner.clone().list().median().into()
95
}
96
97
fn list_std(&self, ddof: u8) -> Self {
98
self.inner.clone().list().std(ddof).into()
99
}
100
101
fn list_var(&self, ddof: u8) -> Self {
102
self.inner.clone().list().var(ddof).into()
103
}
104
105
fn list_min(&self) -> Self {
106
self.inner.clone().list().min().into()
107
}
108
109
fn list_reverse(&self) -> Self {
110
self.inner.clone().list().reverse().into()
111
}
112
113
fn list_shift(&self, periods: PyExpr) -> Self {
114
self.inner.clone().list().shift(periods.inner).into()
115
}
116
117
#[pyo3(signature = (offset, length=None))]
118
fn list_slice(&self, offset: PyExpr, length: Option<PyExpr>) -> Self {
119
let length = match length {
120
Some(i) => i.inner,
121
None => lit(i64::MAX),
122
};
123
self.inner.clone().list().slice(offset.inner, length).into()
124
}
125
126
fn list_tail(&self, n: PyExpr) -> Self {
127
self.inner.clone().list().tail(n.inner).into()
128
}
129
130
fn list_sort(&self, descending: bool, nulls_last: bool) -> Self {
131
self.inner
132
.clone()
133
.list()
134
.sort(
135
SortOptions::default()
136
.with_order_descending(descending)
137
.with_nulls_last(nulls_last),
138
)
139
.into()
140
}
141
142
fn list_sum(&self) -> Self {
143
self.inner.clone().list().sum().into()
144
}
145
146
#[cfg(feature = "list_drop_nulls")]
147
fn list_drop_nulls(&self) -> Self {
148
self.inner.clone().list().drop_nulls().into()
149
}
150
151
#[cfg(feature = "list_sample")]
152
#[pyo3(signature = (n, with_replacement, shuffle, seed=None))]
153
fn list_sample_n(
154
&self,
155
n: PyExpr,
156
with_replacement: bool,
157
shuffle: bool,
158
seed: Option<u64>,
159
) -> Self {
160
self.inner
161
.clone()
162
.list()
163
.sample_n(n.inner, with_replacement, shuffle, seed)
164
.into()
165
}
166
167
#[cfg(feature = "list_sample")]
168
#[pyo3(signature = (fraction, with_replacement, shuffle, seed=None))]
169
fn list_sample_fraction(
170
&self,
171
fraction: PyExpr,
172
with_replacement: bool,
173
shuffle: bool,
174
seed: Option<u64>,
175
) -> Self {
176
self.inner
177
.clone()
178
.list()
179
.sample_fraction(fraction.inner, with_replacement, shuffle, seed)
180
.into()
181
}
182
183
#[cfg(feature = "list_gather")]
184
fn list_gather(&self, index: PyExpr, null_on_oob: bool) -> Self {
185
self.inner
186
.clone()
187
.list()
188
.gather(index.inner, null_on_oob)
189
.into()
190
}
191
192
#[cfg(feature = "list_gather")]
193
fn list_gather_every(&self, n: PyExpr, offset: PyExpr) -> Self {
194
self.inner
195
.clone()
196
.list()
197
.gather_every(n.inner, offset.inner)
198
.into()
199
}
200
201
fn list_to_array(&self, width: usize) -> Self {
202
self.inner.clone().list().to_array(width).into()
203
}
204
205
#[pyo3(signature = (names))]
206
fn list_to_struct(&self, names: Bound<'_, PySequence>) -> PyResult<Self> {
207
Ok(self
208
.inner
209
.clone()
210
.list()
211
.to_struct(
212
names
213
.try_iter()?
214
.map(|x| Ok(x?.extract::<Wrap<PlSmallStr>>()?.0))
215
.collect::<PyResult<Arc<[_]>>>()?,
216
)
217
.into())
218
}
219
220
fn list_n_unique(&self) -> Self {
221
self.inner.clone().list().n_unique().into()
222
}
223
224
fn list_unique(&self, maintain_order: bool) -> Self {
225
let e = self.inner.clone();
226
227
if maintain_order {
228
e.list().unique_stable().into()
229
} else {
230
e.list().unique().into()
231
}
232
}
233
234
#[cfg(feature = "list_sets")]
235
fn list_set_operation(&self, other: PyExpr, operation: Wrap<SetOperation>) -> Self {
236
let e = self.inner.clone().list();
237
match operation.0 {
238
SetOperation::Intersection => e.set_intersection(other.inner),
239
SetOperation::Difference => e.set_difference(other.inner),
240
SetOperation::Union => e.union(other.inner),
241
SetOperation::SymmetricDifference => e.set_symmetric_difference(other.inner),
242
}
243
.into()
244
}
245
}
246
247