Skip to content

Commit

Permalink
Add series::from_vec
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Feb 4, 2022
1 parent 53365be commit 3d5a865
Show file tree
Hide file tree
Showing 19 changed files with 59 additions and 40 deletions.
2 changes: 1 addition & 1 deletion nodejs-polars/src/dataframe/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ pub fn take(cx: CallContext) -> JsResult<JsExternal> {
})
.collect();

let indices = UInt32Chunked::new_from_aligned_vec("", indices);
let indices = UInt32Chunked::from_vec("", indices);
df.take(&indices)
.map_err(JsPolarsEr::from)?
.try_into_js(&cx)
Expand Down
2 changes: 1 addition & 1 deletion nodejs-polars/src/list_construction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ macro_rules! typed_to_chunked {
let v: &[$type] = $arr.as_ref();
let mut buffer = Vec::<$type>::new();
buffer.extend_from_slice(v);
ChunkedArray::<$pl_type>::new_from_aligned_vec("", buffer)
ChunkedArray::<$pl_type>::from_vec("", buffer)
}};
}

Expand Down
2 changes: 1 addition & 1 deletion polars/polars-core/src/chunked_array/list/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl ListChunked {
last = *o;
}
});
UInt32Chunked::new_from_aligned_vec(self.name(), lengths)
UInt32Chunked::from_vec(self.name(), lengths)
}

/// Get the value by index in the sublists.
Expand Down
2 changes: 1 addition & 1 deletion polars/polars-core/src/chunked_array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ where
T: PolarsNumericType,
{
/// Create a new ChunkedArray by taking ownership of the Vec. This operation is zero copy.
pub fn new_from_aligned_vec(name: &str, v: Vec<T::Native>) -> Self {
pub fn from_vec(name: &str, v: Vec<T::Native>) -> Self {
let arr = to_array::<T>(v, None);
Self::new_from_chunks(name, vec![arr])
}
Expand Down
2 changes: 1 addition & 1 deletion polars/polars-core/src/chunked_array/ops/interpolate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ where
);
Self::new_from_chunks(self.name(), vec![Arc::new(array)])
} else {
Self::new_from_aligned_vec(self.name(), av)
Self::from_vec(self.name(), av)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion polars/polars-core/src/chunked_array/ops/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ where
*val = value;
Ok(())
})?;
return Ok(Self::new_from_aligned_vec(self.name(), av));
return Ok(Self::from_vec(self.name(), av));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion polars/polars-core/src/chunked_array/ops/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ where
);
}

ChunkedArray::new_from_aligned_vec(self.name(), vals)
ChunkedArray::from_vec(self.name(), vals)
} else {
let null_count = self.null_count();
let len = self.len();
Expand Down
19 changes: 5 additions & 14 deletions polars/polars-core/src/chunked_array/ops/unique/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,7 @@ where
}

fn arg_unique(&self) -> Result<UInt32Chunked> {
Ok(UInt32Chunked::new_from_aligned_vec(
self.name(),
arg_unique_ca!(self),
))
Ok(UInt32Chunked::from_vec(self.name(), arg_unique_ca!(self)))
}

fn is_unique(&self) -> Result<BooleanChunked> {
Expand Down Expand Up @@ -242,10 +239,7 @@ impl ChunkUnique<Utf8Type> for Utf8Chunked {
}

fn arg_unique(&self) -> Result<UInt32Chunked> {
Ok(UInt32Chunked::new_from_aligned_vec(
self.name(),
arg_unique_ca!(self),
))
Ok(UInt32Chunked::from_vec(self.name(), arg_unique_ca!(self)))
}

fn is_unique(&self) -> Result<BooleanChunked> {
Expand Down Expand Up @@ -331,7 +325,7 @@ fn dummies_helper(mut groups: Vec<u32>, len: usize, name: &str) -> UInt8Chunked
*elem = 1;
}

ChunkedArray::new_from_aligned_vec(name, av)
ChunkedArray::from_vec(name, av)
}

#[cfg(not(feature = "dtype-u8"))]
Expand All @@ -346,7 +340,7 @@ fn dummies_helper(mut groups: Vec<u32>, len: usize, name: &str) -> Int32Chunked
*elem = 1;
}

ChunkedArray::new_from_aligned_vec(name, av)
ChunkedArray::from_vec(name, av)
}

fn sort_columns(mut columns: Vec<Series>) -> Vec<Series> {
Expand Down Expand Up @@ -422,10 +416,7 @@ impl ChunkUnique<BooleanType> for BooleanChunked {
}

fn arg_unique(&self) -> Result<UInt32Chunked> {
Ok(UInt32Chunked::new_from_aligned_vec(
self.name(),
arg_unique_ca!(self),
))
Ok(UInt32Chunked::from_vec(self.name(), arg_unique_ca!(self)))
}

fn is_unique(&self) -> Result<BooleanChunked> {
Expand Down
8 changes: 4 additions & 4 deletions polars/polars-core/src/chunked_array/ops/unique/rank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub(crate) fn rank(s: &Series, method: RankMethod, reverse: bool) -> Series {
use RankMethod::*;
match method {
Ordinal => {
let inv_ca = UInt32Chunked::new_from_aligned_vec(s.name(), inv);
let inv_ca = UInt32Chunked::from_vec(s.name(), inv);
inv_ca.into_series()
}
#[cfg(feature = "random")]
Expand Down Expand Up @@ -155,11 +155,11 @@ pub(crate) fn rank(s: &Series, method: RankMethod, reverse: bool) -> Series {
});
}

let inv_ca = UInt32Chunked::new_from_aligned_vec(s.name(), inv);
let inv_ca = UInt32Chunked::from_vec(s.name(), inv);
inv_ca.into_series()
}
_ => {
let inv_ca = UInt32Chunked::new_from_aligned_vec(s.name(), inv);
let inv_ca = UInt32Chunked::from_vec(s.name(), inv);
// Safety:
// in bounds
let arr = unsafe { s.take_unchecked(&sort_idx_ca).unwrap() };
Expand Down Expand Up @@ -223,7 +223,7 @@ pub(crate) fn rank(s: &Series, method: RankMethod, reverse: bool) -> Series {
}

count.push((len - null_count) as u32);
let count = UInt32Chunked::new_from_aligned_vec(s.name(), count);
let count = UInt32Chunked::from_vec(s.name(), count);

match method {
Max => {
Expand Down
2 changes: 1 addition & 1 deletion polars/polars-core/src/chunked_array/temporal/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl DateChunked {

pub fn new_from_naive_date(name: &str, v: &[NaiveDate]) -> Self {
let unit = v.iter().map(|v| naive_date_to_date(*v)).collect::<Vec<_>>();
Int32Chunked::new_from_aligned_vec(name, unit).into()
Int32Chunked::from_vec(name, unit).into()
}

pub fn parse_from_str_slice(name: &str, v: &[&str], fmt: &str) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion polars/polars-core/src/chunked_array/temporal/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ impl DatetimeChunked {
TimeUnit::Milliseconds => naive_datetime_to_datetime_ms,
};
let vals = v.iter().map(func).collect_trusted::<Vec<_>>();
Int64Chunked::new_from_aligned_vec(name, vals).into_datetime(tu, None)
Int64Chunked::from_vec(name, vals).into_datetime(tu, None)
}

pub fn parse_from_str_slice(name: &str, v: &[&str], fmt: &str, tu: TimeUnit) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion polars/polars-core/src/chunked_array/temporal/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl DurationChunked {
TimeUnit::Milliseconds => |v: &ChronoDuration| v.num_milliseconds(),
};
let vals = v.iter().map(func).collect_trusted::<Vec<_>>();
Int64Chunked::new_from_aligned_vec(name, vals).into_duration(tu)
Int64Chunked::from_vec(name, vals).into_duration(tu)
}

/// Extract the hours from a `Duration`
Expand Down
2 changes: 1 addition & 1 deletion polars/polars-core/src/chunked_array/upstream_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ where
fn from_iter<I: IntoIterator<Item = T::Native>>(iter: I) -> Self {
// 2021-02-07: aligned vec was ~2x faster than arrow collect.
let av = iter.into_iter().collect::<Vec<T::Native>>();
NoNull::new(ChunkedArray::new_from_aligned_vec("", av))
NoNull::new(ChunkedArray::from_vec("", av))
}
}

Expand Down
2 changes: 1 addition & 1 deletion polars/polars-core/src/frame/explode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl DataFrame {
// expand all the other columns based the exploded first column
if i == 0 {
let row_idx = offsets_to_indexes(&offsets, exploded.len());
let row_idx = UInt32Chunked::new_from_aligned_vec("", row_idx);
let row_idx = UInt32Chunked::from_vec("", row_idx);
// Safety
// We just created indices that are in bounds.
df = unsafe { df.take_unchecked(&row_idx) };
Expand Down
8 changes: 3 additions & 5 deletions polars/polars-core/src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,8 @@ impl DataFrame {
/// ```
pub fn with_row_count(&self, name: &str) -> Result<Self> {
let mut columns = Vec::with_capacity(self.columns.len() + 1);
columns.push(
UInt32Chunked::new_from_aligned_vec(name, (0..self.height() as u32).collect())
.into_series(),
);
columns
.push(UInt32Chunked::from_vec(name, (0..self.height() as u32).collect()).into_series());

self.columns.iter().for_each(|s| columns.push(s.clone()));
DataFrame::new(columns)
Expand Down Expand Up @@ -2716,7 +2714,7 @@ impl DataFrame {

let finish_maintain_order = |mut groups: Vec<u32>| {
groups.sort_unstable();
let ca = UInt32Chunked::new_from_aligned_vec("", groups);
let ca = UInt32Chunked::from_vec("", groups);
unsafe { self.take_unchecked(&ca) }
};

Expand Down
32 changes: 31 additions & 1 deletion polars/polars-core/src/named_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,36 @@ pub trait NamedFrom<T, Phantom: ?Sized> {
fn new(name: &str, _: T) -> Self;
}

pub trait NamedFromOwned<T> {
/// Initialize by name and values.
fn from_vec(name: &str, _: T) -> Self;
}

macro_rules! impl_named_from_owned {
($type:ty, $polars_type:ident) => {
impl NamedFromOwned<$type> for Series {
fn from_vec(name: &str, v: $type) -> Self {
ChunkedArray::<$polars_type>::from_vec(name, v).into_series()
}
}
};
}

#[cfg(feature = "dtype-i8")]
impl_named_from_owned!(Vec<i8>, Int8Type);
#[cfg(feature = "dtype-i16")]
impl_named_from_owned!(Vec<i16>, Int16Type);
impl_named_from_owned!(Vec<i32>, Int32Type);
impl_named_from_owned!(Vec<i64>, Int64Type);
#[cfg(feature = "dtype-u8")]
impl_named_from_owned!(Vec<u8>, UInt8Type);
#[cfg(feature = "dtype-u16")]
impl_named_from_owned!(Vec<u16>, UInt16Type);
impl_named_from_owned!(Vec<u32>, UInt32Type);
impl_named_from_owned!(Vec<u64>, UInt64Type);
impl_named_from_owned!(Vec<f32>, Float32Type);
impl_named_from_owned!(Vec<f64>, Float64Type);

macro_rules! impl_named_from {
($type:ty, $polars_type:ident, $method:ident) => {
impl<T: AsRef<$type>> NamedFrom<T, $type> for Series {
Expand Down Expand Up @@ -182,6 +212,6 @@ impl<T: PolarsNumericType> ChunkedArray<T> {
/// Specialization that prevents an allocation
/// prefer this over ChunkedArray::new when you have a `Vec<T::Native>` and no null values.
pub fn new_vec(name: &str, v: Vec<T::Native>) -> Self {
ChunkedArray::new_from_aligned_vec(name, v)
ChunkedArray::from_vec(name, v)
}
}
4 changes: 2 additions & 2 deletions polars/polars-core/src/series/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl Eq for Wrap<Series> {}
impl Hash for Wrap<Series> {
fn hash<H: Hasher>(&self, state: &mut H) {
let rs = RandomState::with_seeds(0, 0, 0, 0);
let h = UInt64Chunked::new_from_aligned_vec("", self.0.vec_hash(rs)).sum();
let h = UInt64Chunked::from_vec("", self.0.vec_hash(rs)).sum();
h.hash(state)
}
}
Expand Down Expand Up @@ -446,7 +446,7 @@ impl Series {
#[cfg_attr(docsrs, doc(cfg(feature = "row_hash")))]
/// Get a hash of this Series
pub fn hash(&self, build_hasher: ahash::RandomState) -> UInt64Chunked {
UInt64Chunked::new_from_aligned_vec(self.name(), self.0.vec_hash(build_hasher))
UInt64Chunked::from_vec(self.name(), self.0.vec_hash(build_hasher))
}

/// Get the sum of the Series as a new Series of length 1.
Expand Down
2 changes: 1 addition & 1 deletion py-polars/src/dataframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ impl PyDataFrame {

pub fn take(&self, indices: Wrap<Vec<u32>>) -> PyResult<Self> {
let indices = indices.0;
let indices = UInt32Chunked::new_from_aligned_vec("", indices);
let indices = UInt32Chunked::from_vec("", indices);
let df = self.df.take(&indices).map_err(PyPolarsEr::from)?;
Ok(PyDataFrame::new(df))
}
Expand Down
2 changes: 1 addition & 1 deletion py-polars/src/series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ impl PySeries {

pub fn take(&self, indices: Wrap<Vec<u32>>) -> PyResult<Self> {
let indices = indices.0;
let indices = UInt32Chunked::new_from_aligned_vec("", indices);
let indices = UInt32Chunked::from_vec("", indices);

let take = self.series.take(&indices).map_err(PyPolarsEr::from)?;
Ok(PySeries::new(take))
Expand Down

0 comments on commit 3d5a865

Please sign in to comment.