Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-plan/src/dsl/array.rs
6939 views
1
use polars_core::prelude::*;
2
3
use crate::dsl::function_expr::ArrayFunction;
4
use crate::prelude::*;
5
6
/// Specialized expressions for [`Series`] of [`DataType::Array`].
7
pub struct ArrayNameSpace(pub Expr);
8
9
impl ArrayNameSpace {
10
/// Compute the length of every subarray.
11
pub fn len(self) -> Expr {
12
self.0
13
.map_unary(FunctionExpr::ArrayExpr(ArrayFunction::Length))
14
}
15
16
/// Compute the maximum of the items in every subarray.
17
pub fn max(self) -> Expr {
18
self.0
19
.map_unary(FunctionExpr::ArrayExpr(ArrayFunction::Max))
20
}
21
22
/// Compute the minimum of the items in every subarray.
23
pub fn min(self) -> Expr {
24
self.0
25
.map_unary(FunctionExpr::ArrayExpr(ArrayFunction::Min))
26
}
27
28
/// Compute the sum of the items in every subarray.
29
pub fn sum(self) -> Expr {
30
self.0
31
.map_unary(FunctionExpr::ArrayExpr(ArrayFunction::Sum))
32
}
33
34
/// Compute the std of the items in every subarray.
35
pub fn std(self, ddof: u8) -> Expr {
36
self.0
37
.map_unary(FunctionExpr::ArrayExpr(ArrayFunction::Std(ddof)))
38
}
39
40
/// Compute the var of the items in every subarray.
41
pub fn var(self, ddof: u8) -> Expr {
42
self.0
43
.map_unary(FunctionExpr::ArrayExpr(ArrayFunction::Var(ddof)))
44
}
45
46
/// Compute the mean of the items in every subarray.
47
pub fn mean(self) -> Expr {
48
self.0
49
.map_unary(FunctionExpr::ArrayExpr(ArrayFunction::Mean))
50
}
51
52
/// Compute the median of the items in every subarray.
53
pub fn median(self) -> Expr {
54
self.0
55
.map_unary(FunctionExpr::ArrayExpr(ArrayFunction::Median))
56
}
57
58
/// Keep only the unique values in every sub-array.
59
pub fn unique(self) -> Expr {
60
self.0
61
.map_unary(FunctionExpr::ArrayExpr(ArrayFunction::Unique(false)))
62
}
63
64
/// Keep only the unique values in every sub-array.
65
pub fn unique_stable(self) -> Expr {
66
self.0
67
.map_unary(FunctionExpr::ArrayExpr(ArrayFunction::Unique(true)))
68
}
69
70
pub fn n_unique(self) -> Expr {
71
self.0
72
.map_unary(FunctionExpr::ArrayExpr(ArrayFunction::NUnique))
73
}
74
75
/// Cast the Array column to List column with the same inner data type.
76
pub fn to_list(self) -> Expr {
77
self.0
78
.map_unary(FunctionExpr::ArrayExpr(ArrayFunction::ToList))
79
}
80
81
#[cfg(feature = "array_any_all")]
82
/// Evaluate whether all boolean values are true for every subarray.
83
pub fn all(self) -> Expr {
84
self.0
85
.map_unary(FunctionExpr::ArrayExpr(ArrayFunction::All))
86
}
87
88
#[cfg(feature = "array_any_all")]
89
/// Evaluate whether any boolean value is true for every subarray
90
pub fn any(self) -> Expr {
91
self.0
92
.map_unary(FunctionExpr::ArrayExpr(ArrayFunction::Any))
93
}
94
95
pub fn sort(self, options: SortOptions) -> Expr {
96
self.0
97
.map_unary(FunctionExpr::ArrayExpr(ArrayFunction::Sort(options)))
98
}
99
100
pub fn reverse(self) -> Expr {
101
self.0
102
.map_unary(FunctionExpr::ArrayExpr(ArrayFunction::Reverse))
103
}
104
105
pub fn arg_min(self) -> Expr {
106
self.0
107
.map_unary(FunctionExpr::ArrayExpr(ArrayFunction::ArgMin))
108
}
109
110
pub fn arg_max(self) -> Expr {
111
self.0
112
.map_unary(FunctionExpr::ArrayExpr(ArrayFunction::ArgMax))
113
}
114
115
/// Get items in every sub-array by index.
116
pub fn get(self, index: Expr, null_on_oob: bool) -> Expr {
117
self.0.map_binary(
118
FunctionExpr::ArrayExpr(ArrayFunction::Get(null_on_oob)),
119
index,
120
)
121
}
122
123
/// Join all string items in a sub-array and place a separator between them.
124
/// # Error
125
/// Raise if inner type of array is not `DataType::String`.
126
pub fn join(self, separator: Expr, ignore_nulls: bool) -> Expr {
127
self.0.map_binary(
128
FunctionExpr::ArrayExpr(ArrayFunction::Join(ignore_nulls)),
129
separator,
130
)
131
}
132
133
#[cfg(feature = "is_in")]
134
/// Check if the sub-array contains specific element
135
pub fn contains<E: Into<Expr>>(self, other: E, nulls_equal: bool) -> Expr {
136
self.0.map_binary(
137
FunctionExpr::ArrayExpr(ArrayFunction::Contains { nulls_equal }),
138
other.into(),
139
)
140
}
141
142
#[cfg(feature = "array_count")]
143
/// Count how often the value produced by ``element`` occurs.
144
pub fn count_matches<E: Into<Expr>>(self, element: E) -> Expr {
145
self.0.map_binary(
146
FunctionExpr::ArrayExpr(ArrayFunction::CountMatches),
147
element.into(),
148
)
149
}
150
151
#[cfg(feature = "array_to_struct")]
152
pub fn to_struct(self, name_generator: Option<DslNameGenerator>) -> Expr {
153
self.0.map_unary(ArrayFunction::ToStruct(name_generator))
154
}
155
156
/// Slice every subarray.
157
pub fn slice(self, offset: Expr, length: Expr, as_array: bool) -> PolarsResult<Expr> {
158
if as_array {
159
let Ok(offset) = offset.extract_i64() else {
160
polars_bail!(InvalidOperation: "Offset must be a constant `i64` value if `as_array=true`, got: {}", offset)
161
};
162
let Ok(length) = length.extract_i64() else {
163
polars_bail!(InvalidOperation: "Length must be a constant `i64` value if `as_array=true`, got: {}", length)
164
};
165
Ok(self
166
.0
167
.map_unary(FunctionExpr::ArrayExpr(ArrayFunction::Slice(
168
offset, length,
169
))))
170
} else {
171
Ok(self
172
.0
173
.map_unary(FunctionExpr::ArrayExpr(ArrayFunction::ToList))
174
.map_ternary(FunctionExpr::ListExpr(ListFunction::Slice), offset, length))
175
}
176
}
177
178
/// Get the head of every subarray
179
pub fn head(self, n: Expr, as_array: bool) -> PolarsResult<Expr> {
180
self.slice(lit(0), n, as_array)
181
}
182
183
/// Get the tail of every subarray
184
pub fn tail(self, n: Expr, as_array: bool) -> PolarsResult<Expr> {
185
self.slice(lit(0i64) - n.clone().cast(DataType::Int64), n, as_array)
186
}
187
188
/// Shift every sub-array.
189
pub fn shift(self, n: Expr) -> Expr {
190
self.0
191
.map_binary(FunctionExpr::ArrayExpr(ArrayFunction::Shift), n)
192
}
193
/// Returns a column with a separate row for every array element.
194
pub fn explode(self) -> Expr {
195
self.0
196
.map_unary(FunctionExpr::ArrayExpr(ArrayFunction::Explode {
197
skip_empty: false,
198
}))
199
}
200
}
201
202