Skip to content

Commit

Permalink
fixed bug in dataframe
Browse files Browse the repository at this point in the history
  • Loading branch information
cliffckerr committed Sep 24, 2019
1 parent 4917374 commit a86212e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
19 changes: 14 additions & 5 deletions sciris/sc_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class dataframe(object):
Works for both numeric and non-numeric data.
Version: 2019mar25
Version: 2019sep24
'''

def __init__(self, cols=None, data=None, nrows=None):
Expand All @@ -48,6 +48,7 @@ def __init__(self, cols=None, data=None, nrows=None):
self.make(cols=cols, data=data, nrows=nrows)
return None


def __repr__(self, spacing=2):
''' spacing = space between columns '''
if not self.cols: # No keys, give up
Expand Down Expand Up @@ -86,6 +87,7 @@ def __repr__(self, spacing=2):

return output


def _val2row(self, value=None):
''' Convert a list, array, or dictionary to the right format for appending to a dataframe '''
if isinstance(value, dict):
Expand All @@ -107,6 +109,7 @@ def _val2row(self, value=None):
raise Exception(errormsg)
return output


def _sanitizecol(self, col, die=True):
''' Take None or a string and return the index of the column '''
if col is None:
Expand All @@ -132,13 +135,17 @@ def _sanitizecol(self, col, die=True):
output = None
return output


@staticmethod
def _cast(arr):
''' Attempt to cast an array to different data types '''
try: # Try to cast the whole array to the type of the first element
output = np.array(arr, dtype=type(arr[0]))
return output
except: # If anything goes wrong, do nothing
if ut.isnumber(arr[0]): # Check that the first element is a number before trying to cast to an array
try: # If it is, try to cast the whole array to the type of the first element
output = np.array(arr, dtype=type(arr[0]))
return output
except: # If anything goes wrong, do nothing
return arr
else:
return arr


Expand Down Expand Up @@ -190,6 +197,7 @@ def __getitem__(self, key=None, die=True, cast=True):
output = None
return output


def __setitem__(self, key, value=None):
if value is None:
value = np.zeros(self.nrows, dtype=object)
Expand Down Expand Up @@ -622,6 +630,7 @@ def jsonify(self, cols=None, rows=None, header=None, die=True):
for col in exportdf.cols:
try:
datum = exportdf.get(cols=col,rows=r)
print(f'I AM DATUM: {col}, {r}, {datum}, {type(datum)}')
thisrow.append(datum)
except:
pass # This has already been handled by the validation above
Expand Down
4 changes: 2 additions & 2 deletions sciris/sc_version.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__all__ = ['__version__', '__versiondate__', '__license__']

__version__ = '0.14.7'
__versiondate__ = '2019-09-19'
__version__ = '0.14.8'
__versiondate__ = '2019-09-24'
__license__ = 'Sciris %s (%s) -- (c) Sciris.org' % (__version__, __versiondate__)

0 comments on commit a86212e

Please sign in to comment.