Repository: pandas-dev/pandas

Issue:
DataFrame.to_csv(f, index=False) fails after appending a column to the data frame
In [7]: pd.**version**
Out[7]: '0.12.0.dev-a026561'
In [8]: import pandas as pd
In [9]: df = pd.DataFrame({'c1':[1,2,3], 'c2':[4,5,6]})
In [10]: df.to_csv('f1.txt', index=False)
In [11]: df['c3'] = [7,8,9]
## In [12]: df.to_csv('f2.txt', index=False)

KeyError                                  Traceback (most recent call last)
<ipython-input-12-b4861447b824> in <module>()
----> 1 df.to_csv('f2.txt', index=False)

/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/pandas-0.12.0.dev_a026561-py3.3-macosx-10.6-intel.egg/pandas/core/frame.py in to_csv(self, path_or_buf, sep, na_rep, float_format, cols, header, index, index_label, mode, nanRep, encoding, quoting, line_terminator, chunksize, **kwds)
   1445                                          index_label=index_label,mode=mode,
   1446                                          chunksize=chunksize,engine=kwds.get("engine") )
-> 1447             formatter.save()
   1448 
   1449     def to_excel(self, excel_writer, sheet_name='sheet1', na_rep='',

/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/pandas-0.12.0.dev_a026561-py3.3-macosx-10.6-intel.egg/pandas/core/format.py in save(self)
    945 
    946             else:
--> 947                 self._save()
    948 
    949 

/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/pandas-0.12.0.dev_a026561-py3.3-macosx-10.6-intel.egg/pandas/core/format.py in _save(self)
   1017                 break
   1018 
-> 1019             self._save_chunk(start_i, end_i)
   1020 
   1021     def _save_chunk(self, start_i, end_i):

/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/pandas-0.12.0.dev_a026561-py3.3-macosx-10.6-intel.egg/pandas/core/format.py in _save_chunk(self, start_i, end_i)
   1031 
   1032                 # self.data is a preallocated list
-> 1033                 self.data[self.column_map[b][i]] = d[i]
   1034 
   1035         ix = data_index.to_native_types(slicer=slicer, na_rep=self.na_rep, float_format=self.float_format)

KeyError: IntBlock: [c3], 1 x 3, dtype int64



Discussion:
did you recompile after you cloned master? 

This works for me just fine (on 3.3), on linux

sorry...this is actually a bug, will give you a fix in a min


Resolving patch:
diff --git a/pandas/core/format.py b/pandas/core/format.py
--- a/pandas/core/format.py
+++ b/pandas/core/format.py
@@ -777,6 +777,8 @@ def __init__(self, obj, path_or_buf, sep=",", na_rep='', float_format=None,
                line_terminator='\n', chunksize=None, engine=None):
 
         self.engine = engine  # remove for 0.12
+
+        obj._consolidate_inplace()
         self.obj = obj
 
         self.path_or_buf = path_or_buf


Test patch:
diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py
--- a/pandas/tests/test_frame.py
+++ b/pandas/tests/test_frame.py
@@ -4939,6 +4939,19 @@ def test_to_csv_from_csv_w_all_infs(self):
             assert_frame_equal(self.frame, recons, check_names=False)  # TODO to_csv drops column name
             assert_frame_equal(np.isinf(self.frame), np.isinf(recons), check_names=False)
 
+    def test_to_csv_no_index(self):
+        # GH 3624, after appending columns, to_csv fails
+        pname = '__tmp_to_csv_no_index__'
+        with ensure_clean(pname) as path:
+            df = DataFrame({'c1':[1,2,3], 'c2':[4,5,6]})
+            df.to_csv(path, index=False)
+            result = read_csv(path)
+            assert_frame_equal(df,result)
+            df['c3'] = [7,8,9]
+            df.to_csv(path, index=False)
+            result = read_csv(path)
+            assert_frame_equal(df,result)
+
     def test_to_csv_multiindex(self):
 
         pname = '__tmp_to_csv_multiindex__'