Skip to content

Commit

Permalink
fix: Raise more informative error on invalid reshape input (pola-rs…
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-beedie authored Nov 7, 2023
1 parent 89fadaf commit 80eb5eb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion crates/polars-core/src/series/ops/to_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Series {

pub fn reshape(&self, dims: &[i64]) -> PolarsResult<Series> {
if dims.is_empty() {
panic!("dimensions cannot be empty")
polars_bail!(ComputeError: "reshape `dimensions` cannot be empty")
}
let s = if let DataType::List(_) = self.dtype() {
Cow::Owned(self.explode()?)
Expand Down
12 changes: 8 additions & 4 deletions py-polars/tests/unit/series/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
UInt64,
Unknown,
)
from polars.exceptions import PolarsInefficientMapWarning, ShapeError
from polars.exceptions import ComputeError, PolarsInefficientMapWarning, ShapeError
from polars.testing import assert_frame_equal, assert_series_equal
from polars.utils._construction import iterable_to_pyseries
from polars.utils._wrap import wrap_s
Expand Down Expand Up @@ -585,7 +585,7 @@ def test_cast() -> None:
assert a.cast(pl.Date).dtype == pl.Date

# display failed values, GH#4706
with pytest.raises(pl.ComputeError, match="foobar"):
with pytest.raises(ComputeError, match="foobar"):
pl.Series(["1", "2", "3", "4", "foobar"]).cast(int)


Expand Down Expand Up @@ -1389,9 +1389,9 @@ def test_range() -> None:


def test_strict_cast() -> None:
with pytest.raises(pl.ComputeError):
with pytest.raises(ComputeError):
pl.Series("a", [2**16]).cast(dtype=pl.Int16, strict=True)
with pytest.raises(pl.ComputeError):
with pytest.raises(ComputeError):
pl.DataFrame({"a": [2**16]}).select([pl.col("a").cast(pl.Int16, strict=True)])


Expand Down Expand Up @@ -1939,6 +1939,10 @@ def test_reshape() -> None:
out = pl.select(pl.lit(s).reshape((-1, 1))).to_series()
assert_series_equal(out, expected)

# invalid (empty) dimensions
with pytest.raises(ComputeError, match="reshape `dimensions` cannot be empty"):
s.reshape(())


def test_init_categorical() -> None:
with pl.StringCache():
Expand Down
9 changes: 7 additions & 2 deletions py-polars/tests/unit/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,14 @@ def test_string_numeric_comp_err() -> None:
def test_panic_error() -> None:
with pytest.raises(
pl.PolarsPanicError,
match="dimensions cannot be empty",
match="unit: 'k' not supported",
):
pl.Series("a", [1, 2, 3]).reshape(())
pl.datetime_range(
start=datetime(2021, 12, 16),
end=datetime(2021, 12, 16, 3),
interval="99k",
eager=True,
)


def test_join_lazy_on_df() -> None:
Expand Down

0 comments on commit 80eb5eb

Please sign in to comment.