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 2 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
7 changes: 3 additions & 4 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
MultiIndex,
RangeIndex,
ensure_index,
transform_index,
)
from pandas.core.indexes.datetimes import DatetimeIndex
from pandas.core.indexes.period import Period, PeriodIndex
Expand Down Expand Up @@ -968,7 +969,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 @@ -985,9 +985,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 = transform_index(ax, 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/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,24 @@ def all_indexes_same(indexes):
if not first.equals(index):
return False
return True


def transform_index(index: Index, func, level=None) -> Index:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how is this different from .map?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the main difference is in the MultiIndex case passing level makes this operate on just that levle

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

k, it would be better to use map then than duplicating this code

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or at least co-locate as a private function for future de-duplication

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

"""
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)
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 @@
import pandas.core.algorithms as algos
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 @@ -184,23 +184,6 @@ def set_axis(self, axis: int, new_labels: Index):

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 @@ -1897,28 +1880,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
5 changes: 3 additions & 2 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
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.indexes.api import transform_index
from pandas.core.internals import concatenate_block_managers
from pandas.core.sorting import is_int64_overflow_possible

if TYPE_CHECKING:
Expand Down Expand Up @@ -2028,4 +2029,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 (transform_index(left, lrenamer), transform_index(right, rrenamer))