.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/02_merging_schemas.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_02_merging_schemas.py: Merging Schemas =============== In this example, we will demonstrate how to merge multiple schemas .. GENERATED FROM PYTHON SOURCE LINES 8-26 .. code-block:: Python import inspect from pathlib import Path import yaml from elphick.pandera_utils.pandera_utils import load_schema_from_yaml, merge_schemas from elphick.pandera_utils.utils.pandera_io_pandas_io import to_yaml __file__ = Path(inspect.getfile(inspect.currentframe())).resolve() def print_schemafile(filepath: Path): with open(filepath, "r", encoding="utf-8") as f: schema_yaml = yaml.safe_load(f) print(yaml.dump(schema_yaml, sort_keys=False, indent=2)) .. GENERATED FROM PYTHON SOURCE LINES 27-30 Load Schemas ------------ Load and view the schemas from the YAML files .. GENERATED FROM PYTHON SOURCE LINES 30-35 .. code-block:: Python schema_filepath_1: Path = __file__.parents[1] / "assets/aliases_schema.yaml" schema_filepath_2: Path = __file__.parents[1] / "assets/category_schema.yaml" schema_filepath_3: Path = __file__.parents[1] / "assets/missing_sentinels_schema.yaml" .. GENERATED FROM PYTHON SOURCE LINES 36-38 .. code-block:: Python print_schemafile(schema_filepath_1) .. rst-class:: sphx-glr-script-out .. code-block:: none columns: my_pet_column: dtype: object metadata: pandera_utils: aliases: - pets - my_pets .. GENERATED FROM PYTHON SOURCE LINES 39-41 .. code-block:: Python print_schemafile(schema_filepath_2) .. rst-class:: sphx-glr-script-out .. code-block:: none columns: my_color_column: dtype: category checks: isin: - R - G - B metadata: pandera_utils: category: add_all_categories: true ordered: false label: map: R: Red G: Green B: Blue dtype: category description: map: R: The color red G: The color green B: The color blue dtype: category wavelength: map: R: 700 G: 546 B: 435 dtype: int .. GENERATED FROM PYTHON SOURCE LINES 42-44 .. code-block:: Python print_schemafile(schema_filepath_3) .. rst-class:: sphx-glr-script-out .. code-block:: none columns: my_nullable_int_column: dtype: Int8 nullable: true coerce: true metadata: pandera_utils: missing_sentinels: - -1 - -9 .. GENERATED FROM PYTHON SOURCE LINES 45-48 Merge Schemas ------------- Merge the schemas into a single schema .. GENERATED FROM PYTHON SOURCE LINES 48-53 .. code-block:: Python schema_1 = load_schema_from_yaml(schema_filepath_1) schema_2 = load_schema_from_yaml(schema_filepath_2) schema_3 = load_schema_from_yaml(schema_filepath_3) merged_schema = merge_schemas([schema_1, schema_2, schema_3]) .. GENERATED FROM PYTHON SOURCE LINES 54-56 Print the merged schema in a nicely formatted way use a temp file (formatting workaround only) .. GENERATED FROM PYTHON SOURCE LINES 56-57 .. code-block:: Python print(to_yaml(merged_schema)) .. rst-class:: sphx-glr-script-out .. code-block:: none schema_type: dataframe version: 0.0.0+dev0 columns: my_pet_column: title: null description: null dtype: object nullable: false checks: null unique: false coerce: false required: true regex: false metadata: pandera_utils: aliases: - pets - my_pets my_color_column: title: null description: null dtype: category nullable: false checks: isin: value: - R - G - B options: raise_warning: false ignore_na: true unique: false coerce: false required: true regex: false metadata: pandera_utils: category: add_all_categories: true ordered: false label: map: R: Red G: Green B: Blue dtype: category description: map: R: The color red G: The color green B: The color blue dtype: category wavelength: map: R: 700 G: 546 B: 435 dtype: int my_nullable_int_column: title: null description: null dtype: Int8 nullable: true checks: null unique: false coerce: true required: true regex: false metadata: pandera_utils: missing_sentinels: - -1 - -9 checks: null index: null dtype: null coerce: false strict: false name: null ordered: false unique: null report_duplicates: all unique_column_names: false add_missing_columns: false title: null description: null .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.016 seconds) .. _sphx_glr_download_auto_examples_02_merging_schemas.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 02_merging_schemas.ipynb <02_merging_schemas.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 02_merging_schemas.py <02_merging_schemas.py>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_