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