Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-plan/src/dsl/dt.rs
6939 views
1
use super::*;
2
3
/// Specialized expressions for [`Series`] with dates/datetimes.
4
pub struct DateLikeNameSpace(pub(crate) Expr);
5
6
impl DateLikeNameSpace {
7
/// Add a given number of business days.
8
#[cfg(feature = "business")]
9
pub fn add_business_days(
10
self,
11
n: Expr,
12
week_mask: [bool; 7],
13
holidays: Vec<i32>,
14
roll: Roll,
15
) -> Expr {
16
self.0.map_binary(
17
FunctionExpr::Business(BusinessFunction::AddBusinessDay {
18
week_mask,
19
holidays,
20
roll,
21
}),
22
n,
23
)
24
}
25
26
/// Convert from Date/Time/Datetime into String with the given format.
27
/// See [chrono strftime/strptime](https://docs.rs/chrono/0.4.19/chrono/format/strftime/index.html).
28
pub fn to_string(self, format: &str) -> Expr {
29
let format = format.to_string();
30
self.0
31
.map_unary(FunctionExpr::TemporalExpr(TemporalFunction::ToString(
32
format,
33
)))
34
}
35
36
/// Convert from Date/Time/Datetime into String with the given format.
37
/// See [chrono strftime/strptime](https://docs.rs/chrono/0.4.19/chrono/format/strftime/index.html).
38
///
39
/// Alias for `to_string`.
40
pub fn strftime(self, format: &str) -> Expr {
41
self.to_string(format)
42
}
43
44
/// Change the underlying [`TimeUnit`]. And update the data accordingly.
45
pub fn cast_time_unit(self, tu: TimeUnit) -> Expr {
46
self.0
47
.map_unary(FunctionExpr::TemporalExpr(TemporalFunction::CastTimeUnit(
48
tu,
49
)))
50
}
51
52
/// Change the underlying [`TimeUnit`] of the [`Series`]. This does not modify the data.
53
pub fn with_time_unit(self, tu: TimeUnit) -> Expr {
54
self.0
55
.map_unary(FunctionExpr::TemporalExpr(TemporalFunction::WithTimeUnit(
56
tu,
57
)))
58
}
59
60
/// Change the underlying [`TimeZone`] of the [`Series`]. This does not modify the data.
61
#[cfg(feature = "timezones")]
62
pub fn convert_time_zone(self, time_zone: TimeZone) -> Expr {
63
self.0.map_unary(FunctionExpr::TemporalExpr(
64
TemporalFunction::ConvertTimeZone(time_zone),
65
))
66
}
67
68
/// Get the millennium of a Date/Datetime
69
pub fn millennium(self) -> Expr {
70
self.0
71
.map_unary(FunctionExpr::TemporalExpr(TemporalFunction::Millennium))
72
}
73
74
/// Get the century of a Date/Datetime
75
pub fn century(self) -> Expr {
76
self.0
77
.map_unary(FunctionExpr::TemporalExpr(TemporalFunction::Century))
78
}
79
80
/// Get the year of a Date/Datetime
81
pub fn year(self) -> Expr {
82
self.0
83
.map_unary(FunctionExpr::TemporalExpr(TemporalFunction::Year))
84
}
85
86
/// Determine whether days are business days.
87
#[cfg(feature = "business")]
88
pub fn is_business_day(self, week_mask: [bool; 7], holidays: Vec<i32>) -> Expr {
89
self.0
90
.map_unary(FunctionExpr::Business(BusinessFunction::IsBusinessDay {
91
week_mask,
92
holidays,
93
}))
94
}
95
96
// Compute whether the year of a Date/Datetime is a leap year.
97
pub fn is_leap_year(self) -> Expr {
98
self.0
99
.map_unary(FunctionExpr::TemporalExpr(TemporalFunction::IsLeapYear))
100
}
101
102
/// Get the iso-year of a Date/Datetime.
103
/// This may not correspond with a calendar year.
104
pub fn iso_year(self) -> Expr {
105
self.0
106
.map_unary(FunctionExpr::TemporalExpr(TemporalFunction::IsoYear))
107
}
108
109
/// Get the month of a Date/Datetime.
110
pub fn month(self) -> Expr {
111
self.0
112
.map_unary(FunctionExpr::TemporalExpr(TemporalFunction::Month))
113
}
114
115
/// Get the number of days in the month of a Date/Datetime.
116
pub fn days_in_month(self) -> Expr {
117
self.0
118
.map_unary(FunctionExpr::TemporalExpr(TemporalFunction::DaysInMonth))
119
}
120
121
/// Extract quarter from underlying NaiveDateTime representation.
122
/// Quarters range from 1 to 4.
123
pub fn quarter(self) -> Expr {
124
self.0
125
.map_unary(FunctionExpr::TemporalExpr(TemporalFunction::Quarter))
126
}
127
128
/// Extract the week from the underlying Date representation.
129
/// Can be performed on Date and Datetime
130
///
131
/// Returns the ISO week number starting from 1.
132
/// The return value ranges from 1 to 53. (The last week of year differs by years.)
133
pub fn week(self) -> Expr {
134
self.0
135
.map_unary(FunctionExpr::TemporalExpr(TemporalFunction::Week))
136
}
137
138
/// Extract the ISO week day from the underlying Date representation.
139
/// Can be performed on Date and Datetime.
140
///
141
/// Returns the weekday number where monday = 1 and sunday = 7
142
pub fn weekday(self) -> Expr {
143
self.0
144
.map_unary(FunctionExpr::TemporalExpr(TemporalFunction::WeekDay))
145
}
146
147
/// Get the month of a Date/Datetime.
148
pub fn day(self) -> Expr {
149
self.0
150
.map_unary(FunctionExpr::TemporalExpr(TemporalFunction::Day))
151
}
152
153
/// Get the ordinal_day of a Date/Datetime.
154
pub fn ordinal_day(self) -> Expr {
155
self.0
156
.map_unary(FunctionExpr::TemporalExpr(TemporalFunction::OrdinalDay))
157
}
158
159
/// Get the (local) time of a Date/Datetime/Time.
160
pub fn time(self) -> Expr {
161
self.0
162
.map_unary(FunctionExpr::TemporalExpr(TemporalFunction::Time))
163
}
164
165
/// Get the (local) date of a Date/Datetime.
166
pub fn date(self) -> Expr {
167
self.0
168
.map_unary(FunctionExpr::TemporalExpr(TemporalFunction::Date))
169
}
170
171
/// Get the (local) datetime of a Datetime.
172
pub fn datetime(self) -> Expr {
173
self.0
174
.map_unary(FunctionExpr::TemporalExpr(TemporalFunction::Datetime))
175
}
176
177
/// Get the hour of a Datetime/Time64.
178
pub fn hour(self) -> Expr {
179
self.0
180
.map_unary(FunctionExpr::TemporalExpr(TemporalFunction::Hour))
181
}
182
183
/// Get the minute of a Datetime/Time64.
184
pub fn minute(self) -> Expr {
185
self.0
186
.map_unary(FunctionExpr::TemporalExpr(TemporalFunction::Minute))
187
}
188
189
/// Get the second of a Datetime/Time64.
190
pub fn second(self) -> Expr {
191
self.0
192
.map_unary(FunctionExpr::TemporalExpr(TemporalFunction::Second))
193
}
194
195
/// Get the millisecond of a Time64 (scaled from nanosecs).
196
pub fn millisecond(self) -> Expr {
197
self.0
198
.map_unary(FunctionExpr::TemporalExpr(TemporalFunction::Millisecond))
199
}
200
201
/// Get the microsecond of a Time64 (scaled from nanosecs).
202
pub fn microsecond(self) -> Expr {
203
self.0
204
.map_unary(FunctionExpr::TemporalExpr(TemporalFunction::Microsecond))
205
}
206
207
/// Get the nanosecond part of a Time64.
208
pub fn nanosecond(self) -> Expr {
209
self.0
210
.map_unary(FunctionExpr::TemporalExpr(TemporalFunction::Nanosecond))
211
}
212
213
/// Return the timestamp (UNIX epoch) of a Datetime/Date.
214
pub fn timestamp(self, tu: TimeUnit) -> Expr {
215
self.0
216
.map_unary(FunctionExpr::TemporalExpr(TemporalFunction::TimeStamp(tu)))
217
}
218
219
/// Truncate the Datetime/Date range into buckets.
220
pub fn truncate(self, every: Expr) -> Expr {
221
self.0.map_binary(
222
FunctionExpr::TemporalExpr(TemporalFunction::Truncate),
223
every,
224
)
225
}
226
227
/// Roll backward to the first day of the month.
228
#[cfg(feature = "month_start")]
229
pub fn month_start(self) -> Expr {
230
self.0
231
.map_unary(FunctionExpr::TemporalExpr(TemporalFunction::MonthStart))
232
}
233
234
/// Roll forward to the last day of the month.
235
#[cfg(feature = "month_end")]
236
pub fn month_end(self) -> Expr {
237
self.0
238
.map_unary(FunctionExpr::TemporalExpr(TemporalFunction::MonthEnd))
239
}
240
241
/// Get the base offset from UTC.
242
#[cfg(feature = "timezones")]
243
pub fn base_utc_offset(self) -> Expr {
244
self.0
245
.map_unary(FunctionExpr::TemporalExpr(TemporalFunction::BaseUtcOffset))
246
}
247
248
/// Get the additional offset from UTC currently in effect (usually due to daylight saving time).
249
#[cfg(feature = "timezones")]
250
pub fn dst_offset(self) -> Expr {
251
self.0
252
.map_unary(FunctionExpr::TemporalExpr(TemporalFunction::DSTOffset))
253
}
254
255
/// Round the Datetime/Date range into buckets.
256
pub fn round(self, every: Expr) -> Expr {
257
self.0
258
.map_binary(FunctionExpr::TemporalExpr(TemporalFunction::Round), every)
259
}
260
261
/// Offset this `Date/Datetime` by a given offset [`Duration`].
262
/// This will take leap years/ months into account.
263
#[cfg(feature = "offset_by")]
264
pub fn offset_by(self, by: Expr) -> Expr {
265
self.0
266
.map_binary(FunctionExpr::TemporalExpr(TemporalFunction::OffsetBy), by)
267
}
268
269
#[cfg(feature = "timezones")]
270
pub fn replace_time_zone(
271
self,
272
time_zone: Option<TimeZone>,
273
ambiguous: Expr,
274
non_existent: NonExistent,
275
) -> Expr {
276
self.0.map_binary(
277
FunctionExpr::TemporalExpr(TemporalFunction::ReplaceTimeZone(time_zone, non_existent)),
278
ambiguous,
279
)
280
}
281
282
/// Combine an existing Date/Datetime with a Time, creating a new Datetime value.
283
pub fn combine(self, time: Expr, tu: TimeUnit) -> Expr {
284
self.0.map_binary(
285
FunctionExpr::TemporalExpr(TemporalFunction::Combine(tu)),
286
time,
287
)
288
}
289
290
/// Express a Duration in terms of its total number of integer days.
291
#[cfg(feature = "dtype-duration")]
292
pub fn total_days(self) -> Expr {
293
self.0
294
.map_unary(FunctionExpr::TemporalExpr(TemporalFunction::TotalDays))
295
}
296
297
/// Express a Duration in terms of its total number of integer hours.
298
#[cfg(feature = "dtype-duration")]
299
pub fn total_hours(self) -> Expr {
300
self.0
301
.map_unary(FunctionExpr::TemporalExpr(TemporalFunction::TotalHours))
302
}
303
304
/// Express a Duration in terms of its total number of integer minutes.
305
#[cfg(feature = "dtype-duration")]
306
pub fn total_minutes(self) -> Expr {
307
self.0
308
.map_unary(FunctionExpr::TemporalExpr(TemporalFunction::TotalMinutes))
309
}
310
311
/// Express a Duration in terms of its total number of integer seconds.
312
#[cfg(feature = "dtype-duration")]
313
pub fn total_seconds(self) -> Expr {
314
self.0
315
.map_unary(FunctionExpr::TemporalExpr(TemporalFunction::TotalSeconds))
316
}
317
318
/// Express a Duration in terms of its total number of milliseconds.
319
#[cfg(feature = "dtype-duration")]
320
pub fn total_milliseconds(self) -> Expr {
321
self.0.map_unary(FunctionExpr::TemporalExpr(
322
TemporalFunction::TotalMilliseconds,
323
))
324
}
325
326
/// Express a Duration in terms of its total number of microseconds.
327
#[cfg(feature = "dtype-duration")]
328
pub fn total_microseconds(self) -> Expr {
329
self.0.map_unary(FunctionExpr::TemporalExpr(
330
TemporalFunction::TotalMicroseconds,
331
))
332
}
333
334
/// Express a Duration in terms of its total number of nanoseconds.
335
#[cfg(feature = "dtype-duration")]
336
pub fn total_nanoseconds(self) -> Expr {
337
self.0.map_unary(FunctionExpr::TemporalExpr(
338
TemporalFunction::TotalNanoseconds,
339
))
340
}
341
342
/// Replace the time units of a value
343
#[allow(clippy::too_many_arguments)]
344
pub fn replace(
345
self,
346
year: Expr,
347
month: Expr,
348
day: Expr,
349
hour: Expr,
350
minute: Expr,
351
second: Expr,
352
microsecond: Expr,
353
ambiguous: Expr,
354
) -> Expr {
355
self.0.map_n_ary(
356
FunctionExpr::TemporalExpr(TemporalFunction::Replace),
357
[
358
year,
359
month,
360
day,
361
hour,
362
minute,
363
second,
364
microsecond,
365
ambiguous,
366
],
367
)
368
}
369
}
370
371