Built with Tutify.io

    AIStudyLabs

    Polars Series Shape Usage & Examples

    Polars Series Shape Usage & Examples

    In Polars, Series.shape represents the dimensions of the Series as a tuple. Since a Polars Series is one-dimensional, Series.shape returns a tuple with a single element indicating the number of items in that Series.

    In this article, I will explain the Polars Series shape attribute by covering its syntax, parameters, and usage, and show how it returns a tuple containing a single integer that represents the number of elements in the Series.

    Key Points –

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

    Polars Series Shape Introduction

    Let’s know the syntax of the series shape

    
    # Syntax of series shape
    property Series.shape: tuple[int]
    

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

    Parameters of the Series shape

    Following are the parameters of the series shape

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

    Return Value

    This function returns a tuple of one integer representing the number of elements in the Series.

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

    Usage of Polars Series Shape

    It returns a tuple containing the number of elements in the Series. Since a Series is 1-dimensional, it’s a tuple with one integer, e.g., (5,).

    First, let’s create a Polars Series.

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

    Yields below output.

    The shape attribute provides the dimensions of a Series. Because a Series is one-dimensional, shape returns a tuple containing a single value, the number of elements in the Series.

    
    # Get and print the shape of the Series
    print("Shape of Series:", ser.shape)
    

    Here,

    <a id="h-empty-series-shape"></a>

    Empty Series Shape

    Using Polars Series.shape with an empty Series returns the dimensions of the Series, which has no elements. An empty Series contains zero elements and is commonly used as a placeholder or a starting point before populating it with data.

    
    import polars as pl
    
    # Create an empty Series
    empty_series = pl.Series("empty", [])
    
    # Print the empty Series
    print("Empty Series:\n", empty_series)
    
    print("Shape:", empty_series.shape)
    
    # Output:
    # Empty Series:
    # shape: (0,)
    # Series: 'empty' [null]
    # [
    # ]
    Shape: (0,)
    

    Here,

    <a id="h-series-with-strings"></a>

    Series with Strings

    Using Polars Series.shape with a Series of strings shows the size of the Series. Polars Series can store various data types, including strings. Here’s how to create a string-based Series and check its shape.

    
    import polars as pl
    
    # Create a Series of strings
    ser = pl.Series("Courses", ["spark", "pandas", "bigdata", "Hyperion"])
    print("Shape of Series:", ser.shape)
    
    # Output:
    # Shape of Series: (4,)
    

    Here,

    <a id="h-series-with-nulls"></a>

    Series with Nulls

    Polars Series.shape counts the total number of elements in a Series, including any null (missing) values. Polars Series can hold nulls, which are useful for representing incomplete or missing data.

    
    import polars as pl
    
    # Create a Series with some null values
    series_with_nulls = pl.Series("data", [2, None, 4, None, 6])
    
    # Print the Series
    print("Series with nulls:\n", series_with_nulls)
    print("Shape:", series_with_nulls.shape)
    
    # Output:
    # Series with nulls:
    # shape: (5,)
    # Series: 'data' [i64]
    # [
    #	2
    #	null
    #	4
    #	null
    #	6
    # ]
    # Shape: (5,)
    

    Here,

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

    Conclusion

    In conclusion, the shape attribute in a Polars Series offers a straightforward and reliable way to determine the size of the Series by returning a tuple that reflects the number of elements it includes. Regardless of whether the Series holds strings, numbers, or null values, shape consistently provides a tuple representing its length.

    Happy Learning!!

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

    Related Articles

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

    Reference