Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-core/src/utils/series.rs
6940 views
1
use std::rc::Rc;
2
3
use crate::prelude::*;
4
use crate::series::amortized_iter::AmortSeries;
5
6
/// A utility that allocates an [`AmortSeries`]. The applied function can then use that
7
/// series container to save heap allocations and swap arrow arrays.
8
pub fn with_unstable_series<F, T>(dtype: &DataType, f: F) -> T
9
where
10
F: Fn(&mut AmortSeries) -> T,
11
{
12
let container = Series::full_null(PlSmallStr::EMPTY, 0, dtype);
13
let mut us = AmortSeries::new(Rc::new(container));
14
15
f(&mut us)
16
}
17
18
pub fn is_deprecated_cast(input_dtype: &DataType, output_dtype: &DataType) -> bool {
19
use DataType as D;
20
21
#[allow(clippy::single_match)]
22
match (input_dtype, output_dtype) {
23
#[cfg(feature = "dtype-struct")]
24
(D::Struct(l_fields), D::Struct(r_fields)) => {
25
l_fields.len() != r_fields.len()
26
|| l_fields
27
.iter()
28
.zip(r_fields.iter())
29
.any(|(l, r)| l.name() != r.name() || is_deprecated_cast(l.dtype(), r.dtype()))
30
},
31
(D::List(input_dtype), D::List(output_dtype)) => {
32
is_deprecated_cast(input_dtype, output_dtype)
33
},
34
#[cfg(feature = "dtype-array")]
35
(D::Array(input_dtype, _), D::Array(output_dtype, _)) => {
36
is_deprecated_cast(input_dtype, output_dtype)
37
},
38
#[cfg(feature = "dtype-array")]
39
(D::List(input_dtype), D::Array(output_dtype, _))
40
| (D::Array(input_dtype, _), D::List(output_dtype)) => {
41
is_deprecated_cast(input_dtype, output_dtype)
42
},
43
_ => false,
44
}
45
}
46
47
pub fn handle_casting_failures(input: &Series, output: &Series) -> PolarsResult<()> {
48
// @Hack to deal with deprecated cast
49
// @2.0
50
if is_deprecated_cast(input.dtype(), output.dtype()) {
51
return Ok(());
52
}
53
54
let mut idxs = Vec::new();
55
input.find_validity_mismatch(output, &mut idxs);
56
57
// Base case. No strict casting failed.
58
if idxs.is_empty() {
59
return Ok(());
60
}
61
62
let num_failures = idxs.len();
63
let failures = input.take_slice(&idxs[..num_failures.min(10)])?;
64
65
let additional_info = match (input.dtype(), output.dtype()) {
66
(DataType::String, DataType::Date | DataType::Datetime(_, _)) => {
67
"\n\nYou might want to try:\n\
68
- setting `strict=False` to set values that cannot be converted to `null`\n\
69
- using `str.strptime`, `str.to_date`, or `str.to_datetime` and providing a format string"
70
},
71
#[cfg(feature = "dtype-categorical")]
72
(DataType::String, DataType::Enum(_, _)) => {
73
"\n\nEnsure that all values in the input column are present in the categories of the enum datatype."
74
},
75
_ if failures.len() < num_failures => {
76
"\n\nDid not show all failed cases as there were too many."
77
},
78
_ => "",
79
};
80
81
polars_bail!(
82
InvalidOperation:
83
"conversion from `{}` to `{}` failed in column '{}' for {} out of {} values: {}{}",
84
input.dtype(),
85
output.dtype(),
86
output.name(),
87
num_failures,
88
input.len(),
89
failures.fmt_list(),
90
additional_info,
91
)
92
}
93
94