Built with Tutify.io

    AIStudyLabs

    Polars Series fill_null() Function with Examples

    Polars Series fill_null() Function with Examples

    In Polars, the fill_null() function is used to handle missing data by replacing null values in a Series or DataFrame column. You can fill these nulls with a fixed value (such as a number or string), or apply a strategy like forward fill or backward fill. The function returns a new Series with the nulls replaced, helping you ensure that your data is complete and ready for analysis.

    In this article, I will explain the fill_null() method of the Polars Series, including its syntax, parameters, and functionality. This method returns a new Series with null values replaced based on the provided value or chosen strategy.

    Key Points –

    <a id="h-polars-series-fill-null-introduction"></a>

    Polars Series fill_null() Introduction

    Let’s know the syntax of the series fill_null() method.

    
    # Syntax of Series fill_null()
    Series.fill_null(
    value: Any | Expr | None = None,
    strategy: FillNullStrategy | None = None,
    limit: int | None = None,
    ) → Series
    

    <a id="h-parameters-of-the-polars-series-fill-null"></a>

    Parameters of the Polars series fill_null()

    Following are the parameters of the fill_null() function.

    <a id="h-return-value"></a>

    Return Value

    This function returns a new Polars Series with null values filled.

    <a id="h-usage-of-polars-series-fill-null-function"></a>

    Usage of Polars Series fill_null() Function

    The fill_null() function in Polars is used to replace null (missing) values in a Series, either with a specified value or by applying a strategy (such as forward fill, mean, etc.).

    First, let’s create a Polars Series.

    
    import polars as pl
    
    # Sample Series with nulls
    ser = pl.Series("values", [10, None, 30, None, 50])
    print("Original Series:\n", ser)
    

    Yields below output.

    To fill nulls with a fixed value (e.g., 0) in a Polars Series, you can use the fill_null() function.

    
    # Fill nulls with a fixed value (0)
    ser2 = ser.fill_null(0)
    print("Series after fill_null(0):\n", ser2)
    

    Here,

    <a id="h-fill-nulls-with-the-mean-of-the-series"></a>

    Fill Nulls with the Mean of the Series

    You can also fill nulls in a Polars Series with the mean of the Series, a common technique in data cleaning. Since Series.fill_null() accepts a fixed value, you first compute the mean and then pass it to fill_null().

    
    # Compute mean of the Series (ignores nulls)
    mean_value = ser.mean()
    print(f"\nMean of the Series (ignoring nulls): {mean_value}")
    
    # Fill nulls with the mean value
    ser2 = ser.fill_null(mean_value)
    print("\nSeries after fill_null(mean):\n", ser2)
    
    # Output:
    # Series after fill_null(mean):
    # shape: (5,)
    # Series: 'values' [f64]
    # [
    #	10.0
    #	30.0
    #	30.0
    #	30.0
    #	50.0
    # ]
    

    Here,

    <a id="h-forward-fill-use-last-non-null-value"></a>

    Forward Fill (Use Last Non-Null Value)

    You can use Polars Series.fill_null() to perform a forward fill. This technique replaces each null value with the most recent non-null value that appears earlier in the Series.

    
    # Forward fill nulls
    ser2 = ser.fill_null(strategy="forward")
    print("\nSeries after forward fill:\n", ser2)
    
    # Output:
    # Series after forward fill:
    # shape: (5,)
    # Series: 'values' [i64]
    # [
    #	10
    #	10
    #	30
    #	30
    #	50
    # ]
    

    Here,

    <a id="h-backward-fill-use-next-non-null-value"></a>

    Backward Fill (Use Next Non-Null Value)

    In backward fill, each null is replaced with the next non-null value that appears after it in the Series. This technique fills nulls with the nearest non-null value below them.

    
    # Backward fill nulls
    ser2 = ser.fill_null(strategy="backward")
    print("Series after backward fill:\n", ser2)
    
    # Output:
    # Series after backward fill:
    # shape: (5,)
    # Series: 'values' [i64]
    # [
    #	10
    #	30
    #	30
    #	50
    #	50
    # ]
    

    Here.

    <a id="h-fill-nulls-with-a-fixed-string-for-string-series"></a>

    Fill Nulls with a Fixed String (For String Series)

    You can also use fill_null() to replace nulls in a string Series with a fixed string. The function works the same way for string data, you can fill null values with strings like "unknown" or "missing".

    
    import polars as pl
    
    # Sample string Series with nulls
    ser = pl.Series("Courses", ["Spark", None, "Python", None, "Hyperion"])
    
    # Fill nulls with a fixed string
    ser2 = ser.fill_null("Unknown")
    print("Series after fill_null('Unknown'):\n", ser2)
    
    # Output:
    # Series after fill_null('Unknown'):
    # shape: (5,)
    # Series: 'Courses' [str]
    # [
    #	"Spark"
    #	"Unknown"
    #	"Python"
    #	"Unknown"
    #	"Hyperion"
    # ]
    

    Here,

    <a id="h-conclusion"></a>

    Conclusion

    In summary, fill_null() is a versatile function in the Polars Series that allows you to handle missing data effectively. Whether you want to replace nulls with a fixed value, use forward or backward fill, or even fill string data with placeholders, fill_null() provides a simple and consistent way to clean your Series for further analysis.

    Happy Learning!!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References