Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/docs/source/src/rust/user-guide/expressions/casting.rs
7889 views
1
fn main() -> Result<(), Box<dyn std::error::Error>> {
2
// --8<-- [start:dfnum]
3
use polars::prelude::*;
4
5
let df = df! (
6
"integers"=> [1, 2, 3],
7
"big_integers"=> [10000002, 2, 30000003],
8
"floats"=> [4.0, 5.8, -6.3],
9
)?;
10
11
println!("{df}");
12
// --8<-- [end:dfnum]
13
14
// --8<-- [start:castnum]
15
let result = df
16
.clone()
17
.lazy()
18
.select([
19
col("integers")
20
.cast(DataType::Float32)
21
.alias("integers_as_floats"),
22
col("floats")
23
.cast(DataType::Int32)
24
.alias("floats_as_integers"),
25
])
26
.collect()?;
27
println!("{result}");
28
// --8<-- [end:castnum]
29
30
// --8<-- [start:downcast]
31
println!("Before downcasting: {} bytes", df.estimated_size());
32
let result = df
33
.clone()
34
.lazy()
35
.with_columns([
36
col("integers").cast(DataType::Int16),
37
col("floats").cast(DataType::Float32),
38
])
39
.collect()?;
40
println!("After downcasting: {} bytes", result.estimated_size());
41
// --8<-- [end:downcast]
42
43
// --8<-- [start:overflow]
44
let result = df
45
.clone()
46
.lazy()
47
.select([col("big_integers").strict_cast(DataType::Int8)])
48
.collect();
49
if let Err(e) = result {
50
println!("{e}")
51
};
52
// --8<-- [end:overflow]
53
54
// --8<-- [start:overflow2]
55
let result = df
56
.lazy()
57
.select([col("big_integers").cast(DataType::Int8)])
58
.collect()?;
59
println!("{result}");
60
// --8<-- [end:overflow2]
61
62
// --8<-- [start:strings]
63
let df = df! (
64
"integers_as_strings" => ["1", "2", "3"],
65
"floats_as_strings" => ["4.0", "5.8", "-6.3"],
66
"floats" => [4.0, 5.8, -6.3],
67
)?;
68
69
let result = df
70
.lazy()
71
.select([
72
col("integers_as_strings").cast(DataType::Int32),
73
col("floats_as_strings").cast(DataType::Float64),
74
col("floats").cast(DataType::String),
75
])
76
.collect()?;
77
println!("{result}");
78
// --8<-- [end:strings]
79
80
// --8<-- [start:strings2]
81
let df = df! ("floats" => ["4.0", "5.8", "- 6 . 3"])?;
82
83
let result = df
84
.lazy()
85
.select([col("floats").strict_cast(DataType::Float64)])
86
.collect();
87
if let Err(e) = result {
88
println!("{e}")
89
};
90
// --8<-- [end:strings2]
91
92
// --8<-- [start:bool]
93
let df = df! (
94
"integers"=> [-1, 0, 2, 3, 4],
95
"floats"=> [0.0, 1.0, 2.0, 3.0, 4.0],
96
"bools"=> [true, false, true, false, true],
97
)?;
98
99
let result = df
100
.lazy()
101
.select([
102
col("integers").cast(DataType::Boolean),
103
col("floats").cast(DataType::Boolean),
104
col("bools").cast(DataType::UInt8),
105
])
106
.collect()?;
107
println!("{result}");
108
// --8<-- [end:bool]
109
110
// --8<-- [start:dates]
111
use chrono::prelude::*;
112
113
let df = df!(
114
"date" => [
115
NaiveDate::from_ymd_opt(1970, 1, 1).unwrap(), // epoch
116
NaiveDate::from_ymd_opt(1970, 1, 10).unwrap(), // 9 days later
117
],
118
"datetime" => [
119
NaiveDate::from_ymd_opt(1970, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap(), // epoch
120
NaiveDate::from_ymd_opt(1970, 1, 1).unwrap().and_hms_opt(0, 1, 0).unwrap(), // 1 minute later
121
],
122
"time" => [
123
NaiveTime::from_hms_opt(0, 0, 0).unwrap(), // reference time
124
NaiveTime::from_hms_opt(0, 0, 1).unwrap(), // 1 second later
125
]
126
)
127
.unwrap()
128
.lazy()
129
// Make the time unit match that of Python's for the same results.
130
.with_column(col("datetime").cast(DataType::Datetime(TimeUnit::Microseconds, None)))
131
.collect()?;
132
133
let result = df
134
.lazy()
135
.select([
136
col("date").cast(DataType::Int64).alias("days_since_epoch"),
137
col("datetime")
138
.cast(DataType::Int64)
139
.alias("us_since_epoch"),
140
col("time").cast(DataType::Int64).alias("ns_since_midnight"),
141
])
142
.collect()?;
143
println!("{result}");
144
// --8<-- [end:dates]
145
146
// --8<-- [start:dates2]
147
let df = df! (
148
"date" => [
149
NaiveDate::from_ymd_opt(2022, 1, 1).unwrap(),
150
NaiveDate::from_ymd_opt(2022, 1, 2).unwrap(),
151
],
152
"string" => [
153
"2022-01-01",
154
"2022-01-02",
155
],
156
)?;
157
158
let result = df
159
.lazy()
160
.select([
161
col("date").dt().to_string("%Y-%m-%d"),
162
col("string").str().to_datetime(
163
Some(TimeUnit::Microseconds),
164
None,
165
StrptimeOptions::default(),
166
lit("raise"),
167
),
168
])
169
.collect()?;
170
println!("{result}");
171
// --8<-- [end:dates2]
172
173
Ok(())
174
}
175
176