Built with Tutify.io

    AIStudyLabs

    Polars Series struct.schema() – Explained by Examples

    Polars Series struct.schema() – Explained by Examples

    The polars.Series.struct.schema property in Polars is used to get the schema of a struct-type Series. When you have a Series where each element is a struct (similar to a nested record or dictionary with named fields), struct.schema returns a dictionary describing the field names and their data types within that struct.

    In this article, I will describe the Polars Series struct.schema() method, including its syntax, parameters, and usage, and details how it returns a Python dictionary that maps field names to their corresponding Polars data types.

    Key Points –

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

    Polars Series struct.schema() Introduction

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

    
    # Syntax of Series struct.schema()
    Series.struct.schema = {}
    

    Following are the parameters of the series struct.schema() method.

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

    Usage of Polars Series struct.schema()

    Series.struct.schema() returns the schema of a Polars Series whose elements are structs. Specifically, it provides a list of tuples describing each field name and its data type within the struct. This lets you see the structure, field names, and types of the nested data inside the Series.

    First, let’s create a Polars Series.

    
    import polars as pl
    
    # Create a struct Series
    ser = pl.Series("tech_col", [
        {"Courses": "Spark", "Fees": 20000},
        {"Courses": "PySpark", "Fees": 30000},
        {"Courses": "Hadoop", "Fees": 40000}
    ])
    print("Original Series:\n", ser)
    

    Yields below output.

    A Polars Series with a struct data type that includes integer and string fields. Below is a straightforward example demonstrating a struct Series with integer and string fields, along with how to retrieve its schema.

    
    # Inspect the inner-struct schema
    ser2 = ser.struct.schema
    print("Struct schema:", ser2)
    

    Here,

    <a id="h-struct-with-nested-struct-inside"></a>

    Struct with Nested Struct Inside

    A Polars Series containing a nested struct, demonstrating how to create such a Series and how to examine the schema at both the outer and inner struct levels. Each element in the Series is a struct that includes another struct nested within it.

    
    import polars as pl
    
    # Create a struct Series with a nested struct field
    ser = pl.Series(
        "nested_struct",
        [
            {"id": 1, "info": {"age": 25, "city": "NY"}},
            {"id": 2, "info": {"age": 30, "city": "LA"}},
        ]
    )
    
    # Outer struct schema
    outer_schema = ser.struct.schema
    print("\nOuter struct schema:\n",outer_schema)
    
    # Extract nested struct 'info' as a Series and get its schema
    info_struct = ser.struct.field("info")
    nested_schema = info_struct.struct.schema
    print("Nested struct 'info' schema:\n",nested_schema)
    
    # Output:
    # Outer struct schema:
    # Schema({'id': Int64, 'info': Struct({'age': Int64, 'city': String})})
    
    # Nested struct 'info' schema:
    # Schema({'age': Int64, 'city': String})
    

    Here,

    <a id="h-struct-with-float-and-boolean-fields"></a>

    Struct with Float and Boolean Fields

    A Polars Series in which each element is a struct with a float and a boolean field. Here's a simple example demonstrating how to create such a Series and check its schema.

    
    import polars as pl
    
    # Struct Series with float and boolean fields
    ser = pl.Series(
        "metrics",
        [
            {"score": 9.5, "passed": True},
            {"score": 7.3, "passed": False},
        ]
    )
    
    # Inspect inner-struct schema (property, no parentheses)
    ser2 = ser.struct.schema
    print("Struct schema:\n", ser2)
    
    # Output:
    # Struct schema:
    # Schema({'score': Float64, 'passed': Boolean})
    

    Here,

    <a id="h-struct-with-optional-null-fields"></a>

    Struct with Optional (Null) Fields

    A struct field can be optional, allowing it to contain null values in certain rows. While the values may be missing, the field still adheres to a consistent schema across all entries.

    
    import polars as pl
    
    # Struct Series with optional (nullable) fields
    ser = pl.Series(
        "person",
        [
            {"name": "Alice", "age": 30},
            {"name": "Bob", "age": None},     # 'age' is null here
            {"name": None, "age": 45},        # 'name' is null here
        ]
    )
    
    # Schema inspection
    ser2 = ser.struct.schema
    print("\nStruct schema:\n", ser2)
    
    # Output:
    # Struct schema:
    # {'name': pl.Utf8, 'age': pl.Int64}
    

    Here,

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

    Conclusion

    In conclusion, the struct.schema property in Polars is a useful feature for examining the structure of complex or nested data. Whether the struct fields hold integers, strings, floats, booleans, or even other structs, struct.schema provides a clear and easy way to view each field's name and data type.

    Happy Learning!!

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

    Related Articles

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

    Reference