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: Raise error when casting Array to different width #14995

Merged
merged 3 commits into from
Mar 15, 2024
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
21 changes: 17 additions & 4 deletions crates/polars-core/src/chunked_array/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,12 @@ impl ChunkCast for ListChunked {
match data_type {
List(child_type) => {
match (self.inner_dtype(), &**child_type) {
(old, new) if old == *new => Ok(self.clone().into_series()),
#[cfg(feature = "dtype-categorical")]
(dt, Categorical(None, _) | Enum(_, _))
if !matches!(dt, Categorical(_, _) | Enum(_, _) | String | Null) =>
{
polars_bail!(ComputeError: "cannot cast List inner type: '{:?}' to Categorical", dt)
polars_bail!(InvalidOperation: "cannot cast List inner type: '{:?}' to Categorical", dt)
},
_ => {
// ensure the inner logical type bubbles up
Expand Down Expand Up @@ -417,7 +418,7 @@ impl ChunkCast for ListChunked {
},
_ => {
polars_bail!(
ComputeError: "cannot cast List type (inner: '{:?}', to: '{:?}')",
InvalidOperation: "cannot cast List type (inner: '{:?}', to: '{:?}')",
self.inner_dtype(),
data_type,
)
Expand All @@ -442,10 +443,16 @@ impl ChunkCast for ArrayChunked {
use DataType::*;
match data_type {
Array(child_type, width) => {
polars_ensure!(
*width == self.width(),
InvalidOperation: "cannot cast Array to a different width"
);

match (self.inner_dtype(), &**child_type) {
(old, new) if old == *new => Ok(self.clone().into_series()),
#[cfg(feature = "dtype-categorical")]
(dt, Categorical(None, _) | Enum(_, _)) if !matches!(dt, String) => {
polars_bail!(ComputeError: "cannot cast fixed-size-list inner type: '{:?}' to dtype: {:?}", dt, child_type)
polars_bail!(InvalidOperation: "cannot cast Array inner type: '{:?}' to dtype: {:?}", dt, child_type)
},
_ => {
// ensure the inner logical type bubbles up
Expand Down Expand Up @@ -476,7 +483,13 @@ impl ChunkCast for ArrayChunked {
))
}
},
_ => polars_bail!(ComputeError: "cannot cast list type"),
_ => {
polars_bail!(
InvalidOperation: "cannot cast Array type (inner: '{:?}', to: '{:?}')",
self.inner_dtype(),
data_type,
)
},
}
}

Expand Down
8 changes: 8 additions & 0 deletions py-polars/tests/unit/operations/test_cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,3 +632,11 @@ def test_cast_decimal() -> None:
df.select(pl.col("s").cast(pl.Boolean)),
pl.DataFrame({"s": [False, True, True]}),
)


def test_cast_array_to_different_width() -> None:
s = pl.Series([[1, 2], [3, 4]], dtype=pl.Array(pl.Int8, 2))
with pytest.raises(
pl.InvalidOperationError, match="cannot cast Array to a different width"
):
s.cast(pl.Array(pl.Int16, 3))
stinodego marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 2 additions & 1 deletion py-polars/tests/unit/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,8 @@ def test_err_on_invalid_time_zone_cast() -> None:
def test_invalid_inner_type_cast_list() -> None:
s = pl.Series([[-1, 1]])
with pytest.raises(
pl.ComputeError, match=r"cannot cast List inner type: 'Int64' to Categorical"
pl.InvalidOperationError,
match=r"cannot cast List inner type: 'Int64' to Categorical",
):
s.cast(pl.List(pl.Categorical))

Expand Down
Loading