From b2227966f2db3576369516bd63a742833b1c82dc Mon Sep 17 00:00:00 2001 From: nameexhaustion Date: Sun, 27 Oct 2024 18:40:20 +1100 Subject: [PATCH] fix: Ensure sorted flag is unset after Int->String cast (#19470) --- crates/polars-core/src/chunked_array/cast.rs | 5 ++--- py-polars/tests/unit/operations/test_cast.py | 6 ++++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/crates/polars-core/src/chunked_array/cast.rs b/crates/polars-core/src/chunked_array/cast.rs index 98ae4962b1ae..fc8f993400d1 100644 --- a/crates/polars-core/src/chunked_array/cast.rs +++ b/crates/polars-core/src/chunked_array/cast.rs @@ -206,10 +206,9 @@ where // - remain signed // - unsigned -> signed // this may still fail with overflow? - let dtype = self.dtype(); - let to_signed = dtype.is_signed_integer(); - let unsigned2unsigned = dtype.is_unsigned_integer() && dtype.is_unsigned_integer(); + let unsigned2unsigned = + self.dtype().is_unsigned_integer() && dtype.is_unsigned_integer(); let allowed = to_signed || unsigned2unsigned; if (allowed) diff --git a/py-polars/tests/unit/operations/test_cast.py b/py-polars/tests/unit/operations/test_cast.py index 9b7d3322baca..dca9eeb3e767 100644 --- a/py-polars/tests/unit/operations/test_cast.py +++ b/py-polars/tests/unit/operations/test_cast.py @@ -672,3 +672,9 @@ def test_cast_consistency() -> None: assert pl.DataFrame().with_columns(a=pl.lit(0.0)).with_columns( b=pl.col("a").cast(pl.String), c=pl.lit(0.0).cast(pl.String) ).to_dict(as_series=False) == {"a": [0.0], "b": ["0.0"], "c": ["0.0"]} + + +def test_cast_int_to_string_unsets_sorted_flag_19424() -> None: + s = pl.Series([1, 2]).set_sorted() + assert s.flags["SORTED_ASC"] + assert not s.cast(pl.String).flags["SORTED_ASC"]