gdx-pandas
==========
Python package to translate between gdx (GAMS data) and pandas.
[Install](#install) | [Use](#use) | [Uninstall](#uninstall)
## Use
There are two main ways to use gdxpds. The first use case is the one that was
initially supported: direct conversion between GDX files on disk and pandas
DataFrames or a csv version thereof. The Version 1.0.0 rewrite intoduces a
second style of use, that is, interfacing with GDX files and symbols via the
`gdxpds.gdx.GdxFile` and `gdxpds.gdx.GdxSymbol` classes.
[Direct Conversion](#direct-conversion) | [Backend Classes](#backend-classes)
### Direct Conversion
The two primary points of reference for the direct conversion utilities are GDX
files on disk and python dicts of {symbol_name: pandas.DataFrame}, where
each pandas.DataFrame contains data for a single set, parameter, equation, or
variable. For sets and parameters, the last column of the DataFrame is assumed to
contain the value of the element, which for sets should be `True`, and for
parameters should be a `float` (or one of the `gdxpds.gdx.NUMPY_SPECIAL_VALUES`).
Equations and variables have additional 'value' columns, in particular a level,
a marginal value, a lower bound, an upper bound, and a scale, as enumerated in
`gdxpds.gdx.GamsValueType`. These values are all assumed to be found in the last
five columns of the DataFrame, also see `gdxpds.gdx.GAMS_VALUE_COLS_MAP`.
The basic interface to convert from GDX to DataFrames is:
```python
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 vice-versa:
```python
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.gdx.GdxFile`.
Additional functions include:
- `gdxpds.list_symbols`
- `gdxpds.to_dataframe` (If the call to this method includes
old_interface=False, then the return value will be a plain DataFrame, not a
{'symbol_name': df} dict.)
The package also includes command line utilities for converting between GDX and
CSV, see
```bash
python C:\your_python_path\Scripts\gdx_to_csv.py --help
python C:\your_python_path\Scripts\csv_to_gdx.py --help
```
### Backend Classes
The basic functionalities described above can also be achieved with direct use
of the backend classes now available in `gdxpds.gdx`. To duplicate the GDX read
functionality shown above one would write:
```python
import gdxpds
gdx_file = 'C:\path_to_my_gdx\data.gdx'
with gdxpds.gdx.GdxFile(lazy_load=False) as f:
f.read(gdx_file)
for symbol in f:
symbol_name = symbol.name
df = symbol.dataframe
print("Doing work with {}:\n{}".format(symbol_name,df.head()))
```
The backend especially gives more control over creating new data in GDX format.
For example:
```python
import gdxpds
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=['u']))
data = pds.DataFrame([['u' + str(i)] for i in range(1,11)])
data['Value'] = 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']))
data = pds.DataFrame([['u' + str(i), i*100] for i in range(1,11)],
columns=(gdx[-1].dims + gdx[-1].value_col_names))
gdx[-1].dataframe = data
gdx.write(out_file)
```
## Install
### Preliminaries
- 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](http://elainethale.wordpress.com/programming-notes/python-environment-set-up/).)
- For Python versions < 3.4, enum34. Also **uninstall the enum package** if it is installed.
- psutil (optional--for monitoring memory use)
- pytest (optional--for running tests)
- GAMS Python bindings
- See GAMS/win64/XX.X/apifiles/readme.txt on Windows,
GAMS/gamsXX.X_osx_x64_64_sfx/apifiles/readme.txt on Mac, or
/opt/gams/gamsXX.X_linux_x64_64_sfx/apifiles/readme.txt on Linux
- Run the following for the correct version of the Python bindings
```bash
python setup.py install
```
or
```bash
python setup.py build --build-base=/path/to/somwhere/you/have/write/access install
```
with the latter being for the case when you can install packages into
Python but don't have GAMS directory write access.
- .../apifiles/Python/api/setup.py works for Python 2.7
- For other versions of Python, especially 3.X, use
.../apifiles/Python/api_XX/setup.py. For Python 3.X in particular 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)
### Get the Latest Package
```bash
pip install git+https://github.com/NREL/gdx-pandas.git@master
```
or
```bash
pip install git+https://github.com/NREL/gdx-pandas.git@v1.0.3
```
Versions are listed at https://github.com/NREL/gdx-pandas/releases.
After installation, you can test the package using pytest:
```bash
pytest --pyargs gdxpds
```
If the tests fail due to permission IOErrors, apply `chmod g+x` and `chmod a+x`
to the `gdx-pandas/gdxpds/test` folder.
## Uninstall
```
pip uninstall gdxpds
```
挣扎的蓝藻
- 粉丝: 14w+
- 资源: 15万+
最新资源
- CC2530无线zigbee裸机代码实现按键控制LED开关.zip
- CC2530无线zigbee裸机代码实现按键控制PWM灯光强度.zip
- CC2530无线zigbee裸机代码实现按键控制流水灯.zip
- 无感FOC电机三相控制高速吹风筒方案 FU6812L+FD2504S 电压AC220V 功率80W 最高转速20万RPM 方案优势:响应快、效率高、噪声低、成本低 控制方式:三相电机无感FOC 闭环方
- CC2530无线zigbee裸机代码实现查询方式使用定时器.zip
- CC2530无线zigbee裸机代码实现串口UART0发送字符串.zip
- CC2530无线zigbee裸机代码实现串口UART0收发字符串.zip
- CC2530无线zigbee裸机代码实现串口发送指令控制LED灯.zip
- CC2530无线zigbee裸机代码实现定时器T1的使用.zip
- CC2530无线zigbee裸机代码实现定时器T3的使用.zip
- 基于51单片机的PWM波形发生器设计(Protues仿真)-毕业设计
- 模块化多电平变流器 MMC 的VSG控制 同步发电机控制 MATLAB–Simulink仿真模型 5电平三相MMC,采用VSG控制 受端接可编辑三相交流源,直流侧接无穷大电源提供调频能量 设置频率
- 锁相环学习电路,有教程 对新手非常友好,一看就懂 1,输出频率800MHz或者1GHz, 采用Ring-VCO的结构 2,输入参考频率20MHz 3,分频器是40-50分频 4,电荷泵电流
- MF000588-ASP.NET信息中心标准化管理系统源码.zip
- 基于51单片机的烟雾采集报警系统(protues仿真)-毕业设计
- 模拟器银河麒麟是基于Linux发行版Ubuntu开发的自主可控操作系统,为我国信息基础建设提供了重要支撑 截至目前,银河麒麟V10的软件仓库已经提供了大量国产软件,但在特定情况下,我们可能还是希望使用
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈