Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename map #1795

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion packages/vaex-core/vaex/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -3664,8 +3664,24 @@ def add_virtual_column(self, name, expression, unique=False):
self._save_assign_expression(valid_name)
self.signal_column_changed.emit(self, valid_name, "add")

def rename(self, name, new_name, unique=False):
def _renames(self, names, unique=False):
"""Renames a column or variable, and rewrite expressions such that they refer to the new name"""
columns = self.get_column_names()
ret = []
for name, new_name in names.items():
if name == new_name:
continue
if name not in columns:
continue
new_name = vaex.utils.find_valid_name(new_name, used=None if not unique else self.get_column_names(hidden=True))
self._rename(name, new_name, rename_meta_data=True)
ret.append(new_name)
return ret

def rename(self, name, new_name=None, unique=False):
"""Renames a column or variable, and rewrite expressions such that they refer to the new name"""
if isinstance(name, dict):
return self._renames(name, unique=unique)
if name == new_name:
return
new_name = vaex.utils.find_valid_name(new_name, used=None if not unique else self.get_column_names(hidden=True))
Expand Down
12 changes: 12 additions & 0 deletions tests/rename_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
from common import *


def test_renames(df_local):
ds = df_local
new_columns = ds.rename({'x': 'x1', 'y': 'y1'})
assert new_columns == ['x1', 'y1']
current_columns = ds.get_column_names()
for column in new_columns:
assert column in current_columns
for column in ['x', 'y']:
assert column not in current_columns


def test_rename(ds_filtered):
ds = ds_filtered
ds['r'] = ds.x
Expand Down