From 6dfd2bcfe6b3902c8d3774b4d0b3b07f0d611c71 Mon Sep 17 00:00:00 2001 From: John Liekezer Date: Mon, 15 Aug 2016 18:09:00 +0300 Subject: [PATCH] BUG: Copy index(GH 13522) --- doc/source/whatsnew/v0.19.0.txt | 1 + pandas/core/frame.py | 2 ++ pandas/tests/frame/test_mutate_columns.py | 7 +++++++ 3 files changed, 10 insertions(+) diff --git a/doc/source/whatsnew/v0.19.0.txt b/doc/source/whatsnew/v0.19.0.txt index 3600b8f52873b..e82332cd1d3b5 100644 --- a/doc/source/whatsnew/v0.19.0.txt +++ b/doc/source/whatsnew/v0.19.0.txt @@ -861,6 +861,7 @@ Bug Fixes - Bug in ``io.json.json_normalize()``, where non-ascii keys raised an exception (:issue:`13213`) - Bug when passing a not-default-indexed ``Series`` as ``xerr`` or ``yerr`` in ``.plot()`` (:issue:`11858`) - Bug in area plot draws legend incorrectly if subplot is enabled or legend is moved after plot (matplotlib 1.5.0 is required to draw area plot legend properly) (issue:`9161`, :issue:`13544`) +- Bug in ``DataFrame`` assignment with an object-dtyped ``Index`` where the resultant column is mutable to the original object. (:issue:`13522`) - Bug in matplotlib ``AutoDataFormatter``; this restores the second scaled formatting and re-adds micro-second scaled formatting (:issue:`13131`) - Bug in selection from a ``HDFStore`` with a fixed format and ``start`` and/or ``stop`` specified will now return the selected range (:issue:`8287`) - Bug in ``Series`` construction from a tuple of integers on windows not returning default dtype (int64) (:issue:`13646`) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index ea83200465582..5db755b0d3dac 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -2638,6 +2638,8 @@ def reindexer(value): value = com._asarray_tuplesafe(value) elif value.ndim == 2: value = value.copy().T + elif isinstance(value, Index): + value = value.copy(deep=True) else: value = value.copy() diff --git a/pandas/tests/frame/test_mutate_columns.py b/pandas/tests/frame/test_mutate_columns.py index 2bdd6657eaf18..5beab1565e538 100644 --- a/pandas/tests/frame/test_mutate_columns.py +++ b/pandas/tests/frame/test_mutate_columns.py @@ -156,6 +156,13 @@ def test_insert(self): df.insert(0, 'baz', df['c']) self.assertEqual(df.columns.name, 'some_name') + # GH 13522 + df = DataFrame(index=['A', 'B', 'C']) + df['X'] = df.index + df['X'] = ['x', 'y', 'z'] + exp = DataFrame(data={'X': ['x', 'y', 'z']}, index=['A', 'B', 'C']) + assert_frame_equal(df, exp) + def test_delitem(self): del self.frame['A'] self.assertNotIn('A', self.frame)