Built with Tutify.io

    AIStudyLabs

    Polars Series interpolate() Function with Examples

    Polars Series interpolate() Function with Examples

    In Polars, the interpolate() function is used to fill missing (null) values in a Series by estimating intermediate values based on neighboring non-null entries, typically using linear interpolation. It works by calculating intermediate values between existing data points, effectively smoothing and completing the dataset for better continuity.

    In this article, I will explain the Polars Series interpolate() method, including its syntax, parameters, and functionality. This method returns a new Polars Series where missing values are filled with interpolated estimates.

    Key Points –

    <a id="h-syntax-of-polars-series-interpolate-function"></a>

    Syntax of Polars Series interpolate() Function

    Let’s know the syntax of the series interpolate() function.

    
    # Syntax of Series interpolate()
    Series.interpolate(method: InterpolationMethod = 'linear') → Series
    

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

    Parameters of the Polars series interpolate()

    It allows only one parameter.

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

    Return Value

    This function returns a new Series with missing values filled via the specified interpolation method.

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

    Usage of Polars Series interpolate() Function

    The interpolate() method in Polars fills missing (null) values in a Series by applying linear interpolation. It calculates the missing values by estimating them along a straight line between the surrounding known values.

    Now, let’s create a Polars Series.

    
    import polars as pl
    
    ser = pl.Series("values", [10, 20, None, None, 50])
    print("Original Series:\n", ser)
    

    Yields below output.

    If you want to fill missing values using basic linear interpolation on a Polars Series, here’s a simple example with the Series.interpolate() method.

    
    # Apply linear interpolation
    ser2 = ser.interpolate()
    print("Interpolated Series:\n", ser2)
    

    Here,

    <a id="h-float-series-interpolation"></a>

    Float Series Interpolation

    Interpolation on a Polars Series containing float values with missing data allows you to estimate and fill those gaps by calculating intermediate values between the existing known points.

    
    import polars as pl
    
    # Create a float Series with missing values
    ser = pl.Series("temperatures", [15.5, None, None, 30.0, 35.5])
    
    # Interpolate missing values (default linear method)
    ser2 = ser.interpolate()
    print("Interpolated Series:\n", ser2)
    
    # Output:
    # Interpolated Series:
    # shape: (5,)
    # Series: 'temperatures' [f64]
    # [
    #	15.5
    #	20.333333
    #	25.166667
    #	30.0
    #	35.5
    # ]
    

    Here,

    <a id="h-interpolating-with-nulls-at-the-beginning"></a>

    Interpolating with Nulls at the Beginning

    Interpolation behaves differently when null values appear at the beginning of a Polars Series. The Series.interpolate() method has a specific way of handling these leading nulls. Here’s an example illustrating what happens when you apply interpolate() to a Series with null values at the start.

    
    import polars as pl
    
    ser = pl.Series("values", [None, None, 20, 30, 40])
    
    # Interpolating with nulls at the beginning
    ser2 = ser.interpolate()
    print("\nInterpolated Series:\n", ser2)
    
    # Output:
    # Interpolated Series:
    # shape: (5,)
    # Series: 'values' [f64]
    # [
    #	null
    #	null
    #	20.0
    #	30.0
    #	40.0
    # ]
    

    Here,

    <a id="h-interpolating-with-nulls-at-the-end"></a>

    Interpolating with Nulls at the End

    When null values appear at the end of a Polars Series, interpolation behaves similarly to how it does at the beginning, but from the opposite side. Polars manages these trailing nulls when using the interpolate() method.

    
    import polars as pl
    
    ser = pl.Series("data", [10, 20, 30, None, None])
    
    # Interpolating with nulls at the end
    ser2 = ser.interpolate()
    print("Interpolated Series:\n", ser2)
    
    # Output:
    # Interpolated Series:
    #shape: (5,)
    # Series: 'data' [f64]
    # [
    #	10.0
    #       20.0
    #	30.0
    #	null
    #	null
    # ]
    

    Here,

    <a id="h-single-null-in-the-middle"></a>

    Single Null in the Middle

    Interpolating a single null value in the middle of a Polars Series is simple. The interpolate() method estimates the missing value by drawing a linear path between the known values surrounding the null.

    
    import polars as pl
    
    ser = pl.Series("data", [5, 10, None, 20, 25])
    
    # Single null in the middle
    ser2 = ser.interpolate()
    print("Interpolated Series:\n", ser2)
    
    # Output:
    # Interpolated Series:
    # shape: (5,)
    # Series: 'data' [f64]
    # [
    #	5.0
    #	10.0
    #       15.0
    #	20.0
    #	25.0
    # ]
    

    Here,

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

    Conclusion

    In conclusion, the interpolate() function in Polars Series provides an easy and effective way to fill missing (null) values through linear interpolation. Whether your gaps appear at the start, middle, or end of the Series, this method helps you generate smooth and continuous data without introducing arbitrary values.

    Happy Learning!!

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

    Related Articles

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

    Reference