UserSpace.new_pandas#
- UserSpace.new_pandas(name, path, data, filetype)#
Assigns a pandas object to a Reference associating a new
PandasData
objectpandas DataFrame and Series objects can be assigned to References by the normal assignment operation, such as
space.x = df
, but the pandas DataFrame/Series assigned this way are saved in a binary file together with other Reference objects. This method allows a DataFrame/Series object passed asdata
to be saved in a separate file.This method creates a
PandasData
object that wraps a DataFrame or Series passed asdata
, and assignsdata
or thePandasData
object to a Reference namedname
in this Space/Model.When the model is saved, the DataFrame or Series is written to a file whose path is given by the
path
parameter, and whose format is specified by thefiletype
parameter. Ifpath
is relative, it is interpreted relative to the model folder. Thefiletype
can take either “excel” or “csv”.If “excel” is given, the pandas object is written to an Excel file. The file name in
path
must have either “.xlsx”, “.xlsm” or “.xls” extention. This method internally uses pandas.read_excel function and to_excel method for reading from and writing to Excel files, so appropriate Excel engines for reading and writing Excel files must be installed, depending on the types of Excel files. See pandas’ document for the required packeges for Excel engines.By default,
data
is assigned toname
, and the associatedPandasData
object is assigned todata._mx_dataclient
. IfFalse
is passed toexpose_data
, thePandasData
object is assigned toname
. To getdata
, call thePandasData
or get itsvalue
attribute.Example
The script below creates a sample DataFrame
df
:>>> index = pd.date_range("20210101", periods=3) >>> df = pd.DataFrame(np.random.randn(3, 3), index=index, columns=list("XYZ")) >>> df X Y Z 2021-01-01 0.184497 0.140037 -1.599499 2021-01-02 -1.029170 0.588080 0.081129 2021-01-03 0.028450 -0.490102 0.025208
The code below assigns the DataFrame created above to a Reference named
x
inspace
, and at the same time creates aPandasData
object and assigns it to the_mx_dataclient
attribute of the DataFrame:>>> space.new_pandas("x", "Space1/df.xlsx", data=df, filetype="excel") >>> space.x X Y Z 2021-01-01 0.184497 0.140037 -1.599499 2021-01-02 -1.029170 0.588080 0.081129 2021-01-03 0.028450 -0.490102 0.025208 >>> space.x._mx_dataclient <modelx.io.pandasio.PandasData at 0x15ebc562356>
When the model is saved, the DataFrame is written to an Excel file named df.xlsx placed under the Space1 folder in model.
>>> model.write("model") # `model` is the parent of `space`
When the model is read back by
modelx.read_model()
function, the DataFrame is read from the file:>>> model2 = mx.read_model("model", name="Model2") >>> model2.Space1.x X Y Z 2021-01-01 0.184497 0.140037 -1.599499 2021-01-02 -1.029170 0.588080 0.081129 2021-01-03 0.028450 -0.490102 0.025208
If
False
is passed toexpose_data
, thePandasData
object instead ofdata
itself is assigned tox
To get the DataFrame, call thePandasData
object or access itsvalue
property:>>> space.x <modelx.io.pandasio.PandasData at 0x15efa565548> >>> space.x() # or space.value X Y Z 2021-01-01 0.184497 0.140037 -1.599499 2021-01-02 -1.029170 0.588080 0.081129 2021-01-03 0.028450 -0.490102 0.025208
- Parameters
name (
str
) – Name of the Referencepath – A path to a file to save the Pandas object. If a relative path is given, it is relative to the model folder.
data – pandas DataFrame or Series
filetype – String to indicate file format. (“excel” or “csv”)
expose_data (
bool
, optional) – IfTrue
, assignsdata
toname
, otherwise assigns thePandasData
object associted withdata
toname
.True
by default.
Changed in version 0.13.0: Add the
expose_data
parameter. By default,data
is assigned instead of itsPandasData
objectNew in version 0.12.0.
See also