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

unique() panics on empty global categorical series #20528

Closed
mcrumiller opened this issue Jan 2, 2025 · 1 comment · Fixed by #20536
Closed

unique() panics on empty global categorical series #20528

mcrumiller opened this issue Jan 2, 2025 · 1 comment · Fixed by #20536
Labels
bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars

Comments

@mcrumiller
Copy link
Contributor

mcrumiller commented Jan 2, 2025

Issue Description

Empty categorical array in a global context panics when unique is called.

import polars as pl

with pl.StringCache():
    pl.Series([], dtype=pl.Categorical).unique()
# pyo3_runtime.PanicException: attempt to subtract with overflow

Installed versions

main

@mcrumiller mcrumiller added bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars labels Jan 2, 2025
@mcrumiller
Copy link
Contributor Author

@ritchie46 this one panics because of this section: https://github.com/pola-rs/polars/blob/main/crates/polars-core/src/chunked_array/logical/categorical/ops/unique.rs#L40-L46

let mut min = u32::MAX;
let mut max = 0u32;

for &v in map.keys() {
    min = min.min(v);
    max = max.max(v);
}

PrimitiveRangedUniqueState::new(min, max + has_nulls)

If the series is empty then min remains as u32::MAX, I think we need a fast path for empty arrays.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant