-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add linear_space
#20678
Open
mcrumiller
wants to merge
5
commits into
pola-rs:main
Choose a base branch
from
mcrumiller:linear-space
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+614
−13
Open
feat: Add linear_space
#20678
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9802899
Add linear_space
mcrumiller 645b6ee
Add missing func
mcrumiller 97bbae8
Add more dtypes and tests
mcrumiller 6627366
Make 'num_samples' expr and add docstring
mcrumiller 8d6f682
Add special condition message for streaming test
mcrumiller File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
use polars_core::prelude::*; | ||
use polars_core::series::IsSorted; | ||
#[cfg(feature = "serde")] | ||
use serde::{Deserialize, Serialize}; | ||
use strum_macros::IntoStaticStr; | ||
|
||
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Default, IntoStaticStr)] | ||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] | ||
#[strum(serialize_all = "snake_case")] | ||
pub enum ClosedInterval { | ||
#[default] | ||
Both, | ||
Left, | ||
Right, | ||
None, | ||
} | ||
|
||
pub fn new_linear_space_f32( | ||
start: f32, | ||
end: f32, | ||
n: u64, | ||
closed: ClosedInterval, | ||
name: PlSmallStr, | ||
) -> PolarsResult<Series> { | ||
let mut ca = match n { | ||
0 => Float32Chunked::full_null(name, 0), | ||
1 => match closed { | ||
ClosedInterval::None => Float32Chunked::from_slice(name, &[(end + start) * 0.5]), | ||
ClosedInterval::Left | ClosedInterval::Both => { | ||
Float32Chunked::from_slice(name, &[start]) | ||
}, | ||
ClosedInterval::Right => Float32Chunked::from_slice(name, &[end]), | ||
}, | ||
_ => Float32Chunked::from_iter_values(name, { | ||
let span = end - start; | ||
|
||
let (start, d, end) = match closed { | ||
ClosedInterval::None => { | ||
let d = span / (n + 1) as f32; | ||
(start + d, d, end - d) | ||
}, | ||
ClosedInterval::Left => (start, span / n as f32, end - span / n as f32), | ||
ClosedInterval::Right => (start + span / n as f32, span / n as f32, end), | ||
ClosedInterval::Both => (start, span / (n - 1) as f32, end), | ||
}; | ||
(0..n - 1) | ||
.map(move |v| (v as f32 * d) + start) | ||
.chain(std::iter::once(end)) // ensures floating point accuracy of final value | ||
}), | ||
}; | ||
|
||
let is_sorted = if end < start { | ||
IsSorted::Descending | ||
} else { | ||
IsSorted::Ascending | ||
}; | ||
ca.set_sorted_flag(is_sorted); | ||
|
||
Ok(ca.into_series()) | ||
} | ||
|
||
pub fn new_linear_space_f64( | ||
start: f64, | ||
end: f64, | ||
n: u64, | ||
closed: ClosedInterval, | ||
name: PlSmallStr, | ||
) -> PolarsResult<Series> { | ||
let mut ca = match n { | ||
0 => Float64Chunked::full_null(name, 0), | ||
1 => match closed { | ||
ClosedInterval::None => Float64Chunked::from_slice(name, &[(end + start) * 0.5]), | ||
ClosedInterval::Left | ClosedInterval::Both => { | ||
Float64Chunked::from_slice(name, &[start]) | ||
}, | ||
ClosedInterval::Right => Float64Chunked::from_slice(name, &[end]), | ||
}, | ||
_ => Float64Chunked::from_iter_values(name, { | ||
let span = end - start; | ||
|
||
let (start, d, end) = match closed { | ||
ClosedInterval::None => { | ||
let d = span / (n + 1) as f64; | ||
(start + d, d, end - d) | ||
}, | ||
ClosedInterval::Left => (start, span / n as f64, end - span / n as f64), | ||
ClosedInterval::Right => (start + span / n as f64, span / n as f64, end), | ||
ClosedInterval::Both => (start, span / (n - 1) as f64, end), | ||
}; | ||
(0..n - 1) | ||
.map(move |v| (v as f64 * d) + start) | ||
.chain(std::iter::once(end)) // ensures floating point accuracy of final value | ||
}), | ||
}; | ||
|
||
let is_sorted = if end < start { | ||
IsSorted::Descending | ||
} else { | ||
IsSorted::Ascending | ||
}; | ||
ca.set_sorted_flag(is_sorted); | ||
|
||
Ok(ca.into_series()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
crates/polars-plan/src/dsl/function_expr/range/linear_space.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use arrow::temporal_conversions::MILLISECONDS_IN_DAY; | ||
use polars_core::prelude::*; | ||
use polars_ops::series::{new_linear_space_f32, new_linear_space_f64, ClosedInterval}; | ||
|
||
use super::utils::ensure_range_bounds_contain_exactly_one_value; | ||
|
||
pub(super) fn linear_space(s: &[Column], closed: ClosedInterval) -> PolarsResult<Column> { | ||
let start = &s[0]; | ||
let end = &s[1]; | ||
let num_samples = &s[2]; | ||
let name = start.name(); | ||
|
||
ensure_range_bounds_contain_exactly_one_value(start, end)?; | ||
polars_ensure!( | ||
num_samples.len() == 1, | ||
ComputeError: "`num_samples` must contain exactly one value, got {} values", num_samples.len() | ||
); | ||
|
||
let start = start.get(0).unwrap(); | ||
let end = end.get(0).unwrap(); | ||
let num_samples = num_samples.get(0).unwrap(); | ||
let num_samples = num_samples | ||
.extract::<u64>() | ||
.ok_or(PolarsError::ComputeError( | ||
format!( | ||
"'num_samples' must be non-negative integer, got {}", | ||
num_samples | ||
) | ||
.into(), | ||
))?; | ||
|
||
match (start.dtype(), end.dtype()) { | ||
(DataType::Float32, DataType::Float32) => new_linear_space_f32( | ||
start.extract::<f32>().unwrap(), | ||
end.extract::<f32>().unwrap(), | ||
num_samples, | ||
closed, | ||
name.clone(), | ||
), | ||
(mut dt, dt2) if dt.is_temporal() && dt == dt2 => { | ||
let mut start = start.extract::<i64>().unwrap(); | ||
let mut end = end.extract::<i64>().unwrap(); | ||
|
||
// A linear space of a Date produces a sequence of Datetimes, so we must upcast. | ||
if dt == DataType::Date { | ||
start *= MILLISECONDS_IN_DAY; | ||
end *= MILLISECONDS_IN_DAY; | ||
dt = DataType::Datetime(TimeUnit::Milliseconds, None); | ||
} | ||
new_linear_space_f64(start as f64, end as f64, num_samples, closed, name.clone()) | ||
.map(|s| s.cast(&dt).unwrap()) | ||
}, | ||
(dt1, dt2) if !dt1.is_primitive_numeric() || !dt2.is_primitive_numeric() => { | ||
Err(PolarsError::ComputeError( | ||
format!( | ||
"'start' and 'end' have incompatible dtypes, got {:?} and {:?}", | ||
dt1, dt2 | ||
) | ||
.into(), | ||
)) | ||
}, | ||
(_, _) => new_linear_space_f64( | ||
start.extract::<f64>().unwrap(), | ||
end.extract::<f64>().unwrap(), | ||
num_samples, | ||
closed, | ||
name.clone(), | ||
), | ||
} | ||
.map(Column::from) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved
ClosedInterval
intolinear_space
since it's feature-gated here and I wanted to re-use it. I'm not sure if there's a better place for it.