Skip to content

Commit 3486c96

Browse files
authored
Merge pull request #164 from mjerick25/dev-rename
Added implementation of CASTable.rename
2 parents bb13215 + 4e61bb3 commit 3486c96

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed

swat/cas/table.py

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5959,8 +5959,47 @@ 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', **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+
# Convert Pandas-style dict to CAS-style list of dicts
5984+
for oldName, newName in columns.items():
5985+
if oldName in self.columns:
5986+
alterTable.append({'name': oldName, 'rename': newName})
5987+
# If we encounter a key that doesn't exist as column
5988+
# and we are raising errors, we do that here
5989+
elif errors == 'raise':
5990+
raise KeyError("Column is not found in CASTable: " + oldName)
5991+
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+
self._retrieve('alterTable', columns=alterTable)
6002+
return self
59646003

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

swat/tests/cas/test_table.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5174,6 +5174,50 @@ def test_partition_inputs(self):
51745174
pcolinfo = pcolinfo.drop('ID', errors='ignore').drop('Label', errors='ignore')
51755175
self.assertTablesEqual(colinfo[['Two', 'Model', 'One', 'MSRP']], pcolinfo)
51765176

5177+
def test_rename(self):
5178+
# Pull in CASTable and Pandas Dataframe
5179+
tbl = self.table
5180+
df = self.get_cars_df()
5181+
5182+
# Rename by name:
5183+
makeCol = tbl['Make']
5184+
self.assertTrue(any(col in 'Make' for col in list(tbl.columns)))
5185+
tbl.rename({'Make': 'Manufacturer'})
5186+
# We use inplace=True as that's what CASTable.rename is doing
5187+
df.rename(columns={'Make': 'Manufacturer'}, inplace=True)
5188+
# No column named "Make" and a column named "Manufacturer"
5189+
self.assertFalse(any(col in 'Make' for col in list(tbl.columns)))
5190+
self.assertTrue(any(col in 'Manufacturer' for col in list(tbl.columns)))
5191+
# Shouldn't be a "new" column, just 'Make' renamed
5192+
self.assertEqual(makeCol, tbl['Manufacturer'])
5193+
# Shoud be same as pandas
5194+
self.assertTablesEqual(tbl, df)
5195+
5196+
# Rename by function:
5197+
# Column Manufacturer -> Manufacturer_0
5198+
tbl.rename(lambda col: col + "_0")
5199+
df.rename(columns=lambda col: col + "_0", inplace=True)
5200+
for col in list(tbl.columns):
5201+
# Last two characters should be _0 for each col
5202+
self.assertEqual(col[-2:], "_0")
5203+
self.assertTablesEqual(tbl, df)
5204+
5205+
# Rename by name for col that doesn't exist
5206+
# errors='ignore'
5207+
originalCols = list(copy.deepcopy(tbl.columns))
5208+
# This column doesn't exist, so it'll just ignore it
5209+
tbl.rename({'nope': 'nuh uh'})
5210+
df.rename(columns={'nope': 'nuh uh'}, inplace=True)
5211+
self.assertFalse(any(col in 'nope' for col in list(tbl.columns)))
5212+
self.assertListEqual(originalCols, list(tbl.columns))
5213+
self.assertTablesEqual(tbl, df)
5214+
5215+
# Rename by name for col that doesn't exist
5216+
# errors='raise'
5217+
with self.assertRaises(KeyError):
5218+
# This column doesn't exist and errors='raise', it'll raise an exception
5219+
tbl.rename(tbl.rename({'nope': 'nuh uh'}, errors='raise'))
5220+
51775221
def test_reset_index(self):
51785222
tbl = self.table
51795223

0 commit comments

Comments
 (0)