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

fix: recursively apply cast_unchecked in lists #11884

Merged
merged 2 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
34 changes: 33 additions & 1 deletion crates/polars-core/src/chunked_array/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,11 @@ impl ChunkCast for ListChunked {
}

unsafe fn cast_unchecked(&self, data_type: &DataType) -> PolarsResult<Series> {
self.cast(data_type)
use DataType::*;
match data_type {
List(child_type) => cast_list_unchecked(self, child_type),
_ => self.cast(data_type),
}
}
}

Expand Down Expand Up @@ -386,6 +390,8 @@ impl ChunkCast for ArrayChunked {
// Returns inner data type. This is needed because a cast can instantiate the dtype inner
// values for instance with categoricals
fn cast_list(ca: &ListChunked, child_type: &DataType) -> PolarsResult<(ArrayRef, DataType)> {
// We still rechunk because we must bubble up a single data-type
// TODO!: consider a version that works on chunks and merges the data-types and arrays.
let ca = ca.rechunk();
let arr = ca.downcast_iter().next().unwrap();
// safety: inner dtype is passed correctly
Expand All @@ -409,6 +415,32 @@ fn cast_list(ca: &ListChunked, child_type: &DataType) -> PolarsResult<(ArrayRef,
Ok((Box::new(new_arr), inner_dtype))
}

unsafe fn cast_list_unchecked(ca: &ListChunked, child_type: &DataType) -> PolarsResult<Series> {
// TODO! add chunked, but this must correct for list offsets.
let ca = ca.rechunk();
let arr = ca.downcast_iter().next().unwrap();
// safety: inner dtype is passed correctly
let s = unsafe {
Series::from_chunks_and_dtype_unchecked("", vec![arr.values().clone()], &ca.inner_dtype())
};
let new_inner = s.cast_unchecked(child_type)?;
let new_values = new_inner.array_ref(0).clone();

let data_type = ListArray::<i64>::default_datatype(new_values.data_type().clone());
let new_arr = ListArray::<i64>::new(
data_type,
arr.offsets().clone(),
new_values,
arr.validity().cloned(),
);
Ok(ListChunked::from_chunks_and_dtype_unchecked(
ca.name(),
vec![Box::new(new_arr)],
DataType::List(Box::new(child_type.clone())),
)
.into_series())
}

// Returns inner data type. This is needed because a cast can instantiate the dtype inner
// values for instance with categoricals
#[cfg(feature = "dtype-array")]
Expand Down
3 changes: 2 additions & 1 deletion crates/polars-core/src/series/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,8 @@ impl Series {
/// Packs every element into a list.
pub fn as_list(&self) -> ListChunked {
let s = self.rechunk();
let values = s.to_arrow(0);
// don't use `to_arrow` as we need the physical types
let values = s.chunks()[0].clone();
let offsets = (0i64..(s.len() as i64 + 1)).collect::<Vec<_>>();
let offsets = unsafe { Offsets::new_unchecked(offsets) };

Expand Down
5 changes: 5 additions & 0 deletions py-polars/tests/unit/datatypes/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,8 @@ def test_categorical_collect_11408() -> None:
"groups": ["a", "b", "c"],
"cats": ["a", "b", "c"],
}


def test_categorical_nested_cast_unchecked() -> None:
s = pl.Series("cat", [["cat"]]).cast(pl.List(pl.Categorical))
assert pl.Series([s]).to_list() == [[["cat"]]]