
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/03_schema_projection.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_03_schema_projection.py>`
        to download the full example code.

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

.. _sphx_glr_auto_examples_03_schema_projection.py:


Schema Projection (Explicit Columns)
====================================

This example shows how to register a Schema Projection from ordered steps, then
export that projection as a Pandera ``DataFrameSchema``.

.. GENERATED FROM PYTHON SOURCE LINES 10-12

Setup
-----

.. GENERATED FROM PYTHON SOURCE LINES 12-16

.. code-block:: Python

    import pandera.pandas as pa

    from pandera_catalog import PanderaCatalog








.. GENERATED FROM PYTHON SOURCE LINES 17-19

Register canonical source schemas
---------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 19-39

.. code-block:: Python

    catalog = PanderaCatalog()
    sensor_schema = pa.DataFrameSchema(
        columns={
            "sensor_id": pa.Column(str, nullable=False),
            "temperature": pa.Column(float),
            "humidity": pa.Column(float, nullable=True),
            "site": pa.Column(str),
        },
        name="sensor_readings",
    )
    catalog.register("sensor_readings", sensor_schema)
    site_schema = pa.DataFrameSchema(
        columns={
            "country": pa.Column(str),
            "site": pa.Column(str, nullable=False),
        },
        name="site_metadata",
    )
    catalog.register("site_metadata", site_schema)








.. GENERATED FROM PYTHON SOURCE LINES 40-42

Register a projection with ordered steps
----------------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 42-54

.. code-block:: Python

    catalog.register_projection(
        name="reporting_projection",
        steps=[
            {"schema": "site_metadata", "kind": "columns", "names": ["country"]},
            {"schema": "sensor_readings", "kind": "columns", "names": ["site", "sensor_id"]},
            {"schema": "sensor_readings", "kind": "columns", "names": ["temperature"]},
        ],
        description="Columns used by the reporting export",
    )
    print("Projection names:", catalog.list_projections())
    print("Projection entry:", catalog.get_projection_entry("reporting_projection"))





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

 .. code-block:: none

    Projection names: ['reporting_projection']
    Projection entry: SchemaProjectionEntry(name='reporting_projection', steps=[SchemaProjectionStep(schema='site_metadata', kind='columns', names=['country']), SchemaProjectionStep(schema='sensor_readings', kind='columns', names=['site', 'sensor_id']), SchemaProjectionStep(schema='sensor_readings', kind='columns', names=['temperature'])], description='Columns used by the reporting export')




.. GENERATED FROM PYTHON SOURCE LINES 55-57

Export the projected schema
---------------------------

.. GENERATED FROM PYTHON SOURCE LINES 57-59

.. code-block:: Python

    projected_schema = catalog.export_projection("reporting_projection")
    print("Exported columns:", list(projected_schema.columns.keys()))




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

 .. code-block:: none

    Exported columns: ['country', 'site', 'sensor_id', 'temperature']





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

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


.. _sphx_glr_download_auto_examples_03_schema_projection.py:

.. only:: html

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

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

      :download:`Download Jupyter notebook: 03_schema_projection.ipynb <03_schema_projection.ipynb>`

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

      :download:`Download Python source code: 03_schema_projection.py <03_schema_projection.py>`

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

      :download:`Download zipped: 03_schema_projection.zip <03_schema_projection.zip>`


.. only:: html

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

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