fumitoh / modelx

Use Python like a spreadsheet!

Home Page:https://modelx.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

AADYABAJAJ opened this issue · comments

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!

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")  

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

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.

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