diff --git a/doc/source/release.rst b/doc/source/release.rst index 7afe53cf33904..80061b6f49aed 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -252,8 +252,7 @@ pandas 0.11.1 - Fix running of bs4 tests when it is not installed (:issue:`3605`) - Fix parsing of html table (:issue:`3606`) - ``read_html()`` now only allows a single backend: ``html5lib`` (:issue:`3616`) - - ``convert_objects`` with ``convert_dates='coerce'`` was parsing some single-letter strings - into today's date + - ``convert_objects`` with ``convert_dates='coerce'`` was parsing some single-letter strings into today's date - ``DataFrame.from_records`` did not accept empty recarrays (:issue:`3682`) - ``DataFrame.to_csv`` will succeed with the deprecated option ``nanRep``, @tdsmith - ``DataFrame.to_html`` and ``DataFrame.to_latex`` now accept a path for @@ -276,10 +275,11 @@ pandas 0.11.1 - Indexing with a string with seconds resolution not selecting from a time index (:issue:`3925`) - csv parsers would loop infinitely if ``iterator=True`` but no ``chunksize`` was specified (:issue:`3967`), python parser failing with ``chunksize=1`` - - Fix index name not propogating when using ``shift`` - - Fixed dropna=False being ignored with multi-index stack (:issue:`3997`) + - Fix index name not propogating when using ``shift`` + - Fixed dropna=False being ignored with multi-index stack (:issue:`3997`) - Fixed flattening of columns when renaming MultiIndex columns DataFrame (:issue:`4004`) - - Fix ``Series.clip`` for datetime series. NA/NaN threshold values will now throw ValueError (:issue:`3996`) + - Fix ``Series.clip`` for datetime series. NA/NaN threshold values will now throw ValueError (:issue:`3996`) + - Fixed insertion issue into DataFrame, after rename (:issue:`4032`) .. _Gh3616: https://github.com/pydata/pandas/issues/3616 diff --git a/pandas/core/internals.py b/pandas/core/internals.py index 568971abc1066..0fbadafeca617 100644 --- a/pandas/core/internals.py +++ b/pandas/core/internals.py @@ -85,6 +85,8 @@ def set_ref_items(self, ref_items, maybe_rename=True): """ if not isinstance(ref_items, Index): raise AssertionError('block ref_items must be an Index') + if maybe_rename == 'clear': + self._ref_locs = None if maybe_rename: self.items = ref_items.take(self.ref_locs) self.ref_items = ref_items @@ -1798,12 +1800,18 @@ def insert(self, loc, item, value, allow_duplicates=False): if len(self.blocks) > 100: self._consolidate_inplace() + elif new_items.is_unique: + self.set_items_clear(new_items) self._known_consolidated = False def set_items_norename(self, value): self.set_axis(0, value, maybe_rename=False, check_axis=False) + def set_items_clear(self, value): + """ clear the ref_locs on all blocks """ + self.set_axis(0, value, maybe_rename='clear', check_axis=False) + def _delete_from_all_blocks(self, loc, item): """ delete from the items loc the item the item could be in multiple blocks which could @@ -1914,7 +1922,7 @@ def _add_new_block(self, item, value, loc=None): # and reset self._reset_ref_locs() self._set_ref_locs(do_refs=True) - + def _find_block(self, item): self._check_have(item) for i, block in enumerate(self.blocks): diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index ef4791aa0968c..872584f9b14ed 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -10018,6 +10018,21 @@ def test_columns_with_dups(self): xp.columns = ['A', 'A', 'B'] assert_frame_equal(rs, xp) + def test_insert_column_bug_4032(self): + # GH4032, inserting a column and renaming causing errors + df = DataFrame({'b': [1.1, 2.2]}) + df = df.rename(columns={}) + df.insert(0, 'a', [1, 2]) + + result = df.rename(columns={}) + expected = DataFrame([[1,1.1],[2, 2.2]],columns=['a','b']) + assert_frame_equal(result,expected) + df.insert(0, 'c', [1.3, 2.3]) + + result = df.rename(columns={}) + expected = DataFrame([[1.3,1,1.1],[2.3,2, 2.2]],columns=['c','a','b']) + assert_frame_equal(result,expected) + def test_cast_internals(self): casted = DataFrame(self.frame._data, dtype=int) expected = DataFrame(self.frame._series, dtype=int)