Skip to content

Commit c62062e

Browse files
author
Kevin D Smith
committed
Add support for eval method on CASTable
1 parent 8fb839e commit c62062e

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

swat/cas/table.py

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3230,8 +3230,56 @@ def fillna(dframe, label, value):
32303230
# def diff(self, periods=1, axis=0):
32313231
# raise NotImplementedError
32323232

3233-
# def eval(self, expr, **kwargs):
3234-
# raise NotImplementedError
3233+
def eval(self, expr, inplace=True, kwargs=None):
3234+
'''
3235+
Evaluate a CAS table expression
3236+
3237+
Parameters
3238+
----------
3239+
expr : string
3240+
The expression to evaluate
3241+
inplace : bool, optional
3242+
If the expression contains an assignment and inplace=True,
3243+
add the column to the existing table.
3244+
kwargs : dict, optional
3245+
Not supported
3246+
3247+
Returns
3248+
-------
3249+
:class:`CASColumn`
3250+
If `expr` does not contain an assignment
3251+
None
3252+
If inplace=True and `expr` contains an assignment
3253+
:class:`CASTable`
3254+
If inplace=False and `expr` contains an assignment
3255+
3256+
'''
3257+
col = self.copy()
3258+
3259+
# Check for assignment
3260+
if re.match(r'^\s*\S+\s*=', expr):
3261+
colname = re.match(r'^\s*(\S+)\s*', expr).group(1)
3262+
expr = re.sub(r'^\s*\S+\s*=s*', r'', expr)
3263+
insert = True
3264+
else:
3265+
colname = '_eval_%s_' % self.get_connection()._gen_id()
3266+
insert = False
3267+
3268+
col.append_computed_columns(colname, '%s = %s; ' % (_nlit(colname),
3269+
_escape_string(expr)))
3270+
3271+
col = col._to_column(colname)
3272+
3273+
# Insert or return
3274+
if insert:
3275+
if inplace:
3276+
self[colname] = col
3277+
else:
3278+
newtbl = self.copy()
3279+
newtbl[colname] = col
3280+
return newtbl
3281+
else:
3282+
return col
32353283

32363284
# def kurt(self, *args, **kwargs):
32373285
# raise NotImplementedError

swat/tests/cas/test_table.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4570,5 +4570,29 @@ def test_plot_scatter(self):
45704570
df.plot.scatter('MSRP', 'Horsepower')
45714571
)
45724572

4573+
def test_eval(self):
4574+
tbl = self.table
4575+
df = self.get_cars_df()
4576+
4577+
dfcol = df.eval('(MPG_City + MPG_Highway) / 2')
4578+
tblcol = tbl.eval('(MPG_City + MPG_Highway) / 2')
4579+
self.assertColsEqual(dfcol, tblcol)
4580+
self.assertEqual(list(df.columns), list(tbl.columns))
4581+
4582+
dfcol = df.eval('MPG_Avg = (MPG_City + MPG_Highway) / 2', inplace=True)
4583+
tblcol = tbl.eval('MPG_Avg = (MPG_City + MPG_Highway) / 2', inplace=True)
4584+
self.assertTrue(dfcol is None)
4585+
self.assertTrue(tblcol is None)
4586+
self.assertTablesEqual(df, tbl)
4587+
4588+
dfcol = df.eval('MPG_Avg = (MPG_City + MPG_Highway) / 2', inplace=False)
4589+
tblcol = tbl.eval('MPG_Avg = (MPG_City + MPG_Highway) / 2', inplace=False)
4590+
self.assertTrue(dfcol is not None)
4591+
self.assertTrue(tblcol is not None)
4592+
self.assertTrue(dfcol is not df)
4593+
self.assertTrue(tblcol is not tbl)
4594+
self.assertTablesEqual(dfcol, tblcol)
4595+
4596+
45734597
if __name__ == '__main__':
45744598
tm.runtests()

0 commit comments

Comments
 (0)