Skip to content

Commit

Permalink
Merge pull request #26 from NREL/fix_booleans
Browse files Browse the repository at this point in the history
Bug fix release has more fool-proof set writing.
  • Loading branch information
elainethale authored Feb 16, 2018
2 parents 79c99d5 + 7a6d435 commit d3d94f1
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 8 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.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
v1.0.1, 09/12/17 -- workaround bug fix for library conflicts between gdxcc and pandas
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ pip install git+https://github.com/NREL/gdx-pandas.git@master
or
```bash
pip install git+https://github.com/NREL/[email protected].2
pip install git+https://github.com/NREL/[email protected].3
```
Versions are listed at https://github.com/NREL/gdx-pandas/releases.
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.2'
__version__ = '1.0.3'

import logging
import os
Expand Down
29 changes: 28 additions & 1 deletion gdxpds/gdx.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
from collections import MutableSequence

import copy
from ctypes import c_bool
from enum import Enum
import logging
from numbers import Number
Expand Down Expand Up @@ -654,6 +655,9 @@ def dataframe(self, data):
self._dataframe.columns = self.dims + self.value_col_names
else:
self._dataframe = pds.DataFrame(data,columns=self.dims + self.value_col_names)

if self.data_type == GamsDataType.Set:
self._fixup_set_value()
return

def _init_dataframe(self):
Expand All @@ -665,10 +669,30 @@ def _init_dataframe(self):
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(bool)
self._dataframe[colname] = self._dataframe[colname].astype(c_bool)
self._dataframe.columns = cols
return

def _fixup_set_value(self):
"""
Tricky to get boolean set values to come through right.
isinstance(True,Number) == True and float(True) = 1, but
isinstance(c_bool(True),Number) == False, and this keeps the default
value of 0.0.
Could just test for isinstance(,bool), but this fix has the added
advantage of speaking the GDX bindings data type language, and also
fills in any missing values, so users no longer need to actually specify
self.dataframe['Value'] = True.
"""
assert self.data_type == GamsDataType.Set

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))
return

@property
def num_records(self):
if self.loaded:
Expand Down Expand Up @@ -728,6 +752,9 @@ def write(self,index=None):
if not self.loaded:
raise Error("Cannot write unloaded symbol {}.".format(repr(self.name)))

if self.data_type == GamsDataType.Set:
self._fixup_set_value()

if index is not None:
self._index = index

Expand Down
8 changes: 4 additions & 4 deletions gdxpds/test/test_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
[/LICENSE]
'''

from ctypes import c_bool
import logging
import os
import subprocess as subp
Expand Down Expand Up @@ -158,12 +159,11 @@ def test_from_scratch_sets(manage_rundir):
data = pds.DataFrame([['u' + str(i)] for i in range(1,11)])
data['Value'] = True
gdx[-1].dataframe = data
assert gdx[-1].dataframe[gdx[-1].dataframe.columns[-1]].dtype == bool
assert isinstance(gdx[-1].dataframe[gdx[-1].dataframe.columns[-1]].values[0], c_bool)
gdx.append(gdxpds.gdx.GdxSymbol('my_other_set',gdxpds.gdx.GamsDataType.Set,dims=['u']))
data = pds.DataFrame([['u' + str(i)] for i in range(1,11)],columns=['u'])
data['Value'] = True
gdx[-1].dataframe = gdx[-1].dataframe.append(data)
assert gdx[-1].dataframe[gdx[-1].dataframe.columns[-1]].dtype == bool
gdx[-1].dataframe = gdx[-1].dataframe.append(data)
gdx.write(os.path.join(outdir,'my_sets.gdx'))
with gdxpds.gdx.GdxFile(lazy_load=False) as gdx:
gdx.read(os.path.join(outdir,'my_sets.gdx'))
Expand All @@ -172,4 +172,4 @@ def test_from_scratch_sets(manage_rundir):
assert sym.dims[0] == 'u'
assert sym.data_type == gdxpds.gdx.GamsDataType.Set
assert sym.num_records == 10
assert sym.dataframe[sym.dataframe.columns[-1]].dtype == bool
assert isinstance(sym.dataframe[sym.dataframe.columns[-1]].values[0],c_bool)
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.2",
version = "1.0.3",
author = 'Elaine T. Hale',
author_email = '[email protected]',
packages = ['gdxpds', 'gdxpds.test'],
Expand Down

0 comments on commit d3d94f1

Please sign in to comment.