Skip to content

BUG: fix for index name lost #9857 and also added the corresponding test in test_index.py #9876

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

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 3 additions & 1 deletion pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,9 @@ def append(self, other):
to_concat.append(other)

for obj in to_concat:
if isinstance(obj, Index) and obj.name != name:
if (isinstance(obj, Index) and
obj.name != name and
obj.name is not None):
name = None
break

Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -4075,6 +4075,19 @@ def test_groupby(self):
exp = dict((key, [key]) for key in self.index)
tm.assert_dict_equal(groups, exp)

def test_index_name_retained(self):
# GH9857
result = pd.DataFrame({'x': [1, 2, 6],
'y': [2, 2, 8],
'z': [-5, 0, 5]})
result = result.set_index('z')
result.loc[10] = [9, 10]
df_expected = pd.DataFrame({'x': [1, 2, 6, 9],
'y': [2, 2, 8, 10],
'z': [-5, 0, 5, 10]})
df_expected = df_expected.set_index('z')
tm.assert_frame_equal(result, df_expected)


def test_get_combined_index():
from pandas.core.index import _get_combined_index
Expand Down