Search code examples
pythonpython-polars

How to get unique value count in a Polars Series excluding null values?


I am working with a Polars Series in Python and I need to obtain the number of unique values in the series. However, I want to exclude any null values from the result.

For example, given the following Series:

series = pl.Series([1, 2, None, 4, 2, 4, None])

The result should be 3 unique values (1, 2, and 4).

What is the best way to achieve this in Polars?


Solution

  • Drop the nulls using .drop_nulls() and count the unique values using .n_unique().

    Also, the correct way to represent null values in a call to pl.Series is using Python's None. pl.Null might have worked earlier but it doesn't in the latest polars.

    import polars as pl
    
    series = pl.Series([1, 2, None, 4, 2, 4, None])
    
    print(series.drop_nulls().n_unique())
    

    Output:

    3