polars_python/expr/
general.rs

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