Skip to content

Commit 1aa8a76

Browse files
committed
Merge pull request #4043 from jreback/GH4032
BUG: (GH4032) Fixed insertion issue into DataFrame, after rename
2 parents b5c5442 + 65227c1 commit 1aa8a76

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

doc/source/release.rst

+5-5
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,7 @@ pandas 0.11.1
253253
- Fix running of bs4 tests when it is not installed (:issue:`3605`)
254254
- Fix parsing of html table (:issue:`3606`)
255255
- ``read_html()`` now only allows a single backend: ``html5lib`` (:issue:`3616`)
256-
- ``convert_objects`` with ``convert_dates='coerce'`` was parsing some single-letter strings
257-
into today's date
256+
- ``convert_objects`` with ``convert_dates='coerce'`` was parsing some single-letter strings into today's date
258257
- ``DataFrame.from_records`` did not accept empty recarrays (:issue:`3682`)
259258
- ``DataFrame.to_csv`` will succeed with the deprecated option ``nanRep``, @tdsmith
260259
- ``DataFrame.to_html`` and ``DataFrame.to_latex`` now accept a path for
@@ -277,10 +276,11 @@ pandas 0.11.1
277276
- Indexing with a string with seconds resolution not selecting from a time index (:issue:`3925`)
278277
- csv parsers would loop infinitely if ``iterator=True`` but no ``chunksize`` was
279278
specified (:issue:`3967`), python parser failing with ``chunksize=1``
280-
- Fix index name not propogating when using ``shift``
281-
- Fixed dropna=False being ignored with multi-index stack (:issue:`3997`)
279+
- Fix index name not propogating when using ``shift``
280+
- Fixed dropna=False being ignored with multi-index stack (:issue:`3997`)
282281
- Fixed flattening of columns when renaming MultiIndex columns DataFrame (:issue:`4004`)
283-
- Fix ``Series.clip`` for datetime series. NA/NaN threshold values will now throw ValueError (:issue:`3996`)
282+
- Fix ``Series.clip`` for datetime series. NA/NaN threshold values will now throw ValueError (:issue:`3996`)
283+
- Fixed insertion issue into DataFrame, after rename (:issue:`4032`)
284284

285285
.. _Gh3616: https://github.com/pydata/pandas/issues/3616
286286

pandas/core/internals.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ def set_ref_items(self, ref_items, maybe_rename=True):
8585
"""
8686
if not isinstance(ref_items, Index):
8787
raise AssertionError('block ref_items must be an Index')
88+
if maybe_rename == 'clear':
89+
self._ref_locs = None
8890
if maybe_rename:
8991
self.items = ref_items.take(self.ref_locs)
9092
self.ref_items = ref_items
@@ -1798,12 +1800,18 @@ def insert(self, loc, item, value, allow_duplicates=False):
17981800

17991801
if len(self.blocks) > 100:
18001802
self._consolidate_inplace()
1803+
elif new_items.is_unique:
1804+
self.set_items_clear(new_items)
18011805

18021806
self._known_consolidated = False
18031807

18041808
def set_items_norename(self, value):
18051809
self.set_axis(0, value, maybe_rename=False, check_axis=False)
18061810

1811+
def set_items_clear(self, value):
1812+
""" clear the ref_locs on all blocks """
1813+
self.set_axis(0, value, maybe_rename='clear', check_axis=False)
1814+
18071815
def _delete_from_all_blocks(self, loc, item):
18081816
""" delete from the items loc the item
18091817
the item could be in multiple blocks which could
@@ -1914,7 +1922,7 @@ def _add_new_block(self, item, value, loc=None):
19141922
# and reset
19151923
self._reset_ref_locs()
19161924
self._set_ref_locs(do_refs=True)
1917-
1925+
19181926
def _find_block(self, item):
19191927
self._check_have(item)
19201928
for i, block in enumerate(self.blocks):

pandas/tests/test_frame.py

+15
Original file line numberDiff line numberDiff line change
@@ -10018,6 +10018,21 @@ def test_columns_with_dups(self):
1001810018
xp.columns = ['A', 'A', 'B']
1001910019
assert_frame_equal(rs, xp)
1002010020

10021+
def test_insert_column_bug_4032(self):
10022+
# GH4032, inserting a column and renaming causing errors
10023+
df = DataFrame({'b': [1.1, 2.2]})
10024+
df = df.rename(columns={})
10025+
df.insert(0, 'a', [1, 2])
10026+
10027+
result = df.rename(columns={})
10028+
expected = DataFrame([[1,1.1],[2, 2.2]],columns=['a','b'])
10029+
assert_frame_equal(result,expected)
10030+
df.insert(0, 'c', [1.3, 2.3])
10031+
10032+
result = df.rename(columns={})
10033+
expected = DataFrame([[1.3,1,1.1],[2.3,2, 2.2]],columns=['c','a','b'])
10034+
assert_frame_equal(result,expected)
10035+
1002110036
def test_cast_internals(self):
1002210037
casted = DataFrame(self.frame._data, dtype=int)
1002310038
expected = DataFrame(self.frame._series, dtype=int)

0 commit comments

Comments
 (0)