Skip to content
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

PM-6264: Fix handling nulls #5

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "polars-tdigest"
version = "0.1.4"
version = "0.1.5"
edition = "2021"

[lib]
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ For an example see the [Yellow Taxi Notebook](./tdigest_yellow_taxi.ipynb). Note

## Dev setup

Setup your virtual environment with a python version `>=3.8`, e.g. use `python -m venv .env`.
Setup your virtual environment with a python version `>=3.8`, e.g. use
```bash
python -m venv .env
source .env/bin/activate
``` .
Install the python dependencies used for development:
```bash
python -m pip install -r requirements.txt
Expand Down
21 changes: 12 additions & 9 deletions src/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ fn estimate_median(inputs: &[Series]) -> PolarsResult<Series> {
}

fn tdigest_output(_: &[Field]) -> PolarsResult<Field> {
let fields = vec![
Ok(Field::new("tdigest", DataType::Struct(tdigest_fields())))
}

fn tdigest_fields() -> Vec<Field> {
vec![
Field::new(
"centroids",
DataType::List(Box::new(DataType::Struct(vec![
Expand All @@ -76,9 +80,7 @@ fn tdigest_output(_: &[Field]) -> PolarsResult<Field> {
Field::new("max", DataType::Int64),
Field::new("count", DataType::Int64),
Field::new("max_size", DataType::Int64),
];

Ok(Field::new("tdigest", DataType::Struct(fields)))
]
}

// fn tidgest_compute<T: NumericNative, PDT: PolarsDataType>(values: &ChunkedArray<PDT>) -> Vec<TDigest> {
Expand All @@ -105,6 +107,7 @@ fn tdigest_output(_: &[Field]) -> PolarsResult<Field> {
#[polars_expr(output_type_func=tdigest_output)]
fn tdigest(inputs: &[Series]) -> PolarsResult<Series> {
let series = &inputs[0];
// TODO: pooling is not feasible on small datasets
let chunks = match series.dtype() {
DataType::Float64 => {
let values = series.f64()?;
Expand All @@ -115,7 +118,7 @@ fn tdigest(inputs: &[Series]) -> PolarsResult<Series> {
.map(|chunk| {
let t = TDigest::new_with_size(100);
let array = chunk.as_any().downcast_ref::<Float64Array>().unwrap();
let val_vec: Vec<f64> = array.values().iter().copied().collect();
let val_vec: Vec<f64> = array.non_null_values_iter().collect();
t.merge_unsorted(val_vec.to_owned())
})
.collect::<Vec<TDigest>>()
Expand All @@ -132,7 +135,7 @@ fn tdigest(inputs: &[Series]) -> PolarsResult<Series> {
let t = TDigest::new_with_size(100);
let array = chunk.as_any().downcast_ref::<Float32Array>().unwrap();
let val_vec: Vec<f64> =
array.values().iter().map(|v| (*v as f64)).collect();
array.non_null_values_iter().map(|v| (v as f64)).collect();
t.merge_unsorted(val_vec.to_owned())
})
.collect::<Vec<TDigest>>()
Expand All @@ -149,7 +152,7 @@ fn tdigest(inputs: &[Series]) -> PolarsResult<Series> {
let t = TDigest::new_with_size(100);
let array = chunk.as_any().downcast_ref::<Int64Array>().unwrap();
let val_vec: Vec<f64> =
array.values().iter().map(|v| (*v as f64)).collect();
array.non_null_values_iter().map(|v| (v as f64)).collect();
t.merge_unsorted(val_vec.to_owned())
})
.collect::<Vec<TDigest>>()
Expand All @@ -166,7 +169,7 @@ fn tdigest(inputs: &[Series]) -> PolarsResult<Series> {
let t = TDigest::new_with_size(100);
let array = chunk.as_any().downcast_ref::<Int32Array>().unwrap();
let val_vec: Vec<f64> =
array.values().iter().map(|v| (*v as f64)).collect();
array.non_null_values_iter().map(|v| (v as f64)).collect();
t.merge_unsorted(val_vec.to_owned())
})
.collect::<Vec<TDigest>>()
Expand All @@ -187,7 +190,7 @@ fn tdigest(inputs: &[Series]) -> PolarsResult<Series> {
let file = Cursor::new(&td_json);
let df = JsonReader::new(file)
.with_json_format(JsonFormat::JsonLines)
.infer_schema_len(Some(3))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inferring schema doesn't work well on empty output

.with_schema(Arc::new(Schema::from_iter(tdigest_fields())))
.with_batch_size(NonZeroUsize::new(3).unwrap())
.finish()
.unwrap();
Expand Down
Loading