Built with Tutify.io

    AIStudyLabs

    Polars DataFrame interpolate() – Usage & Examples

    Polars DataFrame interpolate() – Usage & Examples

    In Polars, the interpolate() method is used to fill missing values (nulls) in numeric columns by estimating intermediate points through linear interpolation. This approach assumes a straight-line relationship between adjacent known values to calculate the missing data. Similar to Pandas, Polars provides an interpolate() method for both Series and DataFrames. Specifically, the DataFrame version fills gaps in numeric columns by linearly estimating values based on surrounding existing data.

    In this article, I will explain the Polars DataFrame interpolate() method, covering its syntax, parameters, and usage. This method produces a new DataFrame where missing values (nulls) in each numeric column are filled independently through interpolation.

    Key Points –

    <a id="h-polars-dataframe-interpolate-introduction"></a>

    Polars DataFrame interpolate() Introduction

    Let’s know the syntax of the DataFrame interpolate() function

    
    # Syntax of DataFrame interpolate()
    DataFrame.interpolate(method: str = "linear") -> DataFrame
    

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

    Parameters of the DataFrame interpolate()

    Following are the parameters of the DataFrame interpolate() function

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

    Return Value

    This function returns a new DataFrame with nulls interpolated in all columns where applicable.

    <a id="h-usage-of-polars-dataframe-interpolate-method"></a>

    Usage of Polars DataFrame interpolate() Method

    The interpolate() method in Polars replaces missing values (nulls) in numeric columns by calculating estimates that lie between existing data points using linear interpolation. It assumes a steady, constant rate of change between known values and fills the gaps by connecting them with straight lines.

    First, let’s create a Polars DataFrame.

    
    import polars as pl
    
    # Sample DataFrame with missing values
    df = pl.DataFrame({
        "A": [1.0, None, None, 4.0, 5.0]
    })
    print("Original DataFrame:\n", df)
    

    Yields below output.

    Linear interpolation in a Polars DataFrame fills missing values (nulls) by calculating estimated points that lie evenly between two known data points. It works under the assumption that the values change steadily and at a constant rate between those points, effectively drawing a straight line to fill the gaps.

    
    # Interpolate missing values linearly
    df2 = df.interpolate()
    print("After Linear Interpolation:\n", df2)
    

    Here,

    <a id="h-interpolation-across-multiple-numeric-columns"></a>

    Interpolation Across Multiple Numeric Columns

    When applied to a Polars DataFrame, the interpolate() method operates independently on each numeric column. It performs linear interpolation within each column, filling in null values based solely on the non-null values present in that specific column, without referencing or being influenced by the values in other columns.

    
    import polars as pl
    
    # DataFrame with missing values in multiple columns
    df = pl.DataFrame({
        "temperature": [20, None, 24, None, 28],
        "humidity": [None, 60, None, 80, 90]
    })
    
    # Apply linear interpolation to all numeric columns
    df2 = df.interpolate()
    print("After Linear Interpolation:\n", df2)
    
    # Output:
    # After Linear Interpolation:
    # shape: (5, 2)
    ┌─────────────┬──────────┐
    │ temperature ┆ humidity │
    │ ---         ┆ ---      │
    │ f64         ┆ f64      │
    ╞═════════════╪══════════╡
    │ 20.0        ┆ null     │
    │ 22.0        ┆ 60.0     │
    │ 24.0        ┆ 70.0     │
    │ 26.0        ┆ 80.0     │
    │ 28.0        ┆ 90.0     │
    └─────────────┴──────────┘
    

    Here,

    <a id="h-interpolate-only-one-column-using-with-columns"></a>

    Interpolate Only one Column Using with_columns()

    To interpolate a single column in a Polars DataFrame without modifying the others, you can use the with_columns() method. This allows you to apply interpolation selectively to just one column while keeping the rest of the DataFrame unchanged.

    
    # Interpolate only the 'temperature' column
    df2 = df.with_columns([
        df["temperature"].interpolate().alias("temperature")
    ])
    print("After Interpolating Only Column 'temperature':\n", df2)
    
    # Output:
    # After Interpolating Only Column 'temperature':
    # shape: (5, 2)
    ┌─────────────┬──────────┐
    │ temperature ┆ humidity │
    │ ---         ┆ ---      │
    │ f64         ┆ i64      │
    ╞═════════════╪══════════╡
    │ 20.0        ┆ null     │
    │ 22.0        ┆ 60       │
    │ 24.0        ┆ null     │
    │ 26.0        ┆ 80       │
    │ 28.0        ┆ 90       │
    └─────────────┴──────────┘
    

    Here,

    <a id="h-interpolation-with-a-sorted-index"></a>

    Interpolation with a Sorted Index

    Sorting the data before interpolation ensures accurate linear estimation, especially in cases where the value progression depends on order, such as in time series data.

    
    import polars as pl
    
    # Unsorted DataFrame
    df = pl.DataFrame({
        "Day": [3, 1, 2, 5, 4],
        "Value": [30.0, 10.0, None, 50.0, None]
    })
    
    # Interpolate after sorting by "Day"
    df2 = (
        df.sort("Day")
          .with_columns(
              pl.col("Value").interpolate().alias("Value")
          )
    )
    print("Sorted and Interpolated:\n", df2)
    
    # Output:
    # Sorted and Interpolated:
    # shape: (5, 2)
    ┌─────┬───────┐
    │ Day ┆ Value │
    │ --- ┆ ---   │
    │ i64 ┆ f64   │
    ╞═════╪═══════╡
    │ 1   ┆ 10.0  │
    │ 2   ┆ 20.0  │
    │ 3   ┆ 30.0  │
    │ 4   ┆ 40.0  │
    │ 5   ┆ 50.0  │
    └─────┴───────┘
    

    Here,

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

    Conclusion

    In conclusion, Polar’s interpolate() method is a powerful and flexible tool for filling missing values through linear estimation. By applying interpolation column-wise, optionally within groups, or on sorted data, you can effectively handle gaps in your datasets, especially in time series or numerical analyses, while maintaining control over which columns get interpolated.

    Happy Learning!!

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

    Related Articles

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

    Reference