Skip to content

Commit 391f494

Browse files
committed
Ensured that the index gets retained on Frame.apped(). Fixes pandas-dev#9857
1 parent 35b20d8 commit 391f494

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

doc/source/whatsnew/v0.16.1.txt

+2
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,5 @@ Bug Fixes
126126
- Bug in unequal comparisons between a ``Series`` of dtype `"category"` and a scalar (e.g. ``Series(Categorical(list("abc"), categories=list("cba"), ordered=True)) > "b"``, which wouldn't use the order of the categories but use the lexicographical order. (:issue:`9848`)
127127

128128
- Bug in unequal comparisons between categorical data and a scalar, which was not in the categories (e.g. ``Series(Categorical(list("abc"), ordered=True)) > "d"``. This returned ``False`` for all elements, but now raises a ``TypeError``. Equality comparisons also now return ``False`` for ``==`` and ``True`` for ``!=``. (:issue:`9848`)
129+
130+
- Bug in ``Frame.append()`` where the index could get dropped when adding a new row. (:issue:`9857`)

pandas/core/frame.py

+1
Original file line numberDiff line numberDiff line change
@@ -3928,6 +3928,7 @@ def append(self, other, ignore_index=False, verify_integrity=False):
39283928
other = other.reindex(combined_columns, copy=False)
39293929
other = DataFrame(other.values.reshape((1, len(other))),
39303930
index=index, columns=combined_columns).convert_objects()
3931+
other.index = other.index.rename(self.index.name)
39313932
if not self.columns.equals(combined_columns):
39323933
self = self.reindex(columns=combined_columns)
39333934
elif isinstance(other, list) and not isinstance(other[0], DataFrame):

pandas/tests/test_index.py

+9
Original file line numberDiff line numberDiff line change
@@ -4081,6 +4081,15 @@ def test_get_combined_index():
40814081
result = _get_combined_index([])
40824082
assert(result.equals(Index([])))
40834083

4084+
def test_new_rows_retains_index():
4085+
#GH 9857
4086+
df = pd.DataFrame({'x': [1,2,6], 'y': [2,2,8], 'z':[-5,0,5]})
4087+
df = df.set_index('z')
4088+
assert(df.index.name == 'z')
4089+
df.loc[5] = {'x': 9, 'y': 99}
4090+
df.loc[10] = {'x': 7, 'y': 77}
4091+
assert(df.index.name == 'z')
4092+
40844093

40854094

40864095
if __name__ == '__main__':

0 commit comments

Comments
 (0)