diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index ee772514f678c4..0cd935e64364ee 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -259,7 +259,7 @@ def __new__( result._set_levels(levels, copy=copy, validate=False) result._set_codes(codes, copy=copy, validate=False) - result._names = [None for _ in levels] + result._names = [None] * len(levels) if names is not None: # handles name validation result._set_names(names) diff --git a/pandas/core/reshape/reshape.py b/pandas/core/reshape/reshape.py index 9f6155822bee1c..4da7e7e58d4b2b 100644 --- a/pandas/core/reshape/reshape.py +++ b/pandas/core/reshape/reshape.py @@ -260,13 +260,10 @@ def get_new_values(self): def get_new_columns(self): if self.value_columns is None: if self.lift == 0: - lev = self.removed_level._shallow_copy() - lev.name = self.removed_name - return lev + return self.removed_level._shallow_copy(name=self.removed_name) lev = self.removed_level.insert(0, item=self.removed_level._na_value) - lev.name = self.removed_name - return lev + return lev.rename(self.removed_name) stride = len(self.removed_level) + self.lift width = len(self.value_columns) @@ -302,12 +299,10 @@ def get_new_index(self): # construct the new index if len(self.new_index_levels) == 1: - lev, lab = self.new_index_levels[0], result_codes[0] - if (lab == -1).any(): - lev = lev.insert(len(lev), lev._na_value) - new_index = lev.take(lab) - new_index.name = self.new_index_names[0] - return new_index + level, level_codes = self.new_index_levels[0], result_codes[0] + if (level_codes == -1).any(): + level = level.insert(len(level), level._na_value) + return level.take(level_codes).rename(self.new_index_names[0]) return MultiIndex( levels=self.new_index_levels, @@ -663,8 +658,7 @@ def _convert_level_number(level_num, columns): new_names = this.columns.names[:-1] new_columns = MultiIndex.from_tuples(unique_groups, names=new_names) else: - new_columns = this.columns.levels[0]._shallow_copy() - new_columns.name = this.columns.names[0] + new_columns = this.columns.levels[0]._shallow_copy(name=this.columns.names[0]) unique_groups = new_columns # time to ravel the values diff --git a/pandas/tests/frame/test_alter_axes.py b/pandas/tests/frame/test_alter_axes.py index 869418bee8e155..3b1b9d1f6be363 100644 --- a/pandas/tests/frame/test_alter_axes.py +++ b/pandas/tests/frame/test_alter_axes.py @@ -980,6 +980,7 @@ def test_reset_index(self, float_frame): values = lev.take(level_codes) name = names[i] tm.assert_index_equal(values, Index(deleveled[name]), check_names=False) + assert values.name is None stacked.index.names = [None, None] deleveled2 = stacked.reset_index()