From d2f96c918abd0297b203e2bc9e52c4f7632fcabb Mon Sep 17 00:00:00 2001 From: Varun Date: Thu, 19 Nov 2015 15:00:39 -0500 Subject: [PATCH] Bug#11637 fix to_csv incorrect output. Added test case test_to_csv_with_mix_columns --- doc/source/whatsnew/v0.17.1.txt | 1 + pandas/core/format.py | 4 ++-- pandas/tests/test_frame.py | 10 ++++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.17.1.txt b/doc/source/whatsnew/v0.17.1.txt index c4e8ae44011ec..6061fd42ebac2 100755 --- a/doc/source/whatsnew/v0.17.1.txt +++ b/doc/source/whatsnew/v0.17.1.txt @@ -164,6 +164,7 @@ Bug Fixes - Bug in ``squeeze()`` with zero length arrays (:issue:`11230`, :issue:`8999`) - Bug in ``describe()`` dropping column names for hierarchical indexes (:issue:`11517`) - Bug in ``DataFrame.pct_change()`` not propagating ``axis`` keyword on ``.fillna`` method (:issue:`11150`) +- Bug in ``.to_csv()`` incorrect output when a mix of integer and string column names passed as columns parameter (:issue:`11637`) diff --git a/pandas/core/format.py b/pandas/core/format.py index efa4b182f1133..8cc30a3444e9f 100644 --- a/pandas/core/format.py +++ b/pandas/core/format.py @@ -1327,7 +1327,7 @@ def __init__(self, obj, path_or_buf=None, sep=",", na_rep='', float_format=None, date_format=date_format, quoting=self.quoting) else: - cols = np.asarray(list(cols)) + cols = list(cols) self.obj = self.obj.loc[:, cols] # update columns to include possible multiplicity of dupes @@ -1339,7 +1339,7 @@ def __init__(self, obj, path_or_buf=None, sep=",", na_rep='', float_format=None, date_format=date_format, quoting=self.quoting) else: - cols = np.asarray(list(cols)) + cols = list(cols) # save it self.cols = cols diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index 79001cf4be9bc..cb3c779c3167e 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -39,6 +39,7 @@ from pandas.util.misc import is_little_endian from pandas.util.testing import (assert_almost_equal, + assert_equal, assert_numpy_array_equal, assert_series_equal, assert_frame_equal, @@ -6987,6 +6988,15 @@ def test_to_csv_no_index(self): result = read_csv(path) assert_frame_equal(df,result) + def test_to_csv_with_mix_columns(self): + #GH11637, incorrect output when a mix of integer and string column + # names passed as columns parameter in to_csv + + df = DataFrame({0: ['a', 'b', 'c'], + 1: ['aa', 'bb', 'cc']}) + df['test'] = 'txt' + assert_equal(df.to_csv(), df.to_csv(columns=[0, 1, 'test'])) + def test_to_csv_headers(self): # GH6186, the presence or absence of `index` incorrectly # causes to_csv to have different header semantics.