.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/discipline/dataframe.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_discipline_dataframe.py: Create a discipline that uses pandas DataFrames =============================================== .. GENERATED FROM PYTHON SOURCE LINES 22-27 .. code-block:: default from gemseo.api import configure_logger from gemseo.core.discipline import MDODiscipline from numpy import ndarray from pandas import DataFrame .. GENERATED FROM PYTHON SOURCE LINES 28-30 Import ------ .. GENERATED FROM PYTHON SOURCE LINES 30-34 .. code-block:: default configure_logger() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 35-63 Create a discipline that uses a DataFrame ----------------------------------------- We will create a class for a simple discipline that computes an output variable ``y = 1 - 0.2 * x`` where ``x`` is an input variable. For whatever reason, the business logic of this discipline uses a pandas DataFrame to store the input and output values outside |g|. Although |g| disciplines only handle input and output variables that are NumPy arrays, their local data and default input values can use DataFrame objects. The input and output grammars of the discipline shall use a naming convention to access the names of the columns of a DataFrame. The naming convention is built with the name of the input or output, the character ``~`` (this can be changed) and the name of the DataFrame column. The code executed by the discipline is in the ``_run`` method, where ``self.local_data``, i.e. the local data, has automatically been initialized with the default inputs and updated with the inputs passed to the discipline. A DataFrame can be retrieved by querying the corresponding key, e.g. ``df``, in the local data and then changes can be made to this DataFrame, e.g. ``discipline.local_data["df"]["x"] = value``. The default inputs and local data are instances of :class:`.DisciplineData`. .. seealso:: :class:`.DisciplineData` has more information about how DataFrames are handled. .. GENERATED FROM PYTHON SOURCE LINES 63-81 .. code-block:: default class DataFrameDiscipline(MDODiscipline): def __init__(self): super().__init__(grammar_type=MDODiscipline.SIMPLE_GRAMMAR_TYPE) self.default_inputs = {"df": DataFrame(data={"x": [0.0]})} self.input_grammar.update({"df~x": ndarray}) self.output_grammar.update({"df~y": ndarray}) def _run(self): df = self.local_data["df"] df["y"] = 1.0 - 0.2 * df["x"] # The code above could also have been written as # self.local_data["df~y"] = 1.0 - 0.2 * self.local_data["df~x"] # self.local_data["df"]["y"] = 1.0 - 0.2 * self.local_data["df"]["x"] .. GENERATED FROM PYTHON SOURCE LINES 82-84 Instantiate the discipline -------------------------- .. GENERATED FROM PYTHON SOURCE LINES 84-86 .. code-block:: default discipline = DataFrameDiscipline() .. GENERATED FROM PYTHON SOURCE LINES 87-90 Execute the discipline ---------------------- Then, we can execute it easily, either considering default inputs: .. GENERATED FROM PYTHON SOURCE LINES 90-92 .. code-block:: default print(discipline.execute()) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none {'df': x y 0 0.0 1.0} .. GENERATED FROM PYTHON SOURCE LINES 93-94 or using new inputs: .. GENERATED FROM PYTHON SOURCE LINES 94-95 .. code-block:: default print(discipline.execute({"df~x": [1.0]})) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none {'df': x y 0 1.0 0.8} .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.015 seconds) .. _sphx_glr_download_examples_discipline_dataframe.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: dataframe.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: dataframe.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_