polars_core/scalar/
new.rs1use std::sync::Arc;
2
3use polars_error::PolarsResult;
4use polars_utils::pl_str::PlSmallStr;
5
6use super::Scalar;
7use crate::datatypes::time_unit::TimeUnit;
8use crate::prelude::{AnyValue, DataType, TimeZone};
9use crate::series::Series;
10
11impl Scalar {
12 #[cfg(feature = "dtype-date")]
13 pub fn new_date(value: i32) -> Self {
14 Scalar::new(DataType::Date, AnyValue::Date(value))
15 }
16
17 #[cfg(feature = "dtype-datetime")]
18 pub fn new_datetime(value: i64, time_unit: TimeUnit, tz: Option<TimeZone>) -> Self {
19 Scalar::new(
20 DataType::Datetime(time_unit, tz.clone()),
21 AnyValue::DatetimeOwned(value, time_unit, tz.map(Arc::new)),
22 )
23 }
24
25 #[cfg(feature = "dtype-duration")]
26 pub fn new_duration(value: i64, time_unit: TimeUnit) -> Self {
27 Scalar::new(
28 DataType::Duration(time_unit),
29 AnyValue::Duration(value, time_unit),
30 )
31 }
32
33 #[cfg(feature = "dtype-time")]
34 pub fn new_time(value: i64) -> Self {
35 Scalar::new(DataType::Time, AnyValue::Time(value))
36 }
37
38 pub fn new_list(values: Series) -> Self {
39 Scalar::new(
40 DataType::List(Box::new(values.dtype().clone())),
41 AnyValue::List(values),
42 )
43 }
44
45 #[cfg(feature = "dtype-map")]
47 pub fn new_map(keys: &Series, values: &Series) -> Self {
48 Scalar::new(
49 DataType::Map(
50 Box::new(keys.dtype().clone()),
51 Box::new(values.dtype().clone()),
52 ),
53 AnyValue::Map(crate::chunked_array::logical::pack_map_entries(
54 keys, values,
55 )),
56 )
57 }
58
59 #[cfg(feature = "dtype-map")]
61 pub(crate) fn map_from_entries(entries: Series) -> Self {
62 let value = AnyValue::Map(entries);
63 Scalar::new(value.dtype(), value)
64 }
65
66 #[cfg(feature = "dtype-array")]
67 pub fn new_array(values: Series, width: usize) -> Self {
68 Scalar::new(
69 DataType::Array(Box::new(values.dtype().clone()), width),
70 AnyValue::Array(values, width),
71 )
72 }
73
74 #[cfg(feature = "dtype-decimal")]
75 pub fn new_decimal(value: i128, precision: usize, scale: usize) -> Self {
76 Scalar::new(
77 DataType::Decimal(precision, scale),
78 AnyValue::Decimal(value, precision, scale),
79 )
80 }
81
82 #[cfg(feature = "dtype-categorical")]
83 pub fn new_enum(
84 value: polars_dtype::categorical::CatSize,
85 categories: &arrow::array::Utf8ViewArray,
86 ) -> PolarsResult<Self> {
87 use arrow::array::Array;
88 use polars_dtype::categorical::FrozenCategories;
89
90 assert_eq!(categories.null_count(), 0);
91
92 let categories = FrozenCategories::new(categories.values_iter())?;
93 let mapping = categories.mapping();
94 Ok(Scalar::new(
95 DataType::Enum(categories.clone(), mapping.clone()),
96 AnyValue::EnumOwned(value, mapping.clone()),
97 ))
98 }
99
100 #[cfg(feature = "dtype-categorical")]
101 pub fn new_categorical(
102 value: &str,
103 name: PlSmallStr,
104 namespace: PlSmallStr,
105 physical: polars_dtype::categorical::CategoricalPhysical,
106 ) -> PolarsResult<Self> {
107 use polars_dtype::categorical::Categories;
108
109 let categories = Categories::new(name, namespace, physical);
110 let dt_mapping = categories.mapping();
111 let av_mapping = categories.mapping();
112
113 let value = av_mapping.insert_cat(value)?;
114
115 Ok(Scalar::new(
116 DataType::Categorical(categories, dt_mapping),
117 AnyValue::CategoricalOwned(value, av_mapping),
118 ))
119 }
120}