Skip to content

Commit

Permalink
fix: Fix mode panicking for String dtype (#20458)
Browse files Browse the repository at this point in the history
Co-authored-by: Ritchie Vink <[email protected]>
  • Loading branch information
lukemanley and ritchie46 authored Dec 26, 2024
1 parent 0d4f8e7 commit 53cda2a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
5 changes: 4 additions & 1 deletion crates/polars-ops/src/chunked_array/mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ pub fn mode(s: &Series) -> PolarsResult<Series> {
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();
Expand Down
1 change: 1 addition & 0 deletions py-polars/tests/unit/series/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit 53cda2a

Please sign in to comment.