Skip to content

to_csv headers kwarg now works regardless of index kwarg #6916

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

Merged
merged 2 commits into from
Apr 21, 2014
Merged
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
2 changes: 2 additions & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,8 @@ Bug Fixes
- Bug in arithmetic operations affecting to NaT (:issue:`6873`)
- Bug in ``Series.str.extract`` where the resulting ``Series`` from a single
group match wasn't renamed to the group name
- Bug in ``DataFrame.to_csv`` where setting `index` to `False` ignored the
`header` kwarg (:issue:`6186`)

pandas 0.13.1
-------------
Expand Down
26 changes: 10 additions & 16 deletions pandas/core/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,14 @@ def _save_header(self):
has_aliases = isinstance(header, (tuple, list, np.ndarray))
if not (has_aliases or self.header):
return
if has_aliases:
if len(header) != len(cols):
raise ValueError(('Writing %d cols but got %d aliases'
% (len(cols), len(header))))
else:
write_cols = header
else:
write_cols = cols

if self.index:
# should write something for index label
Expand All @@ -1219,22 +1227,8 @@ def _save_header(self):
else:
encoded_labels = []

if has_aliases:
if len(header) != len(cols):
raise ValueError(('Writing %d cols but got %d aliases'
% (len(cols), len(header))))
else:
write_cols = header
else:
write_cols = cols

if not has_mi_columns:
encoded_labels += list(write_cols)

else:

if not has_mi_columns:
encoded_labels += list(cols)
if not has_mi_columns:
encoded_labels += list(write_cols)

# write out the mi
if has_mi_columns:
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5836,6 +5836,22 @@ def test_to_csv_no_index(self):
result = read_csv(path)
assert_frame_equal(df,result)

def test_to_csv_headers(self):
# GH6186, the presence or absence of `index` incorrectly
# causes to_csv to have different header semantics.
pname = '__tmp_to_csv_headers__'
from_df = DataFrame([[1, 2], [3, 4]], columns=['A', 'B'])
to_df = DataFrame([[1, 2], [3, 4]], columns=['X', 'Y'])
with ensure_clean(pname) as path:
from_df.to_csv(path, header=['X', 'Y'])
recons = DataFrame.from_csv(path)
assert_frame_equal(to_df, recons)

from_df.to_csv(path, index=False, header=['X', 'Y'])
recons = DataFrame.from_csv(path)
recons.reset_index(inplace=True)
assert_frame_equal(to_df, recons)

def test_to_csv_multiindex(self):

pname = '__tmp_to_csv_multiindex__'
Expand Down