Skip to content

REF: Remove BlockManager.rename_axis #32349

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

Merged
merged 4 commits into from
Mar 11, 2020
Merged
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
6 changes: 2 additions & 4 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,6 @@ def rename(
continue

ax = self._get_axis(axis_no)
baxis = self._get_block_manager_axis(axis_no)
f = com.get_rename_function(replacements)

if level is not None:
Expand All @@ -984,9 +983,8 @@ def rename(
]
raise KeyError(f"{missing_labels} not found in axis")

result._data = result._data.rename_axis(
f, axis=baxis, copy=copy, level=level
)
new_index = ax._transform_index(f, level)
result.set_axis(new_index, axis=axis_no, inplace=True)
result._clear_item_cache()

if inplace:
Expand Down
21 changes: 21 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4738,6 +4738,27 @@ def map(self, mapper, na_action=None):

return Index(new_values, **attributes)

# TODO: De-duplicate with map, xref GH#32349
def _transform_index(self, func, level=None) -> "Index":
"""
Apply function to all values found in index.

This includes transforming multiindex entries separately.
Only apply function to one level of the MultiIndex if level is specified.
"""
if isinstance(self, ABCMultiIndex):
if level is not None:
items = [
tuple(func(y) if i == level else y for i, y in enumerate(x))
for x in self
]
else:
items = [tuple(func(y) for y in x) for x in self]
return type(self).from_tuples(items, names=self.names)
else:
items = [func(x) for x in self]
return Index(items, name=self.name, tupleize_cols=False)

def isin(self, values, level=None):
"""
Return a boolean array where the index values are in `values`.
Expand Down
2 changes: 0 additions & 2 deletions pandas/core/internals/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from pandas.core.internals.managers import (
BlockManager,
SingleBlockManager,
_transform_index,
concatenate_block_managers,
create_block_manager_from_arrays,
create_block_manager_from_blocks,
Expand All @@ -40,7 +39,6 @@
"_block_shape",
"BlockManager",
"SingleBlockManager",
"_transform_index",
"concatenate_block_managers",
"create_block_manager_from_arrays",
"create_block_manager_from_blocks",
Expand Down
41 changes: 1 addition & 40 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from pandas.core.arrays.sparse import SparseDtype
from pandas.core.base import PandasObject
from pandas.core.indexers import maybe_convert_indices
from pandas.core.indexes.api import Index, MultiIndex, ensure_index
from pandas.core.indexes.api import Index, ensure_index
from pandas.core.internals.blocks import (
Block,
CategoricalBlock,
Expand Down Expand Up @@ -216,23 +216,6 @@ def set_axis(self, axis: int, new_labels: Index) -> None:

self.axes[axis] = new_labels

def rename_axis(
self, mapper, axis: int, copy: bool = True, level=None
) -> "BlockManager":
"""
Rename one of axes.

Parameters
----------
mapper : unary callable
axis : int
copy : bool, default True
level : int or None, default None
"""
obj = self.copy(deep=copy)
obj.set_axis(axis, _transform_index(self.axes[axis], mapper, level))
return obj

@property
def _is_single_block(self) -> bool:
if self.ndim == 1:
Expand Down Expand Up @@ -1966,28 +1949,6 @@ def _compare_or_regex_search(a, b, regex=False):
return result


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

This includes transforming multiindex entries separately.
Only apply function to one level of the MultiIndex if level is specified.

"""
if isinstance(index, MultiIndex):
if level is not None:
items = [
tuple(func(y) if i == level else y for i, y in enumerate(x))
for x in index
]
else:
items = [tuple(func(y) for y in x) for x in index]
return MultiIndex.from_tuples(items, names=index.names)
else:
items = [func(x) for x in index]
return Index(items, name=index.name, tupleize_cols=False)


def _fast_count_smallints(arr):
"""Faster version of set(arr) for sequences of small numbers."""
counts = np.bincount(arr.astype(np.int_))
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
from pandas.core.arrays.categorical import _recode_for_categories
import pandas.core.common as com
from pandas.core.frame import _merge_doc
from pandas.core.internals import _transform_index, concatenate_block_managers
from pandas.core.internals import concatenate_block_managers
from pandas.core.sorting import is_int64_overflow_possible

if TYPE_CHECKING:
Expand Down Expand Up @@ -2022,4 +2022,4 @@ def renamer(x, suffix):
lrenamer = partial(renamer, suffix=lsuffix)
rrenamer = partial(renamer, suffix=rsuffix)

return (_transform_index(left, lrenamer), _transform_index(right, rrenamer))
return (left._transform_index(lrenamer), right._transform_index(rrenamer))