Skip to content

Commit 95a5326

Browse files
committed
Merge pull request #3018 from jreback/insert_3010
BUG: Bug in DataFrame column insertion when the column creation fails (GH3010)
2 parents 156e03c + bd2ffc1 commit 95a5326

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
lines changed

RELEASE.rst

+3
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ pandas 0.11.0
141141
- Fixed printing of ``NaT` in an index
142142
- Bug in idxmin/idxmax of ``datetime64[ns]`` Series with ``NaT`` (GH2982__)
143143
- Bug in ``icol`` with negative indicies was incorrect producing incorrect return values (see GH2922_)
144+
- Bug in DataFrame column insertion when the column creation fails, existing frame is left in
145+
an irrecoverable state (GH3010_)
144146

145147
.. _GH622: https://github.com/pydata/pandas/issues/622
146148
.. _GH797: https://github.com/pydata/pandas/issues/797
@@ -166,6 +168,7 @@ pandas 0.11.0
166168
.. _GH2982: https://github.com/pydata/pandas/issues/2982
167169
.. _GH2989: https://github.com/pydata/pandas/issues/2989
168170
.. _GH3002: https://github.com/pydata/pandas/issues/3002
171+
.. _GH3010: https://github.com/pydata/pandas/issues/3010
169172
.. _GH3012: https://github.com/pydata/pandas/issues/3012
170173

171174

doc/source/io.rst

+9-7
Original file line numberDiff line numberDiff line change
@@ -1138,11 +1138,14 @@ defaults to `nan`.
11381138

11391139
.. ipython:: python
11401140
1141-
df_mixed = df.copy()
1142-
df_mixed['string'] = 'string'
1143-
df_mixed['int'] = 1
1144-
df_mixed['bool'] = True
1145-
df_mixed['datetime64'] = Timestamp('20010102')
1141+
df_mixed = DataFrame({ 'A' : randn(8),
1142+
'B' : randn(8),
1143+
'C' : np.array(randn(8),dtype='float32'),
1144+
'string' :'string',
1145+
'int' : 1,
1146+
'bool' : True,
1147+
'datetime64' : Timestamp('20010102')},
1148+
index=range(8))
11461149
df_mixed.ix[3:5,['A', 'B', 'string', 'datetime64']] = np.nan
11471150
11481151
store.append('df_mixed', df_mixed, min_itemsize = {'values': 50})
@@ -1445,8 +1448,7 @@ may not be installed (by Python) by default.
14451448

14461449
Compression for all objects within the file
14471450

1448-
- ``store_compressed = HDFStore('store_compressed.h5', complevel=9,
1449-
complib='blosc')``
1451+
- ``store_compressed = HDFStore('store_compressed.h5', complevel=9, complib='blosc')``
14501452

14511453
Or on-the-fly compression (this only applies to tables). You can turn
14521454
off file compression for a specific table by passing ``complevel=0``

pandas/core/internals.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -1334,11 +1334,22 @@ def insert(self, loc, item, value):
13341334
if item in self.items:
13351335
raise Exception('cannot insert %s, already exists' % item)
13361336

1337-
new_items = self.items.insert(loc, item)
1338-
self.set_items_norename(new_items)
1337+
try:
1338+
new_items = self.items.insert(loc, item)
1339+
self.set_items_norename(new_items)
1340+
1341+
# new block
1342+
self._add_new_block(item, value, loc=loc)
13391343

1340-
# new block
1341-
self._add_new_block(item, value, loc=loc)
1344+
except:
1345+
1346+
# so our insertion operation failed, so back out of the new items
1347+
# GH 3010
1348+
new_items = self.items.delete(loc)
1349+
self.set_items_norename(new_items)
1350+
1351+
# re-raise
1352+
raise
13421353

13431354
if len(self.blocks) > 100:
13441355
self._consolidate_inplace()

pandas/tests/test_frame.py

+13
Original file line numberDiff line numberDiff line change
@@ -1968,6 +1968,19 @@ def test_constructor_cast_failure(self):
19681968
foo = DataFrame({'a': ['a', 'b', 'c']}, dtype=np.float64)
19691969
self.assert_(foo['a'].dtype == object)
19701970

1971+
# GH 3010, constructing with odd arrays
1972+
df = DataFrame(np.ones((4,2)))
1973+
1974+
# this is ok
1975+
df['foo'] = np.ones((4,2)).tolist()
1976+
1977+
# this is not ok
1978+
self.assertRaises(AssertionError, df.__setitem__, tuple(['test']), np.ones((4,2)))
1979+
1980+
# this is ok
1981+
df['foo2'] = np.ones((4,2)).tolist()
1982+
1983+
19711984
def test_constructor_dtype_nocast_view(self):
19721985
df = DataFrame([[1, 2]])
19731986
should_be_view = DataFrame(df, dtype=df[0].dtype)

0 commit comments

Comments
 (0)