Skip to content

Commit 4867415

Browse files
authored
REF: Remove BlockManager.rename_axis (#32349)
1 parent a0972f4 commit 4867415

File tree

5 files changed

+26
-48
lines changed

5 files changed

+26
-48
lines changed

pandas/core/generic.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,6 @@ def rename(
967967
continue
968968

969969
ax = self._get_axis(axis_no)
970-
baxis = self._get_block_manager_axis(axis_no)
971970
f = com.get_rename_function(replacements)
972971

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

987-
result._data = result._data.rename_axis(
988-
f, axis=baxis, copy=copy, level=level
989-
)
986+
new_index = ax._transform_index(f, level)
987+
result.set_axis(new_index, axis=axis_no, inplace=True)
990988
result._clear_item_cache()
991989

992990
if inplace:

pandas/core/indexes/base.py

+21
Original file line numberDiff line numberDiff line change
@@ -4753,6 +4753,27 @@ def map(self, mapper, na_action=None):
47534753

47544754
return Index(new_values, **attributes)
47554755

4756+
# TODO: De-duplicate with map, xref GH#32349
4757+
def _transform_index(self, func, level=None) -> "Index":
4758+
"""
4759+
Apply function to all values found in index.
4760+
4761+
This includes transforming multiindex entries separately.
4762+
Only apply function to one level of the MultiIndex if level is specified.
4763+
"""
4764+
if isinstance(self, ABCMultiIndex):
4765+
if level is not None:
4766+
items = [
4767+
tuple(func(y) if i == level else y for i, y in enumerate(x))
4768+
for x in self
4769+
]
4770+
else:
4771+
items = [tuple(func(y) for y in x) for x in self]
4772+
return type(self).from_tuples(items, names=self.names)
4773+
else:
4774+
items = [func(x) for x in self]
4775+
return Index(items, name=self.name, tupleize_cols=False)
4776+
47564777
def isin(self, values, level=None):
47574778
"""
47584779
Return a boolean array where the index values are in `values`.

pandas/core/internals/__init__.py

-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from pandas.core.internals.managers import (
1818
BlockManager,
1919
SingleBlockManager,
20-
_transform_index,
2120
concatenate_block_managers,
2221
create_block_manager_from_arrays,
2322
create_block_manager_from_blocks,
@@ -40,7 +39,6 @@
4039
"_block_shape",
4140
"BlockManager",
4241
"SingleBlockManager",
43-
"_transform_index",
4442
"concatenate_block_managers",
4543
"create_block_manager_from_arrays",
4644
"create_block_manager_from_blocks",

pandas/core/internals/managers.py

+1-40
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from pandas.core.arrays.sparse import SparseDtype
3535
from pandas.core.base import PandasObject
3636
from pandas.core.indexers import maybe_convert_indices
37-
from pandas.core.indexes.api import Index, MultiIndex, ensure_index
37+
from pandas.core.indexes.api import Index, ensure_index
3838
from pandas.core.internals.blocks import (
3939
Block,
4040
CategoricalBlock,
@@ -226,23 +226,6 @@ def set_axis(self, axis: int, new_labels: Index) -> None:
226226

227227
self.axes[axis] = new_labels
228228

229-
def rename_axis(
230-
self, mapper, axis: int, copy: bool = True, level=None
231-
) -> "BlockManager":
232-
"""
233-
Rename one of axes.
234-
235-
Parameters
236-
----------
237-
mapper : unary callable
238-
axis : int
239-
copy : bool, default True
240-
level : int or None, default None
241-
"""
242-
obj = self.copy(deep=copy)
243-
obj.set_axis(axis, _transform_index(self.axes[axis], mapper, level))
244-
return obj
245-
246229
@property
247230
def _is_single_block(self) -> bool:
248231
if self.ndim == 1:
@@ -1972,28 +1955,6 @@ def _compare_or_regex_search(a, b, regex=False):
19721955
return result
19731956

19741957

1975-
def _transform_index(index, func, level=None):
1976-
"""
1977-
Apply function to all values found in index.
1978-
1979-
This includes transforming multiindex entries separately.
1980-
Only apply function to one level of the MultiIndex if level is specified.
1981-
1982-
"""
1983-
if isinstance(index, MultiIndex):
1984-
if level is not None:
1985-
items = [
1986-
tuple(func(y) if i == level else y for i, y in enumerate(x))
1987-
for x in index
1988-
]
1989-
else:
1990-
items = [tuple(func(y) for y in x) for x in index]
1991-
return MultiIndex.from_tuples(items, names=index.names)
1992-
else:
1993-
items = [func(x) for x in index]
1994-
return Index(items, name=index.name, tupleize_cols=False)
1995-
1996-
19971958
def _fast_count_smallints(arr):
19981959
"""Faster version of set(arr) for sequences of small numbers."""
19991960
counts = np.bincount(arr.astype(np.int_))

pandas/core/reshape/merge.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
from pandas.core.arrays.categorical import _recode_for_categories
4747
import pandas.core.common as com
4848
from pandas.core.frame import _merge_doc
49-
from pandas.core.internals import _transform_index, concatenate_block_managers
49+
from pandas.core.internals import concatenate_block_managers
5050
from pandas.core.sorting import is_int64_overflow_possible
5151

5252
if TYPE_CHECKING:
@@ -1993,4 +1993,4 @@ def renamer(x, suffix):
19931993
lrenamer = partial(renamer, suffix=lsuffix)
19941994
rrenamer = partial(renamer, suffix=rsuffix)
19951995

1996-
return (_transform_index(left, lrenamer), _transform_index(right, rrenamer))
1996+
return (left._transform_index(lrenamer), right._transform_index(rrenamer))

0 commit comments

Comments
 (0)