Skip to content

Commit f8ca3b7

Browse files
ksanghaijreback
authored andcommitted
added the fix for index name lost #9862 and also added the corresponding test in
test_index
1 parent c44eec0 commit f8ca3b7

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

doc/source/whatsnew/v0.16.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ Bug Fixes
9595
- Fixed bug (:issue:`9542`) where labels did not appear properly in legend of ``DataFrame.plot()``. Passing ``label=`` args also now works, and series indices are no longer mutated.
9696
- Bug in json serialization when frame has length zero.(:issue:`9805`)
9797
- Bug in `read_csv` where missing trailing delimiters would cause segfault. (:issue:`5664`)
98+
- Bug in retaining index name on appending (:issue:`9862`)
9899

99100

100101
- Bug in ``scatter_matrix`` draws unexpected axis ticklabels (:issue:`5662`)

pandas/core/index.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,9 @@ def append(self, other):
976976
to_concat.append(other)
977977

978978
for obj in to_concat:
979-
if isinstance(obj, Index) and obj.name != name:
979+
if (isinstance(obj, Index) and
980+
obj.name != name and
981+
obj.name is not None):
980982
name = None
981983
break
982984

pandas/tests/test_index.py

+13
Original file line numberDiff line numberDiff line change
@@ -4075,6 +4075,19 @@ def test_groupby(self):
40754075
exp = dict((key, [key]) for key in self.index)
40764076
tm.assert_dict_equal(groups, exp)
40774077

4078+
def test_index_name_retained(self):
4079+
# GH9857
4080+
result = pd.DataFrame({'x': [1, 2, 6],
4081+
'y': [2, 2, 8],
4082+
'z': [-5, 0, 5]})
4083+
result = result.set_index('z')
4084+
result.loc[10] = [9, 10]
4085+
df_expected = pd.DataFrame({'x': [1, 2, 6, 9],
4086+
'y': [2, 2, 8, 10],
4087+
'z': [-5, 0, 5, 10]})
4088+
df_expected = df_expected.set_index('z')
4089+
tm.assert_frame_equal(result, df_expected)
4090+
40784091

40794092
def test_get_combined_index():
40804093
from pandas.core.index import _get_combined_index

0 commit comments

Comments
 (0)