Skip to content

Commit

Permalink
python take collect into alignedvec
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Aug 5, 2021
1 parent 32a71e7 commit f733ea9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
7 changes: 6 additions & 1 deletion polars/polars-core/src/chunked_array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ pub mod strings;
pub mod temporal;
mod trusted_len;
pub mod upstream_traits;

use arrow::array::Array;

use crate::chunked_array::builder::categorical::RevMapping;
Expand Down Expand Up @@ -868,6 +867,12 @@ pub(crate) fn to_array<T: PolarsPrimitiveType>(
Arc::new(to_primitive::<T>(values, validity))
}

impl<T: PolarsNumericType> From<PrimitiveArray<T::Native>> for ChunkedArray<T> {
fn from(a: PrimitiveArray<T::Native>) -> Self {
ChunkedArray::new_from_chunks("", vec![Arc::new(a)])
}
}

#[cfg(test)]
pub(crate) mod test {
use crate::prelude::*;
Expand Down
12 changes: 12 additions & 0 deletions py-polars/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use pyo3::types::PySequence;
use pyo3::{PyAny, PyResult};
use std::fmt::{Display, Formatter};
use std::hash::{Hash, Hasher};
use polars_core::utils::arrow::datatypes::ArrowNativeType;

#[repr(transparent)]
pub struct Wrap<T>(pub T);
Expand Down Expand Up @@ -268,3 +269,14 @@ impl Default for ObjectValue {
}
}
}

impl<'a, T: ArrowNativeType + FromPyObject<'a>> FromPyObject<'a> for Wrap<AlignedVec<T>> {
fn extract(obj: &'a PyAny) -> PyResult<Self> {
let seq = <PySequence as PyTryFrom>::try_from(obj)?;
let mut v = AlignedVec::with_capacity(seq.len().unwrap_or(0) as usize);
for item in seq.iter()? {
v.push(item?.extract::<T>()?);
}
Ok(Wrap(v))
}
}
6 changes: 4 additions & 2 deletions py-polars/src/dataframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,10 +514,12 @@ impl PyDataFrame {
}
}

pub fn take(&self, indices: Vec<u32>) -> PyResult<Self> {
pub fn take(&self, indices: Wrap<AlignedVec<u32>>) -> PyResult<Self> {
let indices = indices.0;
let indices = indices.into_primitive_array::<UInt32Type>(None);
let df = self
.df
.take_iter(indices.iter().map(|i| *i as usize))
.take(&indices.into())
.map_err(PyPolarsEr::from)?;
Ok(PyDataFrame::new(df))
}
Expand Down
7 changes: 5 additions & 2 deletions py-polars/src/series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,10 +388,13 @@ impl PySeries {
self.series.arg_max()
}

pub fn take(&self, indices: Vec<usize>) -> PyResult<Self> {
pub fn take(&self, indices: Wrap<AlignedVec<u32>>) -> PyResult<Self> {
let indices = indices.0;
let indices = indices.into_primitive_array::<UInt32Type>(None);

let take = self
.series
.take_iter(&mut indices.iter().copied())
.take(&indices.into())
.map_err(PyPolarsEr::from)?;
Ok(PySeries::new(take))
}
Expand Down

0 comments on commit f733ea9

Please sign in to comment.