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