1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use arrow::array::{Array, PrimitiveArray};
use arrow::compute::cast::{cast, CastOptions};
use arrow::compute::temporal;
use arrow::error::Result as ArrowResult;
use polars_arrow::export::arrow;
use polars_core::prelude::*;
use super::*;
fn cast_and_apply<
F: Fn(&dyn Array) -> ArrowResult<PrimitiveArray<T::Native>>,
T: PolarsNumericType,
>(
ca: &DatetimeChunked,
func: F,
) -> ChunkedArray<T> {
let dtype = ca.dtype().to_arrow();
let chunks = ca
.downcast_iter()
.map(|arr| {
let arr = cast(
arr,
&dtype,
CastOptions {
wrapped: true,
partial: false,
},
)
.unwrap();
Box::from(func(&*arr).unwrap()) as ArrayRef
})
.collect();
unsafe { ChunkedArray::from_chunks(ca.name(), chunks) }
}
pub trait DatetimeMethods: AsDatetime {
fn year(&self) -> Int32Chunked {
cast_and_apply(self.as_datetime(), temporal::year)
}
fn iso_year(&self) -> Int32Chunked {
let ca = self.as_datetime();
let f = match ca.time_unit() {
TimeUnit::Nanoseconds => datetime_to_iso_year_ns,
TimeUnit::Microseconds => datetime_to_iso_year_us,
TimeUnit::Milliseconds => datetime_to_iso_year_ms,
};
ca.apply_kernel_cast::<Int32Type>(&f)
}
fn quarter(&self) -> UInt32Chunked {
let months = self.month();
months_to_quarters(months)
}
fn month(&self) -> UInt32Chunked {
cast_and_apply(self.as_datetime(), temporal::month)
}
fn weekday(&self) -> UInt32Chunked {
cast_and_apply(self.as_datetime(), temporal::weekday)
}
fn week(&self) -> UInt32Chunked {
cast_and_apply(self.as_datetime(), temporal::iso_week)
}
fn day(&self) -> UInt32Chunked {
cast_and_apply(self.as_datetime(), temporal::day)
}
fn hour(&self) -> UInt32Chunked {
cast_and_apply(self.as_datetime(), temporal::hour)
}
fn minute(&self) -> UInt32Chunked {
cast_and_apply(self.as_datetime(), temporal::minute)
}
fn second(&self) -> UInt32Chunked {
cast_and_apply(self.as_datetime(), temporal::second)
}
fn nanosecond(&self) -> UInt32Chunked {
cast_and_apply(self.as_datetime(), temporal::nanosecond)
}
fn ordinal(&self) -> UInt32Chunked {
let ca = self.as_datetime();
let f = match ca.time_unit() {
TimeUnit::Nanoseconds => datetime_to_ordinal_ns,
TimeUnit::Microseconds => datetime_to_ordinal_us,
TimeUnit::Milliseconds => datetime_to_ordinal_ms,
};
ca.apply_kernel_cast::<UInt32Type>(&f)
}
fn parse_from_str_slice(name: &str, v: &[&str], fmt: &str, tu: TimeUnit) -> DatetimeChunked {
let func = match tu {
TimeUnit::Nanoseconds => datetime_to_timestamp_ns,
TimeUnit::Microseconds => datetime_to_timestamp_us,
TimeUnit::Milliseconds => datetime_to_timestamp_ms,
};
Int64Chunked::from_iter_options(
name,
v.iter()
.map(|s| NaiveDateTime::parse_from_str(s, fmt).ok().map(func)),
)
.into_datetime(tu, None)
}
}
pub trait AsDatetime {
fn as_datetime(&self) -> &DatetimeChunked;
}
impl AsDatetime for DatetimeChunked {
fn as_datetime(&self) -> &DatetimeChunked {
self
}
}
impl DatetimeMethods for DatetimeChunked {}
#[cfg(test)]
mod test {
use chrono::NaiveDateTime;
use super::*;
#[test]
fn from_datetime() {
let datetimes: Vec<_> = [
"1988-08-25 00:00:16",
"2015-09-05 23:56:04",
"2012-12-21 00:00:00",
]
.iter()
.map(|s| NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M:%S").unwrap())
.collect();
let dt = DatetimeChunked::from_naive_datetime(
"name",
datetimes.iter().copied(),
TimeUnit::Nanoseconds,
);
assert_eq!(
[
588470416000_000_000,
1441497364000_000_000,
1356048000000_000_000
],
dt.cont_slice().unwrap()
);
}
}