Skip to content

Commit

Permalink
Merge pull request #28 from NREL/robust_set_fix
Browse files Browse the repository at this point in the history
Bug fix and change README to point to pypi for installation.
  • Loading branch information
elainethale authored Mar 2, 2018
2 parents 4de1e7e + 58b214e commit 7936861
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
v1.0.4, 03/02/18 -- make sure set 'Value' column fix works even when dims are all named '*'
v1.0.3, 02/15/18 -- fix up set 'Value' column when write to GDX
v1.0.2, 01/19/18 -- instructions and additional filepaths for running pytest on
installed package
Expand Down
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
gdx-pandas
==========
# gdx-pandas
[![PyPI](https://img.shields.io/pypi/v/gdxpds.svg)](https://pypi.python.org/pypi/gdxpds/)

Python package to translate between gdx (GAMS data) and pandas.

Expand Down Expand Up @@ -145,16 +145,24 @@ with gdxpds.gdx.GdxFile() as gdx:
### Get the Latest Package
```bash
pip install git+https://github.com/NREL/gdx-pandas.git@master
pip install gdxpds
```
or
or
```bash
pip install git+https://github.com/NREL/[email protected].3
pip install git+https://github.com/NREL/[email protected].4
```
Versions are listed at https://github.com/NREL/gdx-pandas/releases.
or
```bash
pip install git+https://github.com/NREL/gdx-pandas.git@master
```
Versions are listed at [pypi](https://pypi.python.org/pypi/gdxpds/) and
https://github.com/NREL/gdx-pandas/releases.
After installation, you can test the package using pytest:
Expand Down
2 changes: 1 addition & 1 deletion gdxpds/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
'''


__version__ = '1.0.3'
__version__ = '1.0.4'

import logging
import os
Expand Down
32 changes: 24 additions & 8 deletions gdxpds/gdx.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ def gdx_val_equal(val1,val2,gdxf):
return True
return val1 == val2

def replace_df_column(df,colname,new_col):
# This weirdness with replacing columns is a workaround for
# pandas crashing on multiple columns called '*'.
cols = df.columns
tmpcols = [col if col != '*' else 'aaa' for col in cols ]
df.columns = tmpcols
df[colname] = new_col
df.columns = cols
return


class GdxError(Error):
def __init__(self, H, msg):
Expand Down Expand Up @@ -657,20 +667,15 @@ def dataframe(self, data):
self._dataframe = pds.DataFrame(data,columns=self.dims + self.value_col_names)

if self.data_type == GamsDataType.Set:
logger.debug(self._dataframe.head())
self._fixup_set_value()
return

def _init_dataframe(self):
self._dataframe = pds.DataFrame([],columns=self.dims + self.value_col_names)
if self.data_type == GamsDataType.Set:
colname = self._dataframe.columns[-1]
# This weirdness with replacing columns is a workaround for
# pandas crashing on multiple columns called '*'.
cols = self._dataframe.columns
tmpcols = [col if col != '*' else 'aaa' for col in cols ]
self._dataframe.columns = tmpcols
self._dataframe[colname] = self._dataframe[colname].astype(c_bool)
self._dataframe.columns = cols
replace_df_column(self._dataframe,colname,self._dataframe[colname].astype(c_bool))
return

def _fixup_set_value(self):
Expand All @@ -687,10 +692,21 @@ def _fixup_set_value(self):
"""
assert self.data_type == GamsDataType.Set

warned_values = []

def fix_individual_value(val):
try:
return c_bool(val)
except:
if val not in warned_values:
logger.warn("Replacing {} with {}".format(val,c_bool(True)))
warned_values.append(val)
return c_bool(True)

colname = self._dataframe.columns[-1]
assert colname == self.value_col_names[0], "Unexpected final column in Set dataframe"
self._dataframe[colname].fillna(value=True,inplace=True)
self._dataframe[colname] = self._dataframe[colname].apply(lambda x: c_bool(x))
replace_df_column(self._dataframe,colname,self._dataframe[colname].apply(lambda x: c_bool(x)))
return

@property
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name = 'gdxpds',
version = "1.0.3",
version = "1.0.4",
author = 'Elaine T. Hale',
author_email = '[email protected]',
packages = ['gdxpds', 'gdxpds.test'],
Expand Down

0 comments on commit 7936861

Please sign in to comment.