NREL / gdx-pandas

Python interface to read and write GAMS GDX files using pandas.DataFrames as the intermediate data format.
BSD 3-Clause "New" or "Revised" License
43 stars 15 forks source link

Add helper methods for appending data to a gdxpds.gdx.GdxFile #74

Closed elainethale closed 1 year ago

elainethale commented 4 years ago

E.g.

def append_set(gdx_file, set_name, df, cols=None, dim_names=None, 
        description=None):
    # ensure df is DataFrame and not Series
    logger.debug("Defining set based on:\n{!r}".format(df))
    tmp = pd.DataFrame(df)
    # select down to data we actually want
    if cols is not None:
        tmp = tmp[cols]
    if dim_names is not None:
        tmp.columns = dim_names
    # define the symbol
    logger.debug("After processing, set will be defined using:\n{!r}".format(tmp))
    gdx_file.append(gdxpds.gdx.GdxSymbol(set_name, gdxpds.gdx.GamsDataType.Set, 
        dims = list(tmp.columns), description = description))
    # define the data for the symbol
    gdx_file[-1].dataframe = tmp
    return

def append_parameter(gdx_file, param_name, df, cols=None, dim_names=None, 
        description=None):
    # pre-process the data
    logger.debug("Defining parameter based on:\n{!r}".format(df))
    tmp = df.copy()
    if cols is not None:
        tmp = tmp[cols]
    if dim_names is not None:
        tmp.columns = dim_names + ['Value']
    # define the symbol
    logger.debug("After processing, parameter will be defined using:\n{!r}".format(tmp))
    gdx_file.append(gdxpds.gdx.GdxSymbol(param_name, gdxpds.gdx.GamsDataType.Parameter,
        dims = list(tmp.columns)[:-1], description = description))
    # define the data for the symbol
    gdx_file[-1].dataframe = tmp
    return
elainethale commented 1 year ago

Has been available for awhile now.