Skip to content
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
5 changes: 3 additions & 2 deletions pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -1788,6 +1788,9 @@ def insert(self, loc, item, value, allow_duplicates=False):
# new block
self._add_new_block(item, value, loc=loc)

if loc != len(self.items)-1 and new_items.is_unique:
self.set_items_clear(new_items)

except:

# so our insertion operation failed, so back out of the new items
Expand All @@ -1800,8 +1803,6 @@ 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

Expand Down
4 changes: 4 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -10040,11 +10040,15 @@ def test_insert_column_bug_4032(self):
df.insert(0, 'a', [1, 2])

result = df.rename(columns={})
str(result)

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={})
str(result)

expected = DataFrame([[1.3,1,1.1],[2.3,2, 2.2]],columns=['c','a','b'])
assert_frame_equal(result,expected)

Expand Down
15 changes: 13 additions & 2 deletions vb_suite/frame_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,19 @@ def f(K=500):
df[i] = new_col
"""

frame_insert_500_columns = Benchmark('f()', setup,
start_date=datetime(2011, 1, 1))
frame_insert_500_columns_end = Benchmark('f()', setup, start_date=datetime(2011, 1, 1))

setup = common_setup + """
N = 1000

def f(K=100):
df = DataFrame(index=range(N))
new_col = np.random.randn(N)
for i in range(K):
df.insert(0,i,new_col)
"""

frame_insert_100_columns_begin = Benchmark('f()', setup, start_date=datetime(2011, 1, 1))

#----------------------------------------------------------------------
# strings methods, #2602
Expand Down