Skip to content

Commit 79b2610

Browse files
authored
BUG/PERF: Series(index=MultiIndex).rename losing EA dtypes (#50930)
BUG/PERF: Series(index=MultiIndex).rename
1 parent 0cee41f commit 79b2610

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

doc/source/whatsnew/v2.0.0.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,7 @@ Performance improvements
900900
- Performance improvements to :func:`read_sas` (:issue:`47403`, :issue:`47405`, :issue:`47656`, :issue:`48502`)
901901
- Memory improvement in :meth:`RangeIndex.sort_values` (:issue:`48801`)
902902
- Performance improvement in :meth:`Series.to_numpy` if ``copy=True`` by avoiding copying twice (:issue:`24345`)
903+
- Performance improvement in :meth:`Series.rename` with :class:`MultiIndex` (:issue:`21055`)
903904
- Performance improvement in :class:`DataFrameGroupBy` and :class:`SeriesGroupBy` when ``by`` is a categorical type and ``sort=False`` (:issue:`48976`)
904905
- Performance improvement in :class:`DataFrameGroupBy` and :class:`SeriesGroupBy` when ``by`` is a categorical type and ``observed=False`` (:issue:`49596`)
905906
- Performance improvement in :func:`read_stata` with parameter ``index_col`` set to ``None`` (the default). Now the index will be a :class:`RangeIndex` instead of :class:`Int64Index` (:issue:`49745`)
@@ -1020,6 +1021,7 @@ Indexing
10201021
- Bug in :meth:`DataFrame.iloc` raising ``IndexError`` when indexer is a :class:`Series` with numeric extension array dtype (:issue:`49521`)
10211022
- Bug in :func:`~DataFrame.describe` when formatting percentiles in the resulting index showed more decimals than needed (:issue:`46362`)
10221023
- Bug in :meth:`DataFrame.compare` does not recognize differences when comparing ``NA`` with value in nullable dtypes (:issue:`48939`)
1024+
- Bug in :meth:`Series.rename` with :class:`MultiIndex` losing extension array dtypes (:issue:`21055`)
10231025
- Bug in :meth:`DataFrame.isetitem` coercing extension array dtypes in :class:`DataFrame` to object (:issue:`49922`)
10241026
- Bug in :class:`BusinessHour` would cause creation of :class:`DatetimeIndex` to fail when no opening hour was included in the index (:issue:`49835`)
10251027
-

pandas/core/indexes/base.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6042,15 +6042,13 @@ def _transform_index(self, func, *, level=None) -> Index:
60426042
Only apply function to one level of the MultiIndex if level is specified.
60436043
"""
60446044
if isinstance(self, ABCMultiIndex):
6045-
if level is not None:
6046-
# Caller is responsible for ensuring level is positional.
6047-
items = [
6048-
tuple(func(y) if i == level else y for i, y in enumerate(x))
6049-
for x in self
6050-
]
6051-
else:
6052-
items = [tuple(func(y) for y in x) for x in self]
6053-
return type(self).from_tuples(items, names=self.names)
6045+
values = [
6046+
self.get_level_values(i).map(func)
6047+
if i == level or level is None
6048+
else self.get_level_values(i)
6049+
for i in range(self.nlevels)
6050+
]
6051+
return type(self).from_arrays(values)
60546052
else:
60556053
items = [func(x) for x in self]
60566054
return Index(items, name=self.name, tupleize_cols=False)

pandas/tests/series/methods/test_rename.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,25 @@ def test_rename_series_with_multiindex(self):
136136

137137
tm.assert_series_equal(result, series_expected)
138138

139+
def test_rename_series_with_multiindex_keeps_ea_dtypes(self):
140+
# GH21055
141+
arrays = [
142+
Index([1, 2, 3], dtype="Int64").astype("category"),
143+
Index([1, 2, 3], dtype="Int64"),
144+
]
145+
mi = MultiIndex.from_arrays(arrays, names=["A", "B"])
146+
ser = Series(1, index=mi)
147+
result = ser.rename({1: 4}, level=1)
148+
149+
arrays_expected = [
150+
Index([1, 2, 3], dtype="Int64").astype("category"),
151+
Index([4, 2, 3], dtype="Int64"),
152+
]
153+
mi_expected = MultiIndex.from_arrays(arrays_expected, names=["A", "B"])
154+
expected = Series(1, index=mi_expected)
155+
156+
tm.assert_series_equal(result, expected)
157+
139158
def test_rename_error_arg(self):
140159
# GH 46889
141160
ser = Series(["foo", "bar"])

0 commit comments

Comments
 (0)