Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-plan/src/dsl/functions/range.rs
6940 views
1
use polars_ops::series::ClosedInterval;
2
#[cfg(feature = "temporal")]
3
use polars_time::ClosedWindow;
4
5
use super::*;
6
7
/// Generate a range of integers.
8
///
9
/// Alias for `int_range`.
10
pub fn arange(start: Expr, end: Expr, step: i64, dtype: DataType) -> Expr {
11
int_range(start, end, step, dtype)
12
}
13
14
/// Generate a range of integers.
15
pub fn int_range(start: Expr, end: Expr, step: i64, dtype: impl Into<DataTypeExpr>) -> Expr {
16
Expr::n_ary(
17
RangeFunction::IntRange {
18
step,
19
dtype: dtype.into(),
20
},
21
vec![start, end],
22
)
23
}
24
25
/// Generate a range of integers for each row of the input columns.
26
pub fn int_ranges(start: Expr, end: Expr, step: Expr, dtype: impl Into<DataTypeExpr>) -> Expr {
27
Expr::n_ary(
28
RangeFunction::IntRanges {
29
dtype: dtype.into(),
30
},
31
vec![start, end, step],
32
)
33
}
34
35
/// Create a date range from a `start` and `stop` expression.
36
#[cfg(feature = "temporal")]
37
pub fn date_range(start: Expr, end: Expr, interval: Duration, closed: ClosedWindow) -> Expr {
38
Expr::n_ary(
39
RangeFunction::DateRange { interval, closed },
40
vec![start, end],
41
)
42
}
43
44
/// Create a column of date ranges from a `start` and `stop` expression.
45
#[cfg(feature = "temporal")]
46
pub fn date_ranges(start: Expr, end: Expr, interval: Duration, closed: ClosedWindow) -> Expr {
47
Expr::n_ary(
48
RangeFunction::DateRanges { interval, closed },
49
vec![start, end],
50
)
51
}
52
53
/// Create a datetime range from a `start` and `stop` expression.
54
#[cfg(feature = "dtype-datetime")]
55
pub fn datetime_range(
56
start: Expr,
57
end: Expr,
58
interval: Duration,
59
closed: ClosedWindow,
60
time_unit: Option<TimeUnit>,
61
time_zone: Option<TimeZone>,
62
) -> Expr {
63
Expr::n_ary(
64
RangeFunction::DatetimeRange {
65
interval,
66
closed,
67
time_unit,
68
time_zone,
69
},
70
vec![start, end],
71
)
72
}
73
74
/// Create a column of datetime ranges from a `start` and `stop` expression.
75
#[cfg(feature = "dtype-datetime")]
76
pub fn datetime_ranges(
77
start: Expr,
78
end: Expr,
79
interval: Duration,
80
closed: ClosedWindow,
81
time_unit: Option<TimeUnit>,
82
time_zone: Option<TimeZone>,
83
) -> Expr {
84
Expr::n_ary(
85
RangeFunction::DatetimeRanges {
86
interval,
87
closed,
88
time_unit,
89
time_zone,
90
},
91
vec![start, end],
92
)
93
}
94
95
/// Generate a time range.
96
#[cfg(feature = "dtype-time")]
97
pub fn time_range(start: Expr, end: Expr, interval: Duration, closed: ClosedWindow) -> Expr {
98
Expr::n_ary(
99
RangeFunction::TimeRange { interval, closed },
100
vec![start, end],
101
)
102
}
103
104
/// Create a column of time ranges from a `start` and `stop` expression.
105
#[cfg(feature = "dtype-time")]
106
pub fn time_ranges(start: Expr, end: Expr, interval: Duration, closed: ClosedWindow) -> Expr {
107
Expr::n_ary(
108
RangeFunction::TimeRanges { interval, closed },
109
vec![start, end],
110
)
111
}
112
113
/// Generate a series of equally-spaced points.
114
pub fn linear_space(start: Expr, end: Expr, num_samples: Expr, closed: ClosedInterval) -> Expr {
115
Expr::n_ary(
116
RangeFunction::LinearSpace { closed },
117
vec![start, end, num_samples],
118
)
119
}
120
121
/// Create a column of linearly-spaced sequences from 'start', 'end', and 'num_samples' expressions.
122
pub fn linear_spaces(
123
start: Expr,
124
end: Expr,
125
num_samples: Expr,
126
closed: ClosedInterval,
127
as_array: bool,
128
) -> PolarsResult<Expr> {
129
let mut input = Vec::<Expr>::with_capacity(3);
130
input.push(start);
131
input.push(end);
132
let array_width = if as_array {
133
Some(num_samples.extract_usize().map_err(|_| {
134
polars_err!(InvalidOperation: "'as_array' is only valid when 'num_samples' is a constant integer")
135
})?)
136
} else {
137
input.push(num_samples);
138
None
139
};
140
141
Ok(Expr::n_ary(
142
RangeFunction::LinearSpaces {
143
closed,
144
array_width,
145
},
146
input,
147
))
148
}
149
150