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

How to export a table to gds? #61

Closed alda982 closed 5 years ago

alda982 commented 5 years ago

I am meaning to export a 2-dimensional parameter (table) to GAMS. While I am able to actually create a 2-dimensional parameter in GAMS, I am only able to put the values on the diameter. How can I write values for all parts of the table?

Sample output in gdx:

image

Sample code:

import gdxpds import pandas as pds import numpy as np

out_file = 'my_new_gdx_data.gdx' with gdxpds.gdx.GdxFile() as gdx:

Create a new set with one dimension

gdx.append(gdxpds.gdx.GdxSymbol('my_set',gdxpds.gdx.GamsDataType.Set, dims=['uu']))
data = pds.DataFrame([['u' + str(i)] for i in range(1,11)])
data['Value1'] = True
gdx[-1].dataframe = data
# Create a new parameter with one dimension
gdx.append(gdxpds.gdx.GdxSymbol('my_parameter',gdxpds.gdx.GamsDataType.Parameter, dims=['u','k']))
data = pds.DataFrame([['u' + str(i), 'k' + str(i), i*3] for i in range(1,11)],
columns=(gdx[-1].dims + gdx[-1].value_col_names))
gdx[-1].dataframe = data
gdx.write(out_file)
jebob commented 5 years ago

Your dataframe only has 10 values in it, so when you write it out you get 10 values in the GDX. One way to get more values would be something like data = pds.DataFrame([['u' + str(i), 'k' + str(j), i+j] for i in range(1,11) for j in range(1,11)],

alda982 commented 5 years ago

Thanks a million. You are absolutely right! Now it works as I intend it to.