Skip to content

BUG: (GH4032) Fixed insertion issue into DataFrame, after rename #4043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 26, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
10 changes: 9 additions & 1 deletion pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down