From 391f494d1c5f6770e5f729abbecd018408935fdc Mon Sep 17 00:00:00 2001 From: James Hiebert Date: Mon, 13 Apr 2015 15:59:52 -0400 Subject: [PATCH] Ensured that the index gets retained on Frame.apped(). Fixes #9857 --- doc/source/whatsnew/v0.16.1.txt | 2 ++ pandas/core/frame.py | 1 + pandas/tests/test_index.py | 9 +++++++++ 3 files changed, 12 insertions(+) diff --git a/doc/source/whatsnew/v0.16.1.txt b/doc/source/whatsnew/v0.16.1.txt index 456a3f51fc4ad..938d588436d75 100644 --- a/doc/source/whatsnew/v0.16.1.txt +++ b/doc/source/whatsnew/v0.16.1.txt @@ -126,3 +126,5 @@ Bug Fixes - 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`) - 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`) + +- Bug in ``Frame.append()`` where the index could get dropped when adding a new row. (:issue:`9857`) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 8b683ad89558a..9f148559af0cc 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -3928,6 +3928,7 @@ def append(self, other, ignore_index=False, verify_integrity=False): other = other.reindex(combined_columns, copy=False) other = DataFrame(other.values.reshape((1, len(other))), index=index, columns=combined_columns).convert_objects() + other.index = other.index.rename(self.index.name) if not self.columns.equals(combined_columns): self = self.reindex(columns=combined_columns) elif isinstance(other, list) and not isinstance(other[0], DataFrame): diff --git a/pandas/tests/test_index.py b/pandas/tests/test_index.py index bb75b12754dca..b6860eab71742 100644 --- a/pandas/tests/test_index.py +++ b/pandas/tests/test_index.py @@ -4081,6 +4081,15 @@ def test_get_combined_index(): result = _get_combined_index([]) assert(result.equals(Index([]))) +def test_new_rows_retains_index(): + #GH 9857 + df = pd.DataFrame({'x': [1,2,6], 'y': [2,2,8], 'z':[-5,0,5]}) + df = df.set_index('z') + assert(df.index.name == 'z') + df.loc[5] = {'x': 9, 'y': 99} + df.loc[10] = {'x': 7, 'y': 77} + assert(df.index.name == 'z') + if __name__ == '__main__':