Skip to content

Commit

Permalink
implement expand at idx for List and Object type
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Nov 25, 2021
1 parent 63fde06 commit 35dd556
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
12 changes: 12 additions & 0 deletions polars/polars-core/src/chunked_array/ops/full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ impl ListChunked {
}
}

#[cfg(feature = "object")]
impl<T: PolarsObject> ChunkFull<T> for ObjectChunked<T> {
fn full(name: &str, value: T, length: usize) -> Self
where
Self: Sized,
{
let mut ca: Self = (0..length).map(|_| Some(value.clone())).collect();
ca.rename(name);
ca
}
}

#[cfg(feature = "object")]
impl<T: PolarsObject> ChunkFullNull for ObjectChunked<T> {
fn full_null(name: &str, length: usize) -> ObjectChunked<T> {
Expand Down
18 changes: 13 additions & 5 deletions polars/polars-core/src/chunked_array/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,15 +652,23 @@ impl ChunkExpandAtIndex<CategoricalType> for CategoricalChunked {
}

impl ChunkExpandAtIndex<ListType> for ListChunked {
fn expand_at_index(&self, _index: usize, _length: usize) -> ListChunked {
unimplemented!()
fn expand_at_index(&self, index: usize, length: usize) -> ListChunked {
let opt_val = self.get(index);
match opt_val {
Some(val) => ListChunked::full(self.name(), &val, length),
None => ListChunked::full_null_with_dtype(self.name(), length, &self.inner_dtype()),
}
}
}

#[cfg(feature = "object")]
impl<T> ChunkExpandAtIndex<ObjectType<T>> for ObjectChunked<T> {
fn expand_at_index(&self, _index: usize, _length: usize) -> ObjectChunked<T> {
todo!()
impl<T: PolarsObject> ChunkExpandAtIndex<ObjectType<T>> for ObjectChunked<T> {
fn expand_at_index(&self, index: usize, length: usize) -> ObjectChunked<T> {
let opt_val = self.get(index);
match opt_val {
Some(val) => ObjectChunked::<T>::full(self.name(), val.clone(), length),
None => ObjectChunked::<T>::full_null(self.name(), length),
}
}
}

Expand Down

0 comments on commit 35dd556

Please sign in to comment.