-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reduced pyarrow dependency: series dtype list from sequence
- Loading branch information
Showing
7 changed files
with
206 additions
and
18 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use crate::conversion::get_pyseq; | ||
use crate::utils::str_to_polarstype; | ||
use polars::prelude::*; | ||
use polars_core::utils::CustomIterTools; | ||
use pyo3::{PyAny, PyResult}; | ||
|
||
pub fn py_seq_to_list(name: &str, seq: &PyAny, dtype: &PyAny) -> PyResult<Series> { | ||
let str_repr = dtype.str().unwrap().to_str().unwrap(); | ||
let dtype = str_to_polarstype(str_repr); | ||
|
||
let (seq, len) = get_pyseq(seq)?; | ||
let s = match dtype { | ||
DataType::Int64 => { | ||
let mut builder = ListPrimitiveChunkedBuilder::<Int64Type>::new(name, len, len * 5); | ||
for sub_seq in seq.iter()? { | ||
let sub_seq = sub_seq?; | ||
let (sub_seq, len) = get_pyseq(sub_seq)?; | ||
let iter = sub_seq | ||
.iter()? | ||
.map(|v| { | ||
let v = v.unwrap(); | ||
if v.is_none() { | ||
None | ||
} else { | ||
Some(v.extract::<i64>().unwrap()) | ||
} | ||
}) | ||
.trust_my_length(len); | ||
builder.append_iter(iter) | ||
} | ||
builder.finish().into_series() | ||
} | ||
DataType::Float64 => { | ||
let mut builder = ListPrimitiveChunkedBuilder::<Float64Type>::new(name, len, len * 5); | ||
for sub_seq in seq.iter()? { | ||
let sub_seq = sub_seq?; | ||
let (sub_seq, len) = get_pyseq(sub_seq)?; | ||
let iter = sub_seq | ||
.iter()? | ||
.map(|v| { | ||
let v = v.unwrap(); | ||
if v.is_none() { | ||
None | ||
} else { | ||
Some(v.extract::<f64>().unwrap()) | ||
} | ||
}) | ||
.trust_my_length(len); | ||
builder.append_iter(iter) | ||
} | ||
builder.finish().into_series() | ||
} | ||
DataType::Boolean => { | ||
let mut builder = ListBooleanChunkedBuilder::new(name, len, len * 5); | ||
for sub_seq in seq.iter()? { | ||
let sub_seq = sub_seq?; | ||
let (sub_seq, len) = get_pyseq(sub_seq)?; | ||
let iter = sub_seq | ||
.iter()? | ||
.map(|v| { | ||
let v = v.unwrap(); | ||
if v.is_none() { | ||
None | ||
} else { | ||
Some(v.extract::<bool>().unwrap()) | ||
} | ||
}) | ||
.trust_my_length(len); | ||
builder.append_iter(iter) | ||
} | ||
builder.finish().into_series() | ||
} | ||
DataType::Utf8 => { | ||
let mut builder = ListUtf8ChunkedBuilder::new(name, len, len * 5); | ||
for sub_seq in seq.iter()? { | ||
let sub_seq = sub_seq?; | ||
let (sub_seq, len) = get_pyseq(sub_seq)?; | ||
let iter = sub_seq | ||
.iter()? | ||
.map(|v| { | ||
let v = v.unwrap(); | ||
if v.is_none() { | ||
None | ||
} else { | ||
Some(v.extract::<&str>().unwrap()) | ||
} | ||
}) | ||
.trust_my_length(len); | ||
builder.append_iter(iter) | ||
} | ||
builder.finish().into_series() | ||
} | ||
dt => { | ||
panic!("cannot create list array from {:?}", dt); | ||
} | ||
}; | ||
|
||
Ok(s) | ||
} |
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