Skip to content

BUG: #19497 FIX. Add tupleize_cols option to internals._transform_index() #20526

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

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 4 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3642,6 +3642,10 @@ def rename(self, *args, **kwargs):
level : int or level name, default None
In case of a MultiIndex, only rename labels in the specified
level.
tupleize_cols : boolean, default False
In case of an Index, when True, create MultiIndex if possible.
False ensures that an Index will not be converted to a
MultiIndex if labels are 'rename'd.

Returns
-------
Expand Down
7 changes: 5 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,7 @@ def rename(self, *args, **kwargs):
inplace = kwargs.pop('inplace', False)
level = kwargs.pop('level', None)
axis = kwargs.pop('axis', None)
tupleize_cols = kwargs.pop('tupleize_cols', False)
if axis is not None:
axis = self._get_axis_number(axis)

Expand Down Expand Up @@ -970,8 +971,10 @@ def f(x):
baxis = self._get_block_manager_axis(axis)
if level is not None:
level = self.axes[axis]._get_level_number(level)
result._data = result._data.rename_axis(f, axis=baxis, copy=copy,
level=level)
result._data = \
result._data.rename_axis(f, axis=baxis, copy=copy,
level=level,
tupleize_cols=tupleize_cols)
result._clear_item_cache()

if inplace:
Expand Down
10 changes: 6 additions & 4 deletions pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -3280,7 +3280,8 @@ def set_axis(self, axis, new_labels):

self.axes[axis] = new_labels

def rename_axis(self, mapper, axis, copy=True, level=None):
def rename_axis(self, mapper, axis, copy=True, level=None,
tupleize_cols=False):
"""
Rename one of axes.

Expand All @@ -3293,7 +3294,8 @@ def rename_axis(self, mapper, axis, copy=True, level=None):

"""
obj = self.copy(deep=copy)
obj.set_axis(axis, _transform_index(self.axes[axis], mapper, level))
obj.set_axis(axis, _transform_index(self.axes[axis], mapper, level,
tupleize_cols=tupleize_cols))
return obj

def add_prefix(self, prefix):
Expand Down Expand Up @@ -5234,7 +5236,7 @@ def _safe_reshape(arr, new_shape):
return arr


def _transform_index(index, func, level=None):
def _transform_index(index, func, level=None, tupleize_cols=True):
"""
Apply function to all values found in index.

Expand All @@ -5251,7 +5253,7 @@ def _transform_index(index, func, level=None):
return MultiIndex.from_tuples(items, names=index.names)
else:
items = [func(x) for x in index]
return Index(items, name=index.name)
return Index(items, name=index.name, tupleize_cols=tupleize_cols)


def _putmask_smart(v, m, n):
Expand Down