1use std::hash::{BuildHasher, Hash, Hasher};
2use std::ops::Neg;
3
4use polars::lazy::dsl;
5use polars::prelude::*;
6use polars::series::ops::NullBehavior;
7use polars_core::chunked_array::cast::CastOptions;
8use polars_plan::plans::predicates::aexpr_to_skip_batch_predicate;
9use polars_plan::plans::{
10 AExprSorted, ExprToIRContext, RowEncodingVariant, node_to_expr, to_expr_ir,
11};
12use polars_utils::arena::Arena;
13use pyo3::class::basic::CompareOp;
14use pyo3::prelude::*;
15
16use super::datatype::PyDataTypeExpr;
17use super::selector::PySelector;
18use crate::conversion::{Wrap, parse_fill_null_strategy};
19use crate::error::PyPolarsErr;
20use crate::utils::EnterPolarsExt;
21use crate::{PyDataType, PyExpr};
22
23#[pymethods]
24impl PyExpr {
25 fn __richcmp__(&self, other: Self, op: CompareOp) -> Self {
26 match op {
27 CompareOp::Eq => self.eq(other),
28 CompareOp::Ne => self.neq(other),
29 CompareOp::Gt => self.gt(other),
30 CompareOp::Lt => self.lt(other),
31 CompareOp::Ge => self.gt_eq(other),
32 CompareOp::Le => self.lt_eq(other),
33 }
34 }
35
36 fn __hash__(&self) -> isize {
37 let mut state = PlFixedStateQuality::with_seed(0).build_hasher();
38 Hash::hash(&self.inner, &mut state);
39 state.finish() as _
40 }
41
42 fn __add__(&self, rhs: Self) -> PyResult<Self> {
43 Ok(dsl::binary_expr(self.inner.clone(), Operator::Plus, rhs.inner).into())
44 }
45 fn __sub__(&self, rhs: Self) -> PyResult<Self> {
46 Ok(dsl::binary_expr(self.inner.clone(), Operator::Minus, rhs.inner).into())
47 }
48 fn __mul__(&self, rhs: Self) -> PyResult<Self> {
49 Ok(dsl::binary_expr(self.inner.clone(), Operator::Multiply, rhs.inner).into())
50 }
51 fn __truediv__(&self, rhs: Self) -> PyResult<Self> {
52 Ok(dsl::binary_expr(self.inner.clone(), Operator::TrueDivide, rhs.inner).into())
53 }
54 fn __mod__(&self, rhs: Self) -> PyResult<Self> {
55 Ok(dsl::binary_expr(self.inner.clone(), Operator::Modulus, rhs.inner).into())
56 }
57 fn __floordiv__(&self, rhs: Self) -> PyResult<Self> {
58 Ok(dsl::binary_expr(self.inner.clone(), Operator::FloorDivide, rhs.inner).into())
59 }
60 fn __neg__(&self) -> PyResult<Self> {
61 Ok(self.inner.clone().neg().into())
62 }
63
64 fn to_str(&self) -> String {
65 format!("{:?}", self.inner)
66 }
67 fn eq(&self, other: Self) -> Self {
68 self.inner.clone().eq(other.inner).into()
69 }
70
71 fn eq_missing(&self, other: Self) -> Self {
72 self.inner.clone().eq_missing(other.inner).into()
73 }
74 fn neq(&self, other: Self) -> Self {
75 self.inner.clone().neq(other.inner).into()
76 }
77 fn neq_missing(&self, other: Self) -> Self {
78 self.inner.clone().neq_missing(other.inner).into()
79 }
80 fn gt(&self, other: Self) -> Self {
81 self.inner.clone().gt(other.inner).into()
82 }
83 fn gt_eq(&self, other: Self) -> Self {
84 self.inner.clone().gt_eq(other.inner).into()
85 }
86 fn lt_eq(&self, other: Self) -> Self {
87 self.inner.clone().lt_eq(other.inner).into()
88 }
89 fn lt(&self, other: Self) -> Self {
90 self.inner.clone().lt(other.inner).into()
91 }
92
93 fn alias(&self, name: &str) -> Self {
94 self.inner.clone().alias(name).into()
95 }
96 fn not_(&self) -> Self {
97 self.inner.clone().not().into()
98 }
99 fn is_null(&self) -> Self {
100 self.inner.clone().is_null().into()
101 }
102 fn is_not_null(&self) -> Self {
103 self.inner.clone().is_not_null().into()
104 }
105
106 fn is_infinite(&self) -> Self {
107 self.inner.clone().is_infinite().into()
108 }
109
110 fn is_finite(&self) -> Self {
111 self.inner.clone().is_finite().into()
112 }
113
114 fn is_nan(&self) -> Self {
115 self.inner.clone().is_nan().into()
116 }
117
118 fn is_not_nan(&self) -> Self {
119 self.inner.clone().is_not_nan().into()
120 }
121
122 fn min(&self) -> Self {
123 self.inner.clone().min().into()
124 }
125
126 fn max(&self) -> Self {
127 self.inner.clone().max().into()
128 }
129
130 fn min_by(&self, by: Self) -> Self {
131 self.inner.clone().min_by(by.inner).into()
132 }
133
134 fn max_by(&self, by: Self) -> Self {
135 self.inner.clone().max_by(by.inner).into()
136 }
137
138 #[cfg(feature = "propagate_nans")]
139 fn nan_max(&self) -> Self {
140 self.inner.clone().nan_max().into()
141 }
142 #[cfg(feature = "propagate_nans")]
143 fn nan_min(&self) -> Self {
144 self.inner.clone().nan_min().into()
145 }
146 fn mean(&self) -> Self {
147 self.inner.clone().mean().into()
148 }
149 fn median(&self) -> Self {
150 self.inner.clone().median().into()
151 }
152 fn sum(&self) -> Self {
153 self.inner.clone().sum().into()
154 }
155 fn n_unique(&self) -> Self {
156 self.inner.clone().n_unique().into()
157 }
158 fn arg_unique(&self) -> Self {
159 self.inner.clone().arg_unique().into()
160 }
161 fn unique(&self) -> Self {
162 self.inner.clone().unique().into()
163 }
164 fn unique_stable(&self) -> Self {
165 self.inner.clone().unique_stable().into()
166 }
167 fn first(&self, ignore_nulls: bool) -> Self {
168 if ignore_nulls {
169 self.inner.clone().first_non_null().into()
170 } else {
171 self.inner.clone().first().into()
172 }
173 }
174 fn last(&self, ignore_nulls: bool) -> Self {
175 if ignore_nulls {
176 self.inner.clone().last_non_null().into()
177 } else {
178 self.inner.clone().last().into()
179 }
180 }
181 fn item(&self, allow_empty: bool) -> Self {
182 self.inner.clone().item(allow_empty).into()
183 }
184 fn implode(&self, maintain_order: bool) -> Self {
185 self.inner.clone().implode(maintain_order).into()
186 }
187 fn quantile(&self, quantile: Self, interpolation: Wrap<QuantileMethod>) -> Self {
188 self.inner
189 .clone()
190 .quantile(quantile.inner, interpolation.0)
191 .into()
192 }
193
194 #[pyo3(signature = (breaks, labels, left_closed, include_breaks))]
195 #[cfg(feature = "cutqcut")]
196 fn cut(
197 &self,
198 breaks: Vec<f64>,
199 labels: Option<Vec<String>>,
200 left_closed: bool,
201 include_breaks: bool,
202 ) -> Self {
203 self.inner
204 .clone()
205 .cut(breaks, labels, left_closed, include_breaks)
206 .into()
207 }
208 #[pyo3(signature = (probs, labels, left_closed, allow_duplicates, include_breaks))]
209 #[cfg(feature = "cutqcut")]
210 fn qcut(
211 &self,
212 probs: Vec<f64>,
213 labels: Option<Vec<String>>,
214 left_closed: bool,
215 allow_duplicates: bool,
216 include_breaks: bool,
217 ) -> Self {
218 self.inner
219 .clone()
220 .qcut(probs, labels, left_closed, allow_duplicates, include_breaks)
221 .into()
222 }
223 #[pyo3(signature = (n_bins, labels, left_closed, allow_duplicates, include_breaks))]
224 #[cfg(feature = "cutqcut")]
225 fn qcut_uniform(
226 &self,
227 n_bins: usize,
228 labels: Option<Vec<String>>,
229 left_closed: bool,
230 allow_duplicates: bool,
231 include_breaks: bool,
232 ) -> Self {
233 self.inner
234 .clone()
235 .qcut_uniform(
236 n_bins,
237 labels,
238 left_closed,
239 allow_duplicates,
240 include_breaks,
241 )
242 .into()
243 }
244
245 #[cfg(feature = "rle")]
246 fn rle(&self) -> Self {
247 self.inner.clone().rle().into()
248 }
249 #[cfg(feature = "rle")]
250 fn rle_id(&self) -> Self {
251 self.inner.clone().rle_id().into()
252 }
253
254 fn agg_groups(&self) -> Self {
255 self.inner.clone().agg_groups().into()
256 }
257 fn count(&self) -> Self {
258 self.inner.clone().count().into()
259 }
260 fn len(&self) -> Self {
261 self.inner.clone().len().into()
262 }
263 fn value_counts(&self, sort: bool, parallel: bool, name: String, normalize: bool) -> Self {
264 self.inner
265 .clone()
266 .value_counts(sort, parallel, name.as_str(), normalize)
267 .into()
268 }
269 fn unique_counts(&self) -> Self {
270 self.inner.clone().unique_counts().into()
271 }
272 fn null_count(&self) -> Self {
273 self.inner.clone().null_count().into()
274 }
275 fn cast(&self, dtype: PyDataTypeExpr, strict: bool, wrap_numerical: bool) -> Self {
276 let options = if wrap_numerical {
277 CastOptions::Overflowing
278 } else if strict {
279 CastOptions::Strict
280 } else {
281 CastOptions::NonStrict
282 };
283
284 let expr = self.inner.clone().cast_with_options(dtype.inner, options);
285 expr.into()
286 }
287 fn sort_with(&self, descending: bool, nulls_last: bool) -> Self {
288 self.inner
289 .clone()
290 .sort(SortOptions {
291 descending,
292 nulls_last,
293 multithreaded: true,
294 maintain_order: false,
295 limit: None,
296 })
297 .into()
298 }
299
300 fn arg_sort(&self, descending: bool, nulls_last: bool) -> Self {
301 self.inner.clone().arg_sort(descending, nulls_last).into()
302 }
303
304 #[cfg(feature = "top_k")]
305 fn top_k(&self, k: Self) -> Self {
306 self.inner.clone().top_k(k.inner).into()
307 }
308
309 #[cfg(feature = "top_k")]
310 fn top_k_by(&self, by: Vec<Self>, k: Self, reverse: Vec<bool>) -> Self {
311 let by = by.into_iter().map(|e| e.inner).collect::<Vec<_>>();
312 self.inner.clone().top_k_by(k.inner, by, reverse).into()
313 }
314
315 #[cfg(feature = "top_k")]
316 fn bottom_k(&self, k: Self) -> Self {
317 self.inner.clone().bottom_k(k.inner).into()
318 }
319
320 #[cfg(feature = "top_k")]
321 fn bottom_k_by(&self, by: Vec<Self>, k: Self, reverse: Vec<bool>) -> Self {
322 let by = by.into_iter().map(|e| e.inner).collect::<Vec<_>>();
323 self.inner.clone().bottom_k_by(k.inner, by, reverse).into()
324 }
325
326 #[cfg(feature = "peaks")]
327 fn peak_min(&self) -> Self {
328 self.inner.clone().peak_min().into()
329 }
330
331 #[cfg(feature = "peaks")]
332 fn peak_max(&self) -> Self {
333 self.inner.clone().peak_max().into()
334 }
335
336 fn arg_max(&self) -> Self {
337 self.inner.clone().arg_max().into()
338 }
339
340 fn arg_min(&self) -> Self {
341 self.inner.clone().arg_min().into()
342 }
343
344 #[cfg(feature = "index_of")]
345 fn index_of(&self, element: Self) -> Self {
346 self.inner.clone().index_of(element.inner).into()
347 }
348
349 #[cfg(feature = "search_sorted")]
350 #[pyo3(signature = (element, side, descending))]
351 fn search_sorted(&self, element: Self, side: Wrap<SearchSortedSide>, descending: bool) -> Self {
352 self.inner
353 .clone()
354 .search_sorted(element.inner, side.0, descending)
355 .into()
356 }
357
358 #[pyo3(signature = (idx, null_on_oob=false))]
359 fn gather(&self, idx: Self, null_on_oob: bool) -> Self {
360 self.inner.clone().gather(idx.inner, null_on_oob).into()
361 }
362
363 #[pyo3(signature = (idx, null_on_oob=false))]
364 fn get(&self, idx: Self, null_on_oob: bool) -> Self {
365 self.inner.clone().get(idx.inner, null_on_oob).into()
366 }
367
368 fn sort_by(
369 &self,
370 by: Vec<Self>,
371 descending: Vec<bool>,
372 nulls_last: Vec<bool>,
373 multithreaded: bool,
374 maintain_order: bool,
375 ) -> Self {
376 let by = by.into_iter().map(|e| e.inner).collect::<Vec<_>>();
377 self.inner
378 .clone()
379 .sort_by(
380 by,
381 SortMultipleOptions {
382 descending,
383 nulls_last,
384 multithreaded,
385 maintain_order,
386 limit: None,
387 },
388 )
389 .into()
390 }
391
392 #[pyo3(signature = (n, fill_value))]
393 fn shift(&self, n: Self, fill_value: Option<Self>) -> Self {
394 let expr = self.inner.clone();
395 let out = match fill_value {
396 Some(v) => expr.shift_and_fill(n.inner, v.inner),
397 None => expr.shift(n.inner),
398 };
399 out.into()
400 }
401
402 fn fill_null(&self, expr: Self) -> Self {
403 self.inner.clone().fill_null(expr.inner).into()
404 }
405
406 fn fill_null_with_strategy(&self, strategy: &str, limit: FillNullLimit) -> PyResult<Self> {
407 let strategy = parse_fill_null_strategy(strategy, limit)?;
408 Ok(self.inner.clone().fill_null_with_strategy(strategy).into())
409 }
410
411 fn fill_nan(&self, expr: Self) -> Self {
412 self.inner.clone().fill_nan(expr.inner).into()
413 }
414
415 fn drop_nulls(&self) -> Self {
416 self.inner.clone().drop_nulls().into()
417 }
418
419 fn drop_nans(&self) -> Self {
420 self.inner.clone().drop_nans().into()
421 }
422
423 fn filter(&self, predicate: Self) -> Self {
424 self.inner.clone().filter(predicate.inner).into()
425 }
426
427 fn reverse(&self) -> Self {
428 self.inner.clone().reverse().into()
429 }
430
431 fn std(&self, ddof: u8) -> Self {
432 self.inner.clone().std(ddof).into()
433 }
434
435 fn var(&self, ddof: u8) -> Self {
436 self.inner.clone().var(ddof).into()
437 }
438
439 fn is_unique(&self) -> Self {
440 self.inner.clone().is_unique().into()
441 }
442
443 fn is_between(&self, lower: Self, upper: Self, closed: Wrap<ClosedInterval>) -> Self {
444 self.inner
445 .clone()
446 .is_between(lower.inner, upper.inner, closed.0)
447 .into()
448 }
449
450 fn is_close(&self, other: Self, abs_tol: f64, rel_tol: f64, nans_equal: bool) -> Self {
451 self.inner
452 .clone()
453 .is_close(other.inner, abs_tol, rel_tol, nans_equal)
454 .into()
455 }
456
457 #[cfg(feature = "approx_unique")]
458 fn approx_n_unique(&self) -> Self {
459 self.inner.clone().approx_n_unique().into()
460 }
461
462 fn is_first_distinct(&self) -> Self {
463 self.inner.clone().is_first_distinct().into()
464 }
465
466 fn is_last_distinct(&self) -> Self {
467 self.inner.clone().is_last_distinct().into()
468 }
469
470 fn explode(&self, empty_as_null: bool, keep_nulls: bool) -> Self {
471 self.inner
472 .clone()
473 .explode(ExplodeOptions {
474 empty_as_null,
475 keep_nulls,
476 })
477 .into()
478 }
479
480 fn gather_every(&self, n: usize, offset: usize) -> Self {
481 self.inner.clone().gather_every(n, offset).into()
482 }
483
484 fn slice(&self, offset: Self, length: Self) -> Self {
485 self.inner.clone().slice(offset.inner, length.inner).into()
486 }
487
488 fn append(&self, other: Self, upcast: bool) -> Self {
489 self.inner.clone().append(other.inner, upcast).into()
490 }
491
492 fn rechunk(&self) -> Self {
493 self.inner.clone().rechunk().into()
494 }
495
496 fn round(&self, decimals: u32, mode: Wrap<RoundMode>) -> Self {
497 self.inner.clone().round(decimals, mode.0).into()
498 }
499
500 fn round_sig_figs(&self, digits: i32) -> Self {
501 self.clone().inner.round_sig_figs(digits).into()
502 }
503
504 fn truncate(&self, decimals: u32) -> Self {
505 self.inner.clone().truncate(decimals).into()
506 }
507
508 fn floor(&self) -> Self {
509 self.inner.clone().floor().into()
510 }
511
512 fn ceil(&self) -> Self {
513 self.inner.clone().ceil().into()
514 }
515
516 #[pyo3(signature = (min, max))]
517 fn clip(&self, min: Option<Self>, max: Option<Self>) -> Self {
518 let expr = self.inner.clone();
519 let out = match (min, max) {
520 (Some(min), Some(max)) => expr.clip(min.inner, max.inner),
521 (Some(min), None) => expr.clip_min(min.inner),
522 (None, Some(max)) => expr.clip_max(max.inner),
523 (None, None) => expr,
524 };
525 out.into()
526 }
527
528 fn abs(&self) -> Self {
529 self.inner.clone().abs().into()
530 }
531
532 #[cfg(feature = "trigonometry")]
533 fn sin(&self) -> Self {
534 self.inner.clone().sin().into()
535 }
536
537 #[cfg(feature = "trigonometry")]
538 fn cos(&self) -> Self {
539 self.inner.clone().cos().into()
540 }
541
542 #[cfg(feature = "trigonometry")]
543 fn tan(&self) -> Self {
544 self.inner.clone().tan().into()
545 }
546
547 #[cfg(feature = "trigonometry")]
548 fn cot(&self) -> Self {
549 self.inner.clone().cot().into()
550 }
551
552 #[cfg(feature = "trigonometry")]
553 fn arcsin(&self) -> Self {
554 self.inner.clone().arcsin().into()
555 }
556
557 #[cfg(feature = "trigonometry")]
558 fn arccos(&self) -> Self {
559 self.inner.clone().arccos().into()
560 }
561
562 #[cfg(feature = "trigonometry")]
563 fn arctan(&self) -> Self {
564 self.inner.clone().arctan().into()
565 }
566
567 #[cfg(feature = "trigonometry")]
568 fn arctan2(&self, y: Self) -> Self {
569 self.inner.clone().arctan2(y.inner).into()
570 }
571
572 #[cfg(feature = "trigonometry")]
573 fn sinh(&self) -> Self {
574 self.inner.clone().sinh().into()
575 }
576
577 #[cfg(feature = "trigonometry")]
578 fn cosh(&self) -> Self {
579 self.inner.clone().cosh().into()
580 }
581
582 #[cfg(feature = "trigonometry")]
583 fn tanh(&self) -> Self {
584 self.inner.clone().tanh().into()
585 }
586
587 #[cfg(feature = "trigonometry")]
588 fn arcsinh(&self) -> Self {
589 self.inner.clone().arcsinh().into()
590 }
591
592 #[cfg(feature = "trigonometry")]
593 fn arccosh(&self) -> Self {
594 self.inner.clone().arccosh().into()
595 }
596
597 #[cfg(feature = "trigonometry")]
598 fn arctanh(&self) -> Self {
599 self.inner.clone().arctanh().into()
600 }
601
602 #[cfg(feature = "trigonometry")]
603 pub fn degrees(&self) -> Self {
604 self.inner.clone().degrees().into()
605 }
606
607 #[cfg(feature = "trigonometry")]
608 pub fn radians(&self) -> Self {
609 self.inner.clone().radians().into()
610 }
611
612 #[cfg(feature = "sign")]
613 fn sign(&self) -> Self {
614 self.inner.clone().sign().into()
615 }
616
617 fn is_duplicated(&self) -> Self {
618 self.inner.clone().is_duplicated().into()
619 }
620
621 #[pyo3(signature = (partition_by, order_by, order_by_descending, order_by_nulls_last, mapping_strategy))]
622 fn over(
623 &self,
624 partition_by: Option<Vec<Self>>,
625 order_by: Option<Vec<Self>>,
626 order_by_descending: bool,
627 order_by_nulls_last: bool,
628 mapping_strategy: Wrap<WindowMapping>,
629 ) -> PyResult<Self> {
630 let partition_by = partition_by.map(|partition_by| {
631 partition_by
632 .into_iter()
633 .map(|e| e.inner)
634 .collect::<Vec<Expr>>()
635 });
636
637 let order_by = order_by.map(|order_by| {
638 (
639 order_by.into_iter().map(|e| e.inner).collect::<Vec<Expr>>(),
640 SortOptions {
641 descending: order_by_descending,
642 nulls_last: order_by_nulls_last,
643 maintain_order: false,
644 ..Default::default()
645 },
646 )
647 });
648
649 Ok(self
650 .inner
651 .clone()
652 .over_with_options(partition_by, order_by, mapping_strategy.0)
653 .map_err(PyPolarsErr::from)?
654 .into())
655 }
656
657 fn rolling(
658 &self,
659 index_column: PyExpr,
660 period: &str,
661 offset: &str,
662 closed: Wrap<ClosedWindow>,
663 ) -> PyResult<Self> {
664 let period = Duration::try_parse(period).map_err(PyPolarsErr::from)?;
665 let offset = Duration::try_parse(offset).map_err(PyPolarsErr::from)?;
666 let closed = closed.0;
667
668 Ok(self
669 .inner
670 .clone()
671 .rolling(index_column.inner, period, offset, closed)
672 .into())
673 }
674
675 fn and_(&self, expr: Self) -> Self {
676 self.inner.clone().and(expr.inner).into()
677 }
678
679 fn or_(&self, expr: Self) -> Self {
680 self.inner.clone().or(expr.inner).into()
681 }
682
683 fn xor_(&self, expr: Self) -> Self {
684 self.inner.clone().xor(expr.inner).into()
685 }
686
687 #[cfg(feature = "is_in")]
688 fn is_in(&self, expr: Self, nulls_equal: bool) -> Self {
689 self.inner.clone().is_in(expr.inner, nulls_equal).into()
690 }
691
692 #[cfg(feature = "repeat_by")]
693 fn repeat_by(&self, by: Self) -> Self {
694 self.inner.clone().repeat_by(by.inner).into()
695 }
696
697 fn pow(&self, exponent: Self) -> Self {
698 self.inner.clone().pow(exponent.inner).into()
699 }
700
701 fn sqrt(&self) -> Self {
702 self.inner.clone().sqrt().into()
703 }
704
705 fn cbrt(&self) -> Self {
706 self.inner.clone().cbrt().into()
707 }
708
709 fn cum_sum(&self, reverse: bool) -> Self {
710 self.inner.clone().cum_sum(reverse).into()
711 }
712 fn cum_max(&self, reverse: bool) -> Self {
713 self.inner.clone().cum_max(reverse).into()
714 }
715 fn cum_min(&self, reverse: bool) -> Self {
716 self.inner.clone().cum_min(reverse).into()
717 }
718 fn cum_prod(&self, reverse: bool) -> Self {
719 self.inner.clone().cum_prod(reverse).into()
720 }
721 fn cum_count(&self, reverse: bool) -> Self {
722 self.inner.clone().cum_count(reverse).into()
723 }
724
725 fn cumulative_eval(&self, expr: Self, min_samples: usize) -> Self {
726 self.inner
727 .clone()
728 .cumulative_eval(expr.inner, min_samples)
729 .into()
730 }
731
732 fn product(&self) -> Self {
733 self.inner.clone().product().into()
734 }
735
736 fn dot(&self, other: Self) -> Self {
737 self.inner.clone().dot(other.inner).into()
738 }
739
740 fn reinterpret(&self, signed: Option<bool>, dtype: Option<PyDataType>) -> Self {
741 self.inner
742 .clone()
743 .reinterpret(signed, dtype.map(|dt| dt.0))
744 .into()
745 }
746 fn mode(&self, maintain_order: bool) -> Self {
747 self.inner.clone().mode(maintain_order).into()
748 }
749 fn interpolate(&self, method: Wrap<InterpolationMethod>) -> Self {
750 self.inner.clone().interpolate(method.0).into()
751 }
752 fn interpolate_by(&self, by: PyExpr) -> Self {
753 self.inner.clone().interpolate_by(by.inner).into()
754 }
755
756 fn lower_bound(&self) -> Self {
757 self.inner.clone().lower_bound().into()
758 }
759
760 fn upper_bound(&self) -> Self {
761 self.inner.clone().upper_bound().into()
762 }
763
764 #[pyo3(signature = (method, descending, seed))]
765 fn rank(&self, method: Wrap<RankMethod>, descending: bool, seed: Option<u64>) -> Self {
766 let options = RankOptions {
767 method: method.0,
768 descending,
769 };
770 self.inner.clone().rank(options, seed).into()
771 }
772
773 fn diff(&self, n: PyExpr, null_behavior: Wrap<NullBehavior>) -> Self {
774 self.inner.clone().diff(n.inner, null_behavior.0).into()
775 }
776
777 #[cfg(feature = "pct_change")]
778 fn pct_change(&self, n: Self) -> Self {
779 self.inner.clone().pct_change(n.inner).into()
780 }
781
782 fn skew(&self, bias: bool) -> Self {
783 self.inner.clone().skew(bias).into()
784 }
785 fn kurtosis(&self, fisher: bool, bias: bool) -> Self {
786 self.inner.clone().kurtosis(fisher, bias).into()
787 }
788
789 #[cfg(feature = "dtype-array")]
790 fn reshape(&self, dims: Vec<i64>) -> Self {
791 self.inner.clone().reshape(&dims).into()
792 }
793
794 fn to_physical(&self) -> Self {
795 self.inner.clone().to_physical().into()
796 }
797
798 #[pyo3(signature = (seed))]
799 fn shuffle(&self, seed: Option<u64>) -> Self {
800 self.inner.clone().shuffle(seed).into()
801 }
802
803 #[pyo3(signature = (n, with_replacement, shuffle, seed))]
804 fn sample_n(&self, n: Self, with_replacement: bool, shuffle: bool, seed: Option<u64>) -> Self {
805 self.inner
806 .clone()
807 .sample_n(n.inner, with_replacement, shuffle, seed)
808 .into()
809 }
810
811 #[pyo3(signature = (frac, with_replacement, shuffle, seed))]
812 fn sample_frac(
813 &self,
814 frac: Self,
815 with_replacement: bool,
816 shuffle: bool,
817 seed: Option<u64>,
818 ) -> Self {
819 self.inner
820 .clone()
821 .sample_frac(frac.inner, with_replacement, shuffle, seed)
822 .into()
823 }
824
825 fn ewm_mean(&self, alpha: f64, adjust: bool, min_periods: usize, ignore_nulls: bool) -> Self {
826 let options = EWMOptions {
827 alpha,
828 adjust,
829 bias: false,
830 min_periods,
831 ignore_nulls,
832 };
833 self.inner.clone().ewm_mean(options).into()
834 }
835 fn ewm_mean_by(&self, times: PyExpr, half_life: &str) -> PyResult<Self> {
836 let half_life = Duration::try_parse(half_life).map_err(PyPolarsErr::from)?;
837 Ok(self
838 .inner
839 .clone()
840 .ewm_mean_by(times.inner, half_life)
841 .into())
842 }
843
844 fn ewm_std(
845 &self,
846 alpha: f64,
847 adjust: bool,
848 bias: bool,
849 min_periods: usize,
850 ignore_nulls: bool,
851 ) -> Self {
852 let options = EWMOptions {
853 alpha,
854 adjust,
855 bias,
856 min_periods,
857 ignore_nulls,
858 };
859 self.inner.clone().ewm_std(options).into()
860 }
861 fn ewm_var(
862 &self,
863 alpha: f64,
864 adjust: bool,
865 bias: bool,
866 min_periods: usize,
867 ignore_nulls: bool,
868 ) -> Self {
869 let options = EWMOptions {
870 alpha,
871 adjust,
872 bias,
873 min_periods,
874 ignore_nulls,
875 };
876 self.inner.clone().ewm_var(options).into()
877 }
878 fn extend_constant(&self, value: PyExpr, n: PyExpr) -> Self {
879 self.inner
880 .clone()
881 .extend_constant(value.inner, n.inner)
882 .into()
883 }
884
885 fn any(&self, ignore_nulls: bool) -> Self {
886 self.inner.clone().any(ignore_nulls).into()
887 }
888 fn all(&self, ignore_nulls: bool) -> Self {
889 self.inner.clone().all(ignore_nulls).into()
890 }
891 fn is_empty(&self, ignore_nulls: bool) -> Self {
892 self.inner.clone().is_empty(ignore_nulls).into()
893 }
894
895 fn has_nulls(&self) -> Self {
896 self.inner.clone().has_nulls().into()
897 }
898
899 fn log(&self, base: PyExpr) -> Self {
900 self.inner.clone().log(base.inner).into()
901 }
902
903 fn log1p(&self) -> Self {
904 self.inner.clone().log1p().into()
905 }
906
907 fn exp(&self) -> Self {
908 self.inner.clone().exp().into()
909 }
910
911 fn entropy(&self, base: f64, normalize: bool) -> Self {
912 self.inner.clone().entropy(base, normalize).into()
913 }
914 fn hash(&self, seed: u64, seed_1: u64, seed_2: u64, seed_3: u64) -> Self {
915 self.inner.clone().hash(seed, seed_1, seed_2, seed_3).into()
916 }
917 fn set_sorted_flag(&self, descending: bool, nulls_last: bool) -> Self {
918 let sortedness = AExprSorted::default()
919 .with_desc(Some(descending))
920 .with_nulls_last(Some(nulls_last));
921 self.inner.clone().set_sorted_flag(sortedness).into()
922 }
923
924 fn replace(&self, old: PyExpr, new: PyExpr) -> Self {
925 self.inner.clone().replace(old.inner, new.inner).into()
926 }
927
928 #[pyo3(signature = (old, new, default, return_dtype))]
929 fn replace_strict(
930 &self,
931 old: PyExpr,
932 new: PyExpr,
933 default: Option<PyExpr>,
934 return_dtype: Option<PyDataTypeExpr>,
935 ) -> Self {
936 self.inner
937 .clone()
938 .replace_strict(
939 old.inner,
940 new.inner,
941 default.map(|e| e.inner),
942 return_dtype.map(|dt| dt.inner),
943 )
944 .into()
945 }
946
947 #[cfg(feature = "hist")]
948 #[pyo3(signature = (bins, bin_count, include_category, include_breakpoint))]
949 fn hist(
950 &self,
951 bins: Option<PyExpr>,
952 bin_count: Option<usize>,
953 include_category: bool,
954 include_breakpoint: bool,
955 ) -> Self {
956 let bins = bins.map(|e| e.inner);
957 self.inner
958 .clone()
959 .hist(bins, bin_count, include_category, include_breakpoint)
960 .into()
961 }
962
963 #[pyo3(signature = (schema))]
964 fn skip_batch_predicate(&self, py: Python<'_>, schema: Wrap<Schema>) -> PyResult<Option<Self>> {
965 let mut aexpr_arena = Arena::new();
966 py.enter_polars(|| {
967 let mut ctx = ExprToIRContext::new(&mut aexpr_arena, &schema.0);
968 ctx.allow_unknown = true;
969 let node = to_expr_ir(self.inner.clone(), &mut ctx)?.node();
970 let Some(node) = aexpr_to_skip_batch_predicate(node, &mut aexpr_arena, &schema.0)
971 else {
972 return Ok(None);
973 };
974 let skip_batch_predicate = node_to_expr(node, &aexpr_arena);
975 PolarsResult::Ok(Some(Self {
976 inner: skip_batch_predicate,
977 }))
978 })
979 }
980
981 #[staticmethod]
982 fn row_encode_unordered(exprs: Vec<Self>) -> Self {
983 Expr::n_ary(
984 FunctionExpr::RowEncode(RowEncodingVariant::Unordered),
985 exprs.into_iter().map(|e| e.inner.clone()).collect(),
986 )
987 .into()
988 }
989
990 #[staticmethod]
991 fn row_encode_ordered(
992 exprs: Vec<Self>,
993 descending: Option<Vec<bool>>,
994 nulls_last: Option<Vec<bool>>,
995 ) -> Self {
996 Expr::n_ary(
997 FunctionExpr::RowEncode(RowEncodingVariant::Ordered {
998 descending,
999 nulls_last,
1000 broadcast_nulls: None,
1001 }),
1002 exprs.into_iter().map(|e| e.inner.clone()).collect(),
1003 )
1004 .into()
1005 }
1006
1007 fn row_decode_unordered(&self, names: Vec<String>, datatypes: Vec<PyDataTypeExpr>) -> Self {
1008 let fields = names
1009 .into_iter()
1010 .zip(datatypes)
1011 .map(|(name, dtype)| (PlSmallStr::from_string(name), dtype.inner))
1012 .collect();
1013 self.inner
1014 .clone()
1015 .map_unary(FunctionExpr::RowDecode(
1016 fields,
1017 RowEncodingVariant::Unordered,
1018 ))
1019 .into()
1020 }
1021
1022 fn row_decode_ordered(
1023 &self,
1024 names: Vec<String>,
1025 datatypes: Vec<PyDataTypeExpr>,
1026 descending: Option<Vec<bool>>,
1027 nulls_last: Option<Vec<bool>>,
1028 ) -> Self {
1029 let fields = names
1030 .into_iter()
1031 .zip(datatypes)
1032 .map(|(name, dtype)| (PlSmallStr::from_string(name), dtype.inner))
1033 .collect::<Vec<_>>();
1034 self.inner
1035 .clone()
1036 .map_unary(FunctionExpr::RowDecode(
1037 fields,
1038 RowEncodingVariant::Ordered {
1039 descending,
1040 nulls_last,
1041 broadcast_nulls: None,
1042 },
1043 ))
1044 .into()
1045 }
1046
1047 #[allow(clippy::wrong_self_convention)]
1048 fn into_selector(&self) -> PyResult<PySelector> {
1049 Ok(self
1050 .inner
1051 .clone()
1052 .into_selector()
1053 .ok_or_else(
1054 || polars_err!(InvalidOperation: "expr `{}` is not a selector", &self.inner),
1055 )
1056 .map_err(PyPolarsErr::from)?
1057 .into())
1058 }
1059
1060 #[staticmethod]
1061 fn new_selector(selector: PySelector) -> Self {
1062 Expr::Selector(selector.inner).into()
1063 }
1064}