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

feat!: Change casting failures from ComputeError to InvalidOperationError #16895

Merged
merged 2 commits into from
Jun 12, 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
2 changes: 1 addition & 1 deletion crates/polars-core/src/utils/series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn handle_casting_failures(input: &Series, output: &Series) -> PolarsResult<
};

polars_bail!(
ComputeError:
InvalidOperation:
"conversion from `{}` to `{}` failed in column '{}' for {} out of {} values: {}{}",
input.dtype(),
output.dtype(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,13 @@ fn process_struct_numeric_arithmetic(
fn err_date_str_compare() -> PolarsResult<()> {
if cfg!(feature = "python") {
polars_bail!(
ComputeError:
InvalidOperation:
"cannot compare 'date/datetime/time' to a string value \
(create native python {{ 'date', 'datetime', 'time' }} or compare to a temporal column)"
);
} else {
polars_bail!(
ComputeError: "cannot compare 'date/datetime/time' to a string value"
InvalidOperation: "cannot compare 'date/datetime/time' to a string value"
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions py-polars/polars/interchange/from_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import polars._reexport as pl
import polars.functions as F
from polars.datatypes import Boolean, Enum, Int64, String, UInt8, UInt32
from polars.exceptions import ComputeError
from polars.exceptions import InvalidOperationError
from polars.interchange.dataframe import PolarsDataFrame
from polars.interchange.protocol import ColumnNullType, CopyNotAllowedError, DtypeKind
from polars.interchange.utils import (
Expand Down Expand Up @@ -278,7 +278,7 @@ def _construct_validity_buffer(
if column_dtype.is_temporal():
sentinel = sentinel.cast(column_dtype)
return data != sentinel # noqa: TRY300
except ComputeError as e:
except InvalidOperationError as e:
msg = f"invalid sentinel value for column of type {column_dtype}: {null_value!r}"
raise TypeError(msg) from e

Expand Down
2 changes: 1 addition & 1 deletion py-polars/tests/unit/constructors/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def test_df_init_strict() -> None:
def test_df_init_from_series_strict() -> None:
s = pl.Series("a", [-1, 0, 1])
schema = {"a": pl.UInt8}
with pytest.raises(pl.ComputeError):
with pytest.raises(pl.InvalidOperationError):
pl.DataFrame(s, schema=schema, strict=True)

df = pl.DataFrame(s, schema=schema, strict=False)
Expand Down
15 changes: 8 additions & 7 deletions py-polars/tests/unit/datatypes/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_enum_init_empty(categories: pl.Series | list[str] | None) -> None:

def test_enum_non_existent() -> None:
with pytest.raises(
pl.ComputeError,
pl.InvalidOperationError,
match=re.escape(
"conversion from `str` to `enum` failed in column '' for 1 out of 4 values: [\"c\"]"
),
Expand Down Expand Up @@ -165,7 +165,7 @@ def test_casting_to_an_enum_oob_from_integer() -> None:

def test_casting_to_an_enum_from_categorical_nonexistent() -> None:
with pytest.raises(
pl.ComputeError,
pl.InvalidOperationError,
match=(
r"conversion from `cat` to `enum` failed in column '' for 1 out of 4 values: \[\"c\"\]"
),
Expand All @@ -187,7 +187,7 @@ def test_casting_to_an_enum_from_global_categorical() -> None:
@StringCache()
def test_casting_to_an_enum_from_global_categorical_nonexistent() -> None:
with pytest.raises(
pl.ComputeError,
pl.InvalidOperationError,
match=(
r"conversion from `cat` to `enum` failed in column '' for 1 out of 4 values: \[\"c\"\]"
),
Expand Down Expand Up @@ -347,7 +347,7 @@ def test_compare_enum_str_single_raise(
s2 = "NOTEXIST"

with pytest.raises(
pl.ComputeError,
pl.InvalidOperationError,
match=re.escape(
"conversion from `str` to `enum` failed in column '' for 1 out of 1 values: [\"NOTEXIST\"]"
),
Expand All @@ -363,7 +363,7 @@ def test_compare_enum_str_raise() -> None:
for s_compare in [s2, s_broadcast]:
for op in [operator.le, operator.gt, operator.ge, operator.lt]:
with pytest.raises(
pl.ComputeError,
pl.InvalidOperationError,
match="conversion from `str` to `enum` failed in column",
):
op(s, s_compare)
Expand Down Expand Up @@ -439,13 +439,14 @@ def test_enum_cast_from_other_integer_dtype_oob() -> None:
enum_dtype = pl.Enum(["a", "b", "c", "d"])
series = pl.Series([-1, 2, 3, 3, 2, 1], dtype=pl.Int8)
with pytest.raises(
pl.ComputeError, match="conversion from `i8` to `u32` failed in column"
pl.InvalidOperationError, match="conversion from `i8` to `u32` failed in column"
):
series.cast(enum_dtype)

series = pl.Series([2**34, 2, 3, 3, 2, 1], dtype=pl.UInt64)
with pytest.raises(
pl.ComputeError, match="conversion from `u64` to `u32` failed in column"
pl.InvalidOperationError,
match="conversion from `u64` to `u32` failed in column",
):
series.cast(enum_dtype)

Expand Down
4 changes: 3 additions & 1 deletion py-polars/tests/unit/functions/range/test_int_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,9 @@ def test_int_range_null_input() -> None:


def test_int_range_invalid_conversion() -> None:
with pytest.raises(pl.ComputeError, match="conversion from `i32` to `u32` failed"):
with pytest.raises(
pl.InvalidOperationError, match="conversion from `i32` to `u32` failed"
):
pl.select(pl.int_range(3, -1, -1, dtype=pl.UInt32))


Expand Down
2 changes: 1 addition & 1 deletion py-polars/tests/unit/functions/test_when_then.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def test_comp_incompatible_enum_dtype() -> None:
df = pl.DataFrame({"a": pl.Series(["a", "b"], dtype=pl.Enum(["a", "b"]))})

with pytest.raises(
pl.ComputeError,
pl.InvalidOperationError,
match="conversion from `str` to `enum` failed in column 'literal'",
):
df.with_columns(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_str_slice_expr() -> None:
assert_frame_equal(out, expected)

# negative length is not allowed
with pytest.raises(pl.ComputeError):
with pytest.raises(pl.InvalidOperationError):
df.select(pl.col("a").str.slice(0, -1))


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import polars as pl
from polars.dependencies import _ZONEINFO_AVAILABLE
from polars.exceptions import ComputeError
from polars.testing import assert_series_equal

if sys.version_info >= (3, 9):
Expand Down Expand Up @@ -115,26 +114,23 @@ def test_to_datetime(datetimes: datetime, fmt: str) -> None:
expected = datetime.strptime(input, fmt)
try:
result = pl.Series([input]).str.to_datetime(format=fmt).item()
except ComputeError as exc:
# If there's an exception, check that it's either:
# - something which polars can't parse at all: missing day or month
# - something on which polars intentionally raises
assert ( # noqa: PT017
(
(("%H" in fmt) ^ ("%M" in fmt))
or (("%I" in fmt) ^ ("%M" in fmt))
or ("%S" in fmt and "%H" not in fmt)
or ("%S" in fmt and "%I" not in fmt)
or (("%I" in fmt) ^ ("%p" in fmt))
or (("%H" in fmt) ^ ("%p" in fmt))
)
and "Invalid format string" in str(exc)
) or (
(
not any(day in fmt for day in ("%d", "%j"))
or not any(month in fmt for month in ("%b", "%B", "%m"))
)
and "failed in column" in str(exc)
# If there's an exception, check that it's either:
# - something which polars can't parse at all: missing day or month
# - something on which polars intentionally raises
except pl.InvalidOperationError as exc:
assert "failed in column" in str(exc) # noqa: PT017
assert not any(day in fmt for day in ("%d", "%j")) or not any(
month in fmt for month in ("%b", "%B", "%m")
)
except pl.ComputeError as exc:
assert "Invalid format string" in str(exc) # noqa: PT017
assert (
(("%H" in fmt) ^ ("%M" in fmt))
or (("%I" in fmt) ^ ("%M" in fmt))
or ("%S" in fmt and "%H" not in fmt)
or ("%S" in fmt and "%I" not in fmt)
or (("%I" in fmt) ^ ("%p" in fmt))
or (("%H" in fmt) ^ ("%p" in fmt))
)
else:
assert result == expected
Expand Down
8 changes: 4 additions & 4 deletions py-polars/tests/unit/operations/namespaces/test_strptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def test_to_date_non_exact_strptime() -> None:
)
assert_series_equal(result, expected)

with pytest.raises(pl.ComputeError):
with pytest.raises(pl.InvalidOperationError):
s.str.to_date(format, strict=True, exact=True)


Expand Down Expand Up @@ -161,7 +161,7 @@ def test_to_date_all_inferred_date_patterns(time_string: str, expected: date) ->
],
)
def test_non_exact_short_elements_10223(value: str, attr: str) -> None:
with pytest.raises(pl.ComputeError, match="conversion .* failed"):
with pytest.raises(pl.InvalidOperationError, match="conversion .* failed"):
getattr(pl.Series(["2019-01-01", value]).str, attr)(exact=False)


Expand Down Expand Up @@ -211,7 +211,7 @@ def test_to_datetime_non_exact_strptime(
assert_series_equal(result, expected)
assert result.dtype == pl.Datetime("us", time_zone)

with pytest.raises(pl.ComputeError):
with pytest.raises(pl.InvalidOperationError):
s.str.to_datetime(format, strict=True, exact=True)


Expand Down Expand Up @@ -720,7 +720,7 @@ def test_strptime_ambiguous_earliest(exact: bool) -> None:
@pytest.mark.parametrize("time_unit", ["ms", "us", "ns"])
def test_to_datetime_out_of_range_13401(time_unit: TimeUnit) -> None:
s = pl.Series(["2020-January-01 12:34:66"])
with pytest.raises(pl.ComputeError, match="conversion .* failed"):
with pytest.raises(pl.InvalidOperationError, match="conversion .* failed"):
s.str.to_datetime("%Y-%B-%d %H:%M:%S", time_unit=time_unit)
assert (
s.str.to_datetime("%Y-%B-%d %H:%M:%S", strict=False, time_unit=time_unit).item()
Expand Down
22 changes: 11 additions & 11 deletions py-polars/tests/unit/operations/test_cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_string_date() -> None:
def test_invalid_string_date() -> None:
df = pl.DataFrame({"x1": ["2021-01-aa"]})

with pytest.raises(pl.ComputeError):
with pytest.raises(pl.InvalidOperationError):
df.with_columns(**{"x1-date": pl.col("x1").cast(pl.Date)})


Expand Down Expand Up @@ -63,7 +63,7 @@ def test_string_datetime() -> None:

def test_invalid_string_datetime() -> None:
df = pl.DataFrame({"x1": ["2021-12-19 00:39:57", "2022-12-19 16:39:57"]})
with pytest.raises(pl.ComputeError):
with pytest.raises(pl.InvalidOperationError):
df.with_columns(
**{"x1-datetime-ns": pl.col("x1").cast(pl.Datetime(time_unit="ns"))}
)
Expand Down Expand Up @@ -232,11 +232,11 @@ def test_strict_cast_int(
assert _cast_expr(*args) == expected_value # type: ignore[arg-type]
assert _cast_lit(*args) == expected_value # type: ignore[arg-type]
else:
with pytest.raises(pl.ComputeError):
with pytest.raises(pl.InvalidOperationError):
_cast_series(*args) # type: ignore[arg-type]
with pytest.raises(pl.ComputeError):
with pytest.raises(pl.InvalidOperationError):
_cast_expr(*args) # type: ignore[arg-type]
with pytest.raises(pl.ComputeError):
with pytest.raises(pl.InvalidOperationError):
_cast_lit(*args) # type: ignore[arg-type]


Expand Down Expand Up @@ -371,11 +371,11 @@ def test_strict_cast_temporal(
assert out.item() == expected_value
assert out.dtype == to_dtype
else:
with pytest.raises(pl.ComputeError):
with pytest.raises(pl.InvalidOperationError):
_cast_series_t(*args) # type: ignore[arg-type]
with pytest.raises(pl.ComputeError):
with pytest.raises(pl.InvalidOperationError):
_cast_expr_t(*args) # type: ignore[arg-type]
with pytest.raises(pl.ComputeError):
with pytest.raises(pl.InvalidOperationError):
_cast_lit_t(*args) # type: ignore[arg-type]


Expand Down Expand Up @@ -567,11 +567,11 @@ def test_strict_cast_string_and_binary(
assert out.item() == expected_value
assert out.dtype == to_dtype
else:
with pytest.raises(pl.ComputeError):
with pytest.raises(pl.InvalidOperationError):
_cast_series_t(*args) # type: ignore[arg-type]
with pytest.raises(pl.ComputeError):
with pytest.raises(pl.InvalidOperationError):
_cast_expr_t(*args) # type: ignore[arg-type]
with pytest.raises(pl.ComputeError):
with pytest.raises(pl.InvalidOperationError):
_cast_lit_t(*args) # type: ignore[arg-type]


Expand Down
4 changes: 3 additions & 1 deletion py-polars/tests/unit/operations/test_clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,7 @@ def test_clip_string_input() -> None:

def test_clip_bound_invalid_for_original_dtype() -> None:
s = pl.Series([1, 2, 3, 4], dtype=pl.UInt32)
with pytest.raises(pl.ComputeError, match="conversion from `i32` to `u32` failed"):
with pytest.raises(
pl.InvalidOperationError, match="conversion from `i32` to `u32` failed"
):
s.clip(-1, 5)
4 changes: 3 additions & 1 deletion py-polars/tests/unit/operations/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ def test_replace_cat_to_cat(str_mapping: dict[str | None, str]) -> None:
def test_replace_invalid_old_dtype() -> None:
lf = pl.LazyFrame({"a": [1, 2, 3]})
mapping = {"a": 10, "b": 20}
with pytest.raises(pl.ComputeError, match="conversion from `str` to `i64` failed"):
with pytest.raises(
pl.InvalidOperationError, match="conversion from `str` to `i64` failed"
):
lf.select(pl.col("a").replace(mapping)).collect()


Expand Down
16 changes: 8 additions & 8 deletions py-polars/tests/unit/series/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
UInt64,
Unknown,
)
from polars.exceptions import ComputeError, PolarsInefficientMapWarning, ShapeError
from polars.exceptions import PolarsInefficientMapWarning, ShapeError
from polars.testing import assert_frame_equal, assert_series_equal

if TYPE_CHECKING:
Expand Down Expand Up @@ -498,7 +498,7 @@ def test_cast() -> None:
assert a.cast(pl.Date).dtype == pl.Date

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


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


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


Expand Down Expand Up @@ -2133,13 +2133,13 @@ def test_series_from_pandas_with_dtype() -> None:
s = pl.Series("foo", pd.Series([1, 2, 3], dtype="Int16"), pl.Int8)
assert_series_equal(s, expected)

with pytest.raises(pl.ComputeError, match="conversion from"):
with pytest.raises(pl.InvalidOperationError, match="conversion from"):
pl.Series("foo", pd.Series([-1, 2, 3]), pl.UInt8)
s = pl.Series("foo", pd.Series([-1, 2, 3]), pl.UInt8, strict=False)
assert s.to_list() == [None, 2, 3]
assert s.dtype == pl.UInt8

with pytest.raises(pl.ComputeError, match="conversion from"):
with pytest.raises(pl.InvalidOperationError, match="conversion from"):
pl.Series("foo", pd.Series([-1, 2, 3], dtype="Int8"), pl.UInt8)
s = pl.Series("foo", pd.Series([-1, 2, 3], dtype="Int8"), pl.UInt8, strict=False)
assert s.to_list() == [None, 2, 3]
Expand All @@ -2150,7 +2150,7 @@ def test_series_from_pyarrow_with_dtype() -> None:
s = pl.Series("foo", pa.array([-1, 2, 3]), pl.Int8)
assert_series_equal(s, pl.Series("foo", [-1, 2, 3], dtype=pl.Int8))

with pytest.raises(pl.ComputeError, match="conversion from"):
with pytest.raises(pl.InvalidOperationError, match="conversion from"):
pl.Series("foo", pa.array([-1, 2, 3]), pl.UInt8)

s = pl.Series("foo", pa.array([-1, 2, 3]), dtype=pl.UInt8, strict=False)
Expand All @@ -2162,7 +2162,7 @@ def test_series_from_numpy_with_dtye() -> None:
s = pl.Series("foo", np.array([-1, 2, 3]), pl.Int8)
assert_series_equal(s, pl.Series("foo", [-1, 2, 3], dtype=pl.Int8))

with pytest.raises(pl.ComputeError, match="conversion from"):
with pytest.raises(pl.InvalidOperationError, match="conversion from"):
pl.Series("foo", np.array([-1, 2, 3]), pl.UInt8)

s = pl.Series("foo", np.array([-1, 2, 3]), dtype=pl.UInt8, strict=False)
Expand Down
4 changes: 2 additions & 2 deletions py-polars/tests/unit/sql/test_cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import polars as pl
import polars.selectors as cs
from polars.exceptions import ComputeError, SQLInterfaceError
from polars.exceptions import SQLInterfaceError
from polars.testing import assert_frame_equal


Expand Down Expand Up @@ -165,7 +165,7 @@ def test_cast_errors(values: Any, cast_op: str, error: str) -> None:
df = pl.DataFrame({"values": values})

# invalid CAST should raise an error...
with pytest.raises(ComputeError, match=error):
with pytest.raises(pl.InvalidOperationError, match=error):
df.sql(f"SELECT {cast_op} FROM self")

# ... or return `null` values if using TRY_CAST
Expand Down
Loading