Input/Output#

polars_extensions.io.read_schema(file: str)[source]#

Load a JSON schema file and return a Polars Schema object.

Parameters:
filestr

Input JSON file.

Returns:
pl.Schema
polars_extensions.io.read_xml(
xml_input: str | bytes,
record_path: str | None = None,
include_attributes: bool = True,
flatten: bool = True,
strict: bool = False,
) DataFrame[source]#

Reads and normalizes XML into a flat or semi-structured Polars DataFrame.

Parameters:
xml_inputstr | bytes

XML string or file path.

record_pathstr, optional

Dot-separated path to record nodes (e.g., "channel.item"). Can optionally include the root element (e.g., "catalog.product").

include_attributesbool

Whether to include XML attributes in the output.

flattenbool

Recursively explode lists and unnest structs.

strictbool

True -> Polars raises on type mismatch. False -> wraps primitives in lists to avoid schema mismatch.

Returns:
pl.DataFrame

Examples

import polars_extensions as plx
xml_data = '''
<catalog>
    <product sku="A123" available="true">
        <name>Mechanical Keyboard</name>
        <price currency="USD">129.99</price>
        <features>
            <feature>RGB Lighting</feature>
            <feature>Hot-swappable switches</feature>
            <feature>Aluminum frame</feature>
        </features>
    </product>

    <product sku="B456" available="false">
        <name>Noise Cancelling Headphones</name>
        <price currency="USD">299.00</price>
        <features>
            <feature>ANC</feature>
            <feature>Bluetooth 5.0</feature>
        </features>
    </product>
</catalog>
'''

df = plx.read_xml(xml_data,flatten=True)
df
shape: (5, 6)
┌────────────────┬────────────────┬────────────────┬───────────────┬───────────────┬───────────────┐
│ catalog.produc ┆ catalog.produc ┆ catalog.produc ┆ catalog.produ ┆ catalog.produ ┆ catalog.produ │
│ t.product.sku  ┆ t.product.avai ┆ t.product.name ┆ ct.product.pr ┆ ct.product.pr ┆ ct.product.fe │
│ ---            ┆ la…            ┆ .t…            ┆ ice.…         ┆ ice.…         ┆ atur…         │
│ str            ┆ ---            ┆ ---            ┆ ---           ┆ ---           ┆ ---           │
│                ┆ str            ┆ str            ┆ str           ┆ str           ┆ str           │
╞════════════════╪════════════════╪════════════════╪═══════════════╪═══════════════╪═══════════════╡
│ A123           ┆ true           ┆ Mechanical     ┆ USD           ┆ 129.99        ┆ RGB Lighting  │
│                ┆                ┆ Keyboard       ┆               ┆               ┆               │
│ A123           ┆ true           ┆ Mechanical     ┆ USD           ┆ 129.99        ┆ Hot-swappable │
│                ┆                ┆ Keyboard       ┆               ┆               ┆ switches      │
│ A123           ┆ true           ┆ Mechanical     ┆ USD           ┆ 129.99        ┆ Aluminum      │
│                ┆                ┆ Keyboard       ┆               ┆               ┆ frame         │
│ B456           ┆ false          ┆ Noise          ┆ USD           ┆ 299.00        ┆ ANC           │
│                ┆                ┆ Cancelling     ┆               ┆               ┆               │
│                ┆                ┆ Headphones     ┆               ┆               ┆               │
│ B456           ┆ false          ┆ Noise          ┆ USD           ┆ 299.00        ┆ Bluetooth 5.0 │
│                ┆                ┆ Cancelling     ┆               ┆               ┆               │
│                ┆                ┆ Headphones     ┆               ┆               ┆               │
└────────────────┴────────────────┴────────────────┴───────────────┴───────────────┴───────────────┘
polars_extensions.io.write_schema(
schema: DataFrame | Schema,
file: str,
)[source]#

Save a Polars schema to a JSON file.

Parameters:
schemaDataFrame | Schema

The schema source.

filestr

Output JSON file.