Skip to content

Commit bae9399

Browse files
author
Michael Erickson
committed
Initial implementation of CASTable.rename
1 parent dd3a683 commit bae9399

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

swat/cas/table.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5959,8 +5959,46 @@ def drop_duplicates(self, casout, subset=[]):
59595959
# def reindex_like(self, *args, **kwargs):
59605960
# raise NotImplementedError
59615961

5962-
# def rename(self, *args, **kwargs):
5963-
# raise NotImplementedError
5962+
def rename(self, columns, errors='ignore', inplace=True, **kwargs):
5963+
'''
5964+
Rename columns of the CASTable.
5965+
5966+
Parameters
5967+
----------
5968+
columns : dict or function
5969+
Transformations to apply to the columns of the CASTable.
5970+
errors : 'ignore' or 'raise', default 'ignore'
5971+
Given a dict with a key that does not match one of the columns
5972+
in the CASTable, this determines whether to ignore that key or
5973+
raise a KeyError.
5974+
5975+
Returns
5976+
-------
5977+
:class:`CASTable`
5978+
'''
5979+
5980+
#Columns is a dict:
5981+
alterTable = []
5982+
if isinstance(columns, dict):
5983+
#Errors to be raised -> check all keys upfront
5984+
if errors == 'raise':
5985+
for key in columns.keys():
5986+
if key not in self.columns:
5987+
raise KeyError("Column is not found in CASTable: " + key)
5988+
5989+
#Convert Pandas-style dict to CAS-style list of dicts
5990+
for oldName, newName in columns.items():
5991+
alterTable.append({'name': oldName, 'rename': newName})
5992+
#Columns is a function:
5993+
elif callable(columns):
5994+
#Iterate through all table columns and apply function
5995+
for col in self.columns:
5996+
alterTable.append({'name': col, 'rename': columns(col)})
5997+
else:
5998+
raise TypeError("Columns must be a dict or function")
5999+
6000+
#Use list of dicts to rename using alterTable action
6001+
return self._retrieve('alterTable', columns=alterTable)
59646002

59656003
def reset_index(self, level=None, drop=False, inplace=False,
59666004
col_level=0, col_fill='', **kwargs):

0 commit comments

Comments
 (0)