Skip to content

Commit

Permalink
Adding rename_columns method to Parsons Table (#923)
Browse files Browse the repository at this point in the history
* added rename_columns for multiple cols

* linted

* added clarification to docs about dict structure

* updated docs

---------

Co-authored-by: mattkrausse <[email protected]>
  • Loading branch information
mkrausse-ggtx and mattkrausse authored Nov 16, 2023
1 parent e4c6a47 commit e350d5f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/table.rst
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ of commonly used methods. The full list can be found in the API section.
- Remove a column
* - :py:meth:`~parsons.etl.etl.ETL.rename_column`
- Rename a column
* - :py:meth:`~parsons.etl.etl.ETL.rename_columns`
- Rename multiple columns
* - :py:meth:`~parsons.etl.etl.ETL.move_column`
- Move a column within a table
* - :py:meth:`~parsons.etl.etl.ETL.cut`
Expand Down
29 changes: 29 additions & 0 deletions parsons/etl/etl.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,35 @@ def rename_column(self, column_name, new_column_name):

return self

def rename_columns(self, column_map):
"""
Rename multiple columns
`Args:`
column_map: dict
A dictionary of columns and new names.
The key is the old name and the value is the new name.
Example dictionary:
{'old_name': 'new_name',
'old_name2': 'new_name2'}
`Returns:`
`Parsons Table` and also updates self
"""

# Check if old column name exists and new column name does not exist
for old_name, new_name in column_map.items():
if old_name not in self.table.columns():
raise KeyError(f"Column name {old_name} does not exist")
if new_name in self.table.columns():
raise ValueError(f"Column name {new_name} already exists")

# Uses the underlying petl method
self.table = petl.rename(self.table, column_map)

return self

def fill_column(self, column_name, fill_value):
"""
Fill a column in a table
Expand Down
28 changes: 28 additions & 0 deletions test/test_etl.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,34 @@ def test_column_rename_dupe(self):
# Test that we can't rename to a column that already exists
self.assertRaises(ValueError, self.tbl.rename_column, "last", "first")

def test_rename_columns(self):
# Test renaming columns with a valid column_map
column_map = {"first": "firstname", "last": "lastname"}
self.tbl.rename_columns(column_map)
self.assertEqual(self.tbl.columns, ["firstname", "lastname"])

def test_rename_columns_partial(self):
# Test renaming only some columns
column_map = {"first": "firstname"}
self.tbl.rename_columns(column_map)
self.assertEqual(self.tbl.columns, ["firstname", "last"])

def test_rename_columns_nonexistent(self):
# Test renaming a column that doesn't exist
column_map = {"nonexistent": "newname"}
self.assertRaises(KeyError, self.tbl.rename_columns, column_map)

def test_rename_columns_empty(self):
# Test renaming with an empty column_map
column_map = {}
self.tbl.rename_columns(column_map)
self.assertEqual(self.tbl.columns, ["first", "last"])

def test_rename_columns_duplicate(self):
# Test renaming to a column name that already exists
column_map = {"first": "last"}
self.assertRaises(ValueError, self.tbl.rename_columns, column_map)

def test_fill_column(self):
# Test that the column is filled
tbl = Table(self.lst)
Expand Down

0 comments on commit e350d5f

Please sign in to comment.