Built with Tutify.io

    AIStudyLabs

    Polars Series describe() – Usage & Examples

    Polars Series describe() – Usage & Examples

    In Polars, the Series.describe() method generates a quick statistical summary of a Series. It’s a convenient way to understand the distribution, central tendency, and variability of numeric data, similar to Pandas’ describe() but tailored for Polars' fast and efficient data processing.

    In this article, I will explain the Polars Series describe() function, covering its syntax, parameters, and usage, and explain how it returns a Polars DataFrame containing summary statistics like count, mean, std, min, max, and the specified percentiles.

    Key Points –

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

    Polars Series describe() Introduction

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

    
    # Syntax of describe()
    Series.describe(
    percentiles: Sequence[float] | float | None = (0.25, 0.5, 0.75),
    interpolation: RollingInterpolationMethod = 'nearest',
    ) → DataFrame
    

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

    Parameters of the Series describe()

    Following are the parameters of the series describe() method.

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

    Return Value

    This function returns a polars DataFrame containing summary statistics of the Series.

    <a id="h-usage-of-polars-series-describe-method"></a>

    Usage of Polars Series.describe() Method

    The Series.describe() method computes and returns a summary of key statistics for the polars Series. It provides a quick overview of the data, including metrics like count, number of null values, mean, standard deviation, minimum and maximum values, and important percentiles (25%, 50%, and 75%). This helps you quickly understand the distribution, central tendency, and spread of the numeric data within the Series.

    Now, let’s create a Polars Series.

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

    Yields below output.

    You can use the describe() function on a Series or DataFrame to get basic descriptive statistics like count, mean, std, min, max, and percentiles by default.

    
    # Using describe() to get basic statistics
    ser2 = ser.describe()
    print("Summary Statistics:\n", ser2)
    

    Here,

    <a id="h-custom-percentiles-10th-and-90th-percentile"></a>

    Custom Percentiles 10th and 90th Percentile

    To get custom percentiles like the 10th and 90th percentiles using Polars’ describe() function, you can pass a list of percentiles as a parameter.

    
    # Using describe() with custom percentiles: 10th and 90th percentile
    ser2 = ser.describe(percentiles=[0.1, 0.9])
    print("Descriptive statistics with custom percentiles:\n", ser2)
    
    # Output:
    # Descriptive statistics with custom percentiles:
    3 shape: (8, 2)
    ┌────────────┬───────────┐
    │ statistic  ┆ value     │
    │ ---        ┆ ---       │
    │ str        ┆ f64       │
    ╞════════════╪═══════════╡
    │ count      ┆ 5.0       │
    │ null_count ┆ 0.0       │
    │ mean       ┆ 30.0      │
    │ std        ┆ 15.811388 │
    │ min        ┆ 10.0      │
    │ 10%        ┆ 10.0      │
    │ 90%        ┆ 50.0      │
    │ max        ┆ 50.0      │
    └────────────┴───────────┘
    

    <a id="h-change-interpolation-method-to-linear"></a>

    Change Interpolation Method to 'linear'

    When using describe() with percentiles, you can specify the interpolation method to determine how the percentiles are computed.

    To set the interpolation method to 'linear', simply include the interpolation argument when calling describe().

    
    # Describe with custom percentiles and linear interpolation
    ser2 = ser.describe(percentiles=[0.1, 0.9], interpolation='linear')
    print("Descriptive statistics with custom percentiles and linear interpolation:\n", ser2)
    
    # Output:
    # Descriptive statistics with custom percentiles and linear interpolation:
    # shape: (8, 2)
    ┌────────────┬───────────┐
    │ statistic  ┆ value     │
    │ ---        ┆ ---       │
    │ str        ┆ f64       │
    ╞════════════╪═══════════╡
    │ count      ┆ 5.0       │
    │ null_count ┆ 0.0       │
    │ mean       ┆ 30.0      │
    │ std        ┆ 15.811388 │
    │ min        ┆ 10.0      │
    │ 10%        ┆ 14.0      │
    │ 90%        ┆ 46.0      │
    │ max        ┆ 50.0      │
    └────────────┴───────────┘
    

    <a id="h-excluding-nan-values-in-summary-statistics"></a>

    Excluding NaN Values in Summary Statistics

    By default, Polars’ describe() function excludes NaN values when computing summary statistics, so you usually don’t have to do anything special.

    
    import polars as pl
    
    # Create a Series with some NaN values
    ser_with_nan = pl.Series("numbers", [10, 20, None, 40, 50])
    
    # Describe ignoring NaN (default behavior)
    ser2 = ser_with_nan.describe()
    print("\nDescriptive statistics excluding NaN values:\n", ser2)
    
    # Output:
    # Descriptive statistics excluding NaN values:
    # shape: (9, 2)
    ┌────────────┬───────────┐
    │ statistic  ┆ value     │
    │ ---        ┆ ---       │
    │ str        ┆ f64       │
    ╞════════════╪═══════════╡
    │ count      ┆ 4.0       │
    │ null_count ┆ 1.0       │
    │ mean       ┆ 30.0      │
    │ std        ┆ 18.257419 │
    │ min        ┆ 10.0      │
    │ 25%        ┆ 20.0      │
    │ 50%        ┆ 40.0      │
    │ 75%        ┆ 40.0      │
    │ max        ┆ 50.0      │
    └────────────┴───────────┘
    

    <a id="h-series-with-negative-and-positive-numbers"></a>

    Series with Negative and Positive Numbers

    Creating a Polars Series that includes both negative and positive numbers, and then obtaining its descriptive statistics by applying the Series.describe() method.

    
    import polars as pl
    
    # Series with negative and positive numbers
    ser = pl.Series("numbers", [-50, -20, 0, 30, 60])
    
    # Get descriptive statistics
    ser2 = ser.describe()
    print("Descriptive statistics:\n", ser2)
    
    # Output:
    # Descriptive statistics:
    # shape: (9, 2)
    ┌────────────┬───────────┐
    │ statistic  ┆ value     │
    │ ---        ┆ ---       │
    │ str        ┆ f64       │
    ╞════════════╪═══════════╡
    │ count      ┆ 5.0       │
    │ null_count ┆ 0.0       │
    │ mean       ┆ 4.0       │
    │ std        ┆ 42.778499 │
    │ min        ┆ -50.0     │
    │ 25%        ┆ -20.0     │
    │ 50%        ┆ 0.0       │
    │ 75%        ┆ 30.0      │
    │ max        ┆ 60.0      │
    └────────────┴───────────┘
    

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

    Conclusion

    In conclusion, the polars Series.describe() is a powerful and convenient method for quickly generating a comprehensive statistical summary of your data. It provides essential descriptive statistics such as count, mean, standard deviation, min, max, and customizable percentiles, all while gracefully handling missing values.

    Happy Learning!!

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

    Related Articles

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

    Reference