Python package to translate between gdx (GAMS data) and pandas.
To work in memory between GDX and pandas DataFrames, note that the two primary points of reference are GDX files on disk and python dicts of {symbol_name: pandas.DataFrame}, where each pandas.DataFrame contains data for a single parameter whose value is in the last column, and that column is labeled 'value' (case insensitive). Alternatively for GDX files, you can directly access the intermediate gdxdict.gdxdict objects in memory.
Then, to convert from GDX to DataFrames:
import gdxpds
gdx_file = 'C:\path_to_my_gdx\data.gdx'
dataframes = gdxpds.to_dataframes(gdx_file)
for symbol_name, df in dataframes.items():
print("Doing work with {}.".format(symbol_name))
And within the loop, df is a pandas.DataFrame with unhelpful column names except for 'value'.
And vice-versa:
import gdxpds
# assume we have a DataFrame df with last column 'value'
data_ready_for_GAMS = { 'symbol_name': df }
gdx_file = 'C:\path_to_my_output_gdx\data_to_send_to_gams.gdx'
gdx = gdxpds.to_gdx(data_ready_for_GAMS, gdx_file)
Note that providing a gdx_file is optional, and the returned gdx is an object of type gdxpds.gdxdict.gdxdict.
Additional functions include:
gdxpds.list_symbols
gdxpds.to_dataframe
The package also includes command line utilities for converting between GDX and CSV, see
python C:\your_python_path\Scripts\gdx_to_csv.py --help
python C:\your_python_path\Scripts\csv_to_gdx.py --help
(Ideally, after installing gdxpds you should be able to simply call python gdx_to_csv.py
, but for some reason python does not use either PATH or PYTHONPATH when looking for scripts, see stackoverflow question.)
- Python 2.6 or higher 2.X; Python 3.4 or higher 3.X
- pandas (In general you will want the SciPy stack. Anaconda comes with it, or see my notes for Windows.)
- psutil (optional--for monitoring memory use)
- nose (optional--for running tests)
- GAMS Python bindings
-
See GAMS/win64/XX.X/apifiles/readme.txt
-
Run the following for the correct version of the Python bindings
python setup.py install
-
GAMS/win64/XX.X/apifiles/Python/api/setup.py works for Python 2.X
-
For Python 3.X, use GAMS/win64/XX.X/apifiles/Python/api_3X/setup.py, for which you will need GAMS version >= 24.5.1 (Python 3.4, Windows and Linux), 24.7.4 (Python 3.4, Mac OS X), or >= 24.8.4 (Python 3.6)
-
pip install git+https://github.com/NREL/gdx-pandas.git@master
or
pip install git+https://github.com/NREL/gdx-pandas.git@v0.6.0
Versions are listed at https://github.com/NREL/gdx-pandas/releases.
Unfortunately on Windows, while this command nominally works (after running it, pip list
will show gdxpds as installed), it may exit with errors.
After installation, you can test the package using nose:
nosetests gdxpds
pip uninstall gdxpds