Skip to content

Bug#11637 fix to_csv #11649

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.17.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`)



Expand Down
4 changes: 2 additions & 2 deletions pandas/core/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down