diff --git a/crates/polars-ops/src/chunked_array/mode.rs b/crates/polars-ops/src/chunked_array/mode.rs index bd1269eea719..87b5fc5e5134 100644 --- a/crates/polars-ops/src/chunked_array/mode.rs +++ b/crates/polars-ops/src/chunked_array/mode.rs @@ -61,7 +61,10 @@ pub fn mode(s: &Series) -> PolarsResult { DataType::Boolean => mode_primitive(s_phys.bool().unwrap())?.into_series(), DataType::Float32 => mode_f32(s_phys.f32().unwrap())?.into_series(), DataType::Float64 => mode_64(s_phys.f64().unwrap())?.into_series(), - DataType::String => mode_primitive(&s_phys.str().unwrap().as_binary())?.into_series(), + DataType::String => { + let ca = mode_primitive(&s_phys.str().unwrap().as_binary())?; + unsafe { ca.to_string_unchecked() }.into_series() + }, dt if dt.is_integer() => { with_match_physical_integer_polars_type!(dt, |$T| { let ca: &ChunkedArray<$T> = s_phys.as_ref().as_ref().as_ref(); diff --git a/py-polars/tests/unit/series/test_series.py b/py-polars/tests/unit/series/test_series.py index f545b1290943..40df19738f6e 100644 --- a/py-polars/tests/unit/series/test_series.py +++ b/py-polars/tests/unit/series/test_series.py @@ -994,6 +994,7 @@ def test_mode() -> None: == "bar" ) assert pl.Series([1.0, 2.0, 3.0, 2.0]).mode().item() == 2.0 + assert pl.Series(["a", "b", "c", "b"]).mode().item() == "b" # sorted data assert set(pl.int_range(0, 3, eager=True).mode().to_list()) == {0, 1, 2}