Skip to content

Commit d2f96c9

Browse files
Bug#11637 fix to_csv incorrect output.
Added test case test_to_csv_with_mix_columns
1 parent 12d9938 commit d2f96c9

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

doc/source/whatsnew/v0.17.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ Bug Fixes
164164
- Bug in ``squeeze()`` with zero length arrays (:issue:`11230`, :issue:`8999`)
165165
- Bug in ``describe()`` dropping column names for hierarchical indexes (:issue:`11517`)
166166
- Bug in ``DataFrame.pct_change()`` not propagating ``axis`` keyword on ``.fillna`` method (:issue:`11150`)
167+
- Bug in ``.to_csv()`` incorrect output when a mix of integer and string column names passed as columns parameter (:issue:`11637`)
167168

168169

169170

pandas/core/format.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1327,7 +1327,7 @@ def __init__(self, obj, path_or_buf=None, sep=",", na_rep='', float_format=None,
13271327
date_format=date_format,
13281328
quoting=self.quoting)
13291329
else:
1330-
cols = np.asarray(list(cols))
1330+
cols = list(cols)
13311331
self.obj = self.obj.loc[:, cols]
13321332

13331333
# 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,
13391339
date_format=date_format,
13401340
quoting=self.quoting)
13411341
else:
1342-
cols = np.asarray(list(cols))
1342+
cols = list(cols)
13431343

13441344
# save it
13451345
self.cols = cols

pandas/tests/test_frame.py

+10
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
from pandas.util.misc import is_little_endian
4040

4141
from pandas.util.testing import (assert_almost_equal,
42+
assert_equal,
4243
assert_numpy_array_equal,
4344
assert_series_equal,
4445
assert_frame_equal,
@@ -6987,6 +6988,15 @@ def test_to_csv_no_index(self):
69876988
result = read_csv(path)
69886989
assert_frame_equal(df,result)
69896990

6991+
def test_to_csv_with_mix_columns(self):
6992+
#GH11637, incorrect output when a mix of integer and string column
6993+
# names passed as columns parameter in to_csv
6994+
6995+
df = DataFrame({0: ['a', 'b', 'c'],
6996+
1: ['aa', 'bb', 'cc']})
6997+
df['test'] = 'txt'
6998+
assert_equal(df.to_csv(), df.to_csv(columns=[0, 1, 'test']))
6999+
69907000
def test_to_csv_headers(self):
69917001
# GH6186, the presence or absence of `index` incorrectly
69927002
# causes to_csv to have different header semantics.

0 commit comments

Comments
 (0)