IOSpec types#
BaseIOSpec#
- class BaseIOSpec[source]#
Abstract base class for storing objects in files.
This base class is inherited from by IOSpec classes, such as
PandasData
andModuleData
, and defines properties shared among the child classes. TheModel.get_spec
method returns the IOSpec object associated with a given object referenced in the model.Model.iospecs
returns a list of all the IOSpec associated with the objects referenced in the model.Example
The code below returns the
PandasData
object associated with a DataFramedf
referenced in the model:>>> model.get_spec(df) <PandasData path='df.xlsx' file_type='excel' sheet='df1'>
The code below returns a list of all the IOSpec objects associated with the objects referenced in the model:
>>> model.iospecs [<PandasData path='df.xlsx' file_type='excel' sheet='df1'>, <PandasData path='df.xlsx' file_type='excel' sheet='s1'>, <ModuleData path='mod1.py'>]
Changed in version 0.20.0: Renamed from
BaseDataSpec
toBaseIOSpec
Changed in version 0.18.0: The
is_hidden
parameter is removed.Changed in version 0.18.0: Renamed from
BaseDataClient
toBaseDataSpec
.See also
- property BaseIOSpec.path#
File path that the object is written to.
This property is defined in
BaseIOSpec
, and returns the path to a file to which the object is written to when the model is saved by thewrite()
method. The returned path is an instance of the Path class defined in pathlib module in the Python standard library. The Path class is either WindowsPath or PosixPath, depending on the platform the Python session is running on.This property also works as a setter to set the path.
str
or any path-like object can be given.Example
In the code below,
df
is a DataFrame referenced inmodel
, and the IPython sessin is running on Windows. The DataFrame is saved in an Excel file named df.xlsx in the model folder:>>> model.get_spec(df).path WindowsPath('df.xlsx')
The next assignment changes the file path to files/df2.xlsx under the model folder:
>>> model.get_spec(df).path = 'files/df2.xlsx' >>> model.get_spec(df).path WindowsPath('files/df.xlsx')
See also
PandasData#
- class PandasData(data, sheet=None)[source]#
A subclass of
BaseIOSpec
that associates a pandas DataFrame or Series with a fileA
PandasData
holds a pandas DataFrame or Series object, and associates it with a file for writing and reading the object.A
PandasData
can be created only byUserSpace.new_pandas
orModel.new_pandas
.The DataFrame or Series held in
PandasData
objects are accessible throughvalue
property or a call()
method.- Parameters
path – Path to a file for saving data. If a relative path is given, it is relative to the model folder.
data – a pandas DataFrame or Series.
filetype (
str
) – String to specify the file format. “excel” or “csv”
- path#
A path to the associated file as a pathlib.Path object. See
BaseIOSpec.path
.
Changed in version 0.18.0: The
expose_data
parameter is removed.
- property PandasData.value#
pandas DataFrame or Series held in the object
- property PandasData.sheet#
The name of the sheet to which the data is written to
ModuleData#
- class ModuleData(module=None)[source]#
A subclass of
BaseIOSpec
that associates a user module with its source file in the modelA
ModuleData
is created either byUserSpace.new_module
orModel.new_module
when a user module is assigned to a Reference. TheModuleData
is assigned to the_mx_dataclient
attribute of the module.New in version 0.13.0.
- property ModuleData.value#
Module held in the object
ExcelRange#
- class ExcelRange(range_, sheet=None, keyids=None)[source]#
Mapping class for accessing Excel ranges
An ExcelRange is a dict-like object that represents a range in an Excel file. The user can read values from the range or write values to it by the subscription operator
[]
. ExcelRange is a mapping class, thus it implements all the mapping methods and operations.ExcelRange objects can only be created by the
Model.new_excel_range
orUserSpace.new_excel_range
method.ExcelRange
is a subclass of theBaseIOSpec
abstract class. Theiospecs
property list all theBaseIOSpec
instances held in the Model includingExcelRange
objects.New in version 0.9.0.