polars_python/expr/
datetime.rs

1use polars::prelude::*;
2use pyo3::prelude::*;
3
4use crate::PyExpr;
5use crate::conversion::Wrap;
6
7#[pymethods]
8impl PyExpr {
9    fn dt_add_business_days(
10        &self,
11        n: PyExpr,
12        week_mask: [bool; 7],
13        holidays: Vec<i32>,
14        roll: Wrap<Roll>,
15    ) -> Self {
16        self.inner
17            .clone()
18            .dt()
19            .add_business_days(n.inner, week_mask, holidays, roll.0)
20            .into()
21    }
22
23    fn dt_to_string(&self, format: &str) -> Self {
24        self.inner.clone().dt().to_string(format).into()
25    }
26
27    fn dt_offset_by(&self, by: PyExpr) -> Self {
28        self.inner.clone().dt().offset_by(by.inner).into()
29    }
30
31    fn dt_epoch_seconds(&self) -> Self {
32        self.inner
33            .clone()
34            .map(
35                |s| {
36                    s.take_materialized_series()
37                        .timestamp(TimeUnit::Milliseconds)
38                        .map(|ca| Some((ca / 1000).into_column()))
39                },
40                GetOutput::from_type(DataType::Int64),
41            )
42            .into()
43    }
44
45    fn dt_with_time_unit(&self, time_unit: Wrap<TimeUnit>) -> Self {
46        self.inner.clone().dt().with_time_unit(time_unit.0).into()
47    }
48
49    #[cfg(feature = "timezones")]
50    fn dt_convert_time_zone(&self, time_zone: String) -> PyResult<Self> {
51        use crate::utils::to_py_err;
52
53        Ok(self
54            .inner
55            .clone()
56            .dt()
57            .convert_time_zone(
58                TimeZone::opt_try_new(Some(PlSmallStr::from(time_zone)))
59                    .map_err(to_py_err)?
60                    .unwrap_or(TimeZone::UTC),
61            )
62            .into())
63    }
64
65    fn dt_cast_time_unit(&self, time_unit: Wrap<TimeUnit>) -> Self {
66        self.inner.clone().dt().cast_time_unit(time_unit.0).into()
67    }
68
69    #[cfg(feature = "timezones")]
70    #[pyo3(signature = (time_zone, ambiguous, non_existent))]
71    fn dt_replace_time_zone(
72        &self,
73        time_zone: Option<String>,
74        ambiguous: Self,
75        non_existent: Wrap<NonExistent>,
76    ) -> PyResult<Self> {
77        use crate::utils::to_py_err;
78
79        Ok(self
80            .inner
81            .clone()
82            .dt()
83            .replace_time_zone(
84                TimeZone::opt_try_new(time_zone.map(PlSmallStr::from_string)).map_err(to_py_err)?,
85                ambiguous.inner,
86                non_existent.0,
87            )
88            .into())
89    }
90
91    fn dt_truncate(&self, every: Self) -> Self {
92        self.inner.clone().dt().truncate(every.inner).into()
93    }
94
95    fn dt_month_start(&self) -> Self {
96        self.inner.clone().dt().month_start().into()
97    }
98
99    fn dt_month_end(&self) -> Self {
100        self.inner.clone().dt().month_end().into()
101    }
102
103    #[cfg(feature = "timezones")]
104    fn dt_base_utc_offset(&self) -> Self {
105        self.inner.clone().dt().base_utc_offset().into()
106    }
107    #[cfg(feature = "timezones")]
108    fn dt_dst_offset(&self) -> Self {
109        self.inner.clone().dt().dst_offset().into()
110    }
111
112    fn dt_round(&self, every: Self) -> Self {
113        self.inner.clone().dt().round(every.inner).into()
114    }
115
116    fn dt_replace(
117        &self,
118        year: Self,
119        month: Self,
120        day: Self,
121        hour: Self,
122        minute: Self,
123        second: Self,
124        microsecond: Self,
125        ambiguous: Self,
126    ) -> Self {
127        self.inner
128            .clone()
129            .dt()
130            .replace(
131                year.inner,
132                month.inner,
133                day.inner,
134                hour.inner,
135                minute.inner,
136                second.inner,
137                microsecond.inner,
138                ambiguous.inner,
139            )
140            .into()
141    }
142
143    fn dt_combine(&self, time: Self, time_unit: Wrap<TimeUnit>) -> Self {
144        self.inner
145            .clone()
146            .dt()
147            .combine(time.inner, time_unit.0)
148            .into()
149    }
150    fn dt_millennium(&self) -> Self {
151        self.inner.clone().dt().millennium().into()
152    }
153    fn dt_century(&self) -> Self {
154        self.inner.clone().dt().century().into()
155    }
156    fn dt_year(&self) -> Self {
157        self.inner.clone().dt().year().into()
158    }
159    fn dt_is_business_day(&self, week_mask: [bool; 7], holidays: Vec<i32>) -> Self {
160        self.inner
161            .clone()
162            .dt()
163            .is_business_day(week_mask, holidays)
164            .into()
165    }
166    fn dt_is_leap_year(&self) -> Self {
167        self.inner.clone().dt().is_leap_year().into()
168    }
169    fn dt_iso_year(&self) -> Self {
170        self.inner.clone().dt().iso_year().into()
171    }
172    fn dt_quarter(&self) -> Self {
173        self.inner.clone().dt().quarter().into()
174    }
175    fn dt_month(&self) -> Self {
176        self.inner.clone().dt().month().into()
177    }
178    fn dt_week(&self) -> Self {
179        self.inner.clone().dt().week().into()
180    }
181    fn dt_weekday(&self) -> Self {
182        self.inner.clone().dt().weekday().into()
183    }
184    fn dt_day(&self) -> Self {
185        self.inner.clone().dt().day().into()
186    }
187    fn dt_ordinal_day(&self) -> Self {
188        self.inner.clone().dt().ordinal_day().into()
189    }
190    fn dt_time(&self) -> Self {
191        self.inner.clone().dt().time().into()
192    }
193    fn dt_date(&self) -> Self {
194        self.inner.clone().dt().date().into()
195    }
196    fn dt_datetime(&self) -> Self {
197        self.inner.clone().dt().datetime().into()
198    }
199    fn dt_hour(&self) -> Self {
200        self.inner.clone().dt().hour().into()
201    }
202    fn dt_minute(&self) -> Self {
203        self.inner.clone().dt().minute().into()
204    }
205    fn dt_second(&self) -> Self {
206        self.inner.clone().dt().second().into()
207    }
208    fn dt_millisecond(&self) -> Self {
209        self.inner.clone().dt().millisecond().into()
210    }
211    fn dt_microsecond(&self) -> Self {
212        self.inner.clone().dt().microsecond().into()
213    }
214    fn dt_nanosecond(&self) -> Self {
215        self.inner.clone().dt().nanosecond().into()
216    }
217    fn dt_timestamp(&self, time_unit: Wrap<TimeUnit>) -> Self {
218        self.inner.clone().dt().timestamp(time_unit.0).into()
219    }
220    fn dt_total_days(&self) -> Self {
221        self.inner.clone().dt().total_days().into()
222    }
223    fn dt_total_hours(&self) -> Self {
224        self.inner.clone().dt().total_hours().into()
225    }
226    fn dt_total_minutes(&self) -> Self {
227        self.inner.clone().dt().total_minutes().into()
228    }
229    fn dt_total_seconds(&self) -> Self {
230        self.inner.clone().dt().total_seconds().into()
231    }
232    fn dt_total_milliseconds(&self) -> Self {
233        self.inner.clone().dt().total_milliseconds().into()
234    }
235    fn dt_total_microseconds(&self) -> Self {
236        self.inner.clone().dt().total_microseconds().into()
237    }
238    fn dt_total_nanoseconds(&self) -> Self {
239        self.inner.clone().dt().total_nanoseconds().into()
240    }
241}