
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/02_load_schema_from_yaml.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_auto_examples_02_load_schema_from_yaml.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_02_load_schema_from_yaml.py:


Loading a Schema from YAML
===========================

This example shows how to serialise a Pandera schema to YAML, then reload it
via :func:`~pandera_catalog.schemas.load_schema_from_yaml` and register it in
a :class:`~pandera_catalog.PanderaCatalog`.

.. GENERATED FROM PYTHON SOURCE LINES 11-13

Setup
-----

.. GENERATED FROM PYTHON SOURCE LINES 13-21

.. code-block:: Python


    from pathlib import Path
    import tempfile

    import pandera.pandas as pa
    from pandera_catalog import PanderaCatalog
    from pandera_catalog.schemas import load_schema_from_yaml








.. GENERATED FROM PYTHON SOURCE LINES 22-25

Define and serialise a schema to YAML
--------------------------------------
Pandera has built-in YAML serialisation via ``.to_yaml()``.

.. GENERATED FROM PYTHON SOURCE LINES 25-72

.. code-block:: Python


    schema = pa.DataFrameSchema(
        columns={
            "sensor_id": pa.Column(str, nullable=False),
            "temperature": pa.Column(
                float,
                checks=[
                    pa.Check.greater_than(-50.0),
                    pa.Check.less_than(100.0),
                ],
            ),
            "humidity": pa.Column(
                float,
                checks=pa.Check.in_range(0.0, 100.0),
                nullable=True,
            ),
        },
        name="sensor_readings",
    )

    # Write the schema to a temporary YAML file.
    with tempfile.TemporaryDirectory() as tmp_dir:
        yaml_path = Path(tmp_dir) / "sensor_readings.yaml"
        yaml_path.write_text(schema.to_yaml())

        print("--- YAML content ---")
        print(yaml_path.read_text())

        # %%
        # Load the schema back from YAML
        # --------------------------------

        loaded_schema = load_schema_from_yaml(yaml_path)
        print(f"Loaded schema type: {type(loaded_schema).__name__}")
        print(f"Columns: {list(loaded_schema.columns.keys())}")

        # %%
        # Register in the catalog
        # ------------------------

        catalog = PanderaCatalog()
        catalog.register("sensor_readings", loaded_schema, tags=["iot", "sensors"])

        print(f"Catalog contents: {catalog.list()}")
        entry = catalog.get_entry("sensor_readings")
        print(f"Tags: {entry.tags}")





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    --- YAML content ---
    schema_type: dataframe
    columns:
      sensor_id:
        dtype: string[python]
      temperature:
        dtype: float64
        greater_than: -50.0
        less_than: 100.0
      humidity:
        dtype: float64
        nullable: true
        in_range:
          min_value: 0.0
          max_value: 100.0
          include_min: true
          include_max: true
    name: sensor_readings
    drop_invalid_rows: false

    Loaded schema type: DataFrameSchema
    Columns: ['sensor_id', 'temperature', 'humidity']
    Catalog contents: ['sensor_readings']
    Tags: ['iot', 'sensors']




.. GENERATED FROM PYTHON SOURCE LINES 73-75

Validate data with the loaded schema
-------------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 75-88

.. code-block:: Python


    import pandas as pd

    df = pd.DataFrame(
        {
            "sensor_id": ["S001", "S002"],
            "temperature": [22.5, 18.3],
            "humidity": [55.0, None],
        }
    )

    validated = catalog.get("sensor_readings").validate(df)
    print(validated)




.. rst-class:: sphx-glr-script-out

 .. code-block:: none

      sensor_id  temperature  humidity
    0      S001         22.5      55.0
    1      S002         18.3       NaN





.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 0.021 seconds)


.. _sphx_glr_download_auto_examples_02_load_schema_from_yaml.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: 02_load_schema_from_yaml.ipynb <02_load_schema_from_yaml.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: 02_load_schema_from_yaml.py <02_load_schema_from_yaml.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: 02_load_schema_from_yaml.zip <02_load_schema_from_yaml.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
