Tips for preserving the data type of ref data after saving model (pickled data) #107
Replies: 1 comment 1 reply
-
It appears there's a bug in modelx where it fails to correctly restore the data types (dtypes) of string columns that only contain numeric values. I'll create an issue to address this. In the meantime, there's a workaround you can use. Instead of directly storing your file within the model, you can place it externally and read it into the model. With the release of modelx 0.25.0, it's now possible to determine the path of the model where it was loaded from or saved to. For instance, let’s say you have a data file named import modelx as mx
import pandas as pd
m = mx.new_model()
@mx.defcells
def df():
file_path = _model.path.parent / 'test.csv'
return pd.read_csv(file_path, dtype={'PROD_CODE': 'string', 'PLAN_CODE': 'string'}, index_col=['PROD_CODE', 'PLAN_CODE'])
m.write(r"C:\Users\xxx\yyy\TempModel") # Sets Model.path to C:\Users\xxx\yyy\TempModel
print(df()) # Outputs the DataFrame with the correct dtypes In this code, |
Beta Was this translation helpful? Give feedback.
-
Hi, I am really excited to test various modelx features nowadays.
I want to ask some tips about preserving the data type of ref data ( which I get via new_pandas function) after saving the model.
Below is an example which I faced some trouble.
sample data
I can get exact same data when I make the ref varable "test"
However, after I saved the model, it seems the data type I assigned to each column ( STRING to "PROD_CODE" and "PLAN_CODE" disappear)
--> When I reload the model, the data changed as below
after reloading the model
If there is any way to preserve the data type of each column I first assigned, pleas let me know.
Thank you for reading my question!
Beta Was this translation helpful? Give feedback.
All reactions