fumitoh / modelx

Use Python like a spreadsheet!
https://modelx.io
GNU Lesser General Public License v3.0
89 stars 20 forks source link

How to change the data being used in any of the models, for eg. BasicTerm_M? #87

Closed AADYABAJAJ closed 8 months ago

AADYABAJAJ commented 9 months ago

Hello,

Many thanks for promptly responding to the queries till now.

As per my understanding, currently the model point data is being generated using a .ipynb file and an excel file is created, and that data is being read by the model. So in case we want to modify the model point data, what approach do we take?

Say for example, the user has the information that he wants the model to read and calculate the liability for. He has this information in an xlsx file. How can the user make the model read this?

Many thanks for your help!

fumitoh commented 9 months ago

By default, all tables are imported as pandas DataFrames. To replace an existing DataFrame, use the Model.update_pandas method. To assign a new DataFrame, use Model.new_pandas or UserSpace.new_pandas.

For example,

import pandas as pd
import modelx as mx

df = pd.read_excel('new_model_point_table.xlsx', index_col=0)

model = mx.read_model('BasicTerm_S')

# The code below replaces the old model_point_table with the new df
model.update_pandas(df, model.Projection.model_point_table)  

# The code below makes the new df avaialble as model.Projection.new_table  
model.Projection.new_pandas("new_table", "new_table.xlsx", data=df, file_type="excel")  
AADYABAJAJ commented 8 months ago

Many thanks for your prompt response.

Saw the below error when I typed below line of code: model.update_pandas(df, model.Projection.model_point_table)

Error: Traceback (most recent call last):

File "\\ipykernel_664\524511074.py", line 1, in <cell line: 1> model.update_pandas(df, model.Projection.model_point_table)

File "\\WPy64-31020-20230513\WPy64-31020\python-3.10.2.amd64\lib\site-packages\modelx\core\model.py", line 174, in update_pandas return self._impl.model.refmgr.update_value(old_data, new_data)

File "\\WPy64-31020-20230513\WPy64-31020\python-3.10.2.amd64\lib\site-packages\modelx\core\model.py", line 2420, in update_value raise ValueError("value not referenced")

ValueError: value not referenced


I hope this is not an import error? I have imported the following libraries - import modelx as mx import lifelib import pandas as pd

fumitoh commented 8 months ago

My apologies, the 1st and 2nd arguments to update_pandas should be in the reverse order, i.e. the old comes first and the new comes second.

AADYABAJAJ commented 8 months ago

Perfect, the code works smoothly. Thanks for coming to the rescue!