Skip to content

Commit

Permalink
overload header keyword instead of extra col_aliases keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
Chang She authored and wesm committed Jun 25, 2012
1 parent 791e704 commit 7923fa5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
40 changes: 20 additions & 20 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1025,15 +1025,16 @@ def to_panel(self):
to_wide = deprecate('to_wide', to_panel)

def _helper_csvexcel(self, writer, na_rep=None, cols=None,
col_aliases=None, header=True,
index=True, index_label=None):
header=True, index=True, index_label=None):
if cols is None:
cols = self.columns

series = {}
for k, v in self._series.iteritems():
series[k] = v.values
if header:

has_aliases = isinstance(header, (tuple, list, np.ndarray))
if has_aliases or header:
if index:
# should write something for index label
if index_label is None:
Expand All @@ -1054,12 +1055,12 @@ def _helper_csvexcel(self, writer, na_rep=None, cols=None,
index_label = [index_label]

encoded_labels = list(index_label)
if col_aliases is not None:
if len(col_aliases) != len(cols):
if has_aliases:
if len(header) != len(cols):
raise ValueError(('Writing %d cols but got %d aliases'
% (len(cols), len(col_aliases))))
% (len(cols), len(header))))
else:
write_cols = col_aliases
write_cols = header
else:
write_cols = cols
encoded_cols = list(write_cols)
Expand Down Expand Up @@ -1087,7 +1088,7 @@ def _helper_csvexcel(self, writer, na_rep=None, cols=None,
writer.writerow(row_fields)

def to_csv(self, path_or_buf, sep=",", na_rep='', cols=None,
col_aliases=None, header=True, index=True, index_label=None,
header=True, index=True, index_label=None,
mode='w', nanRep=None, encoding=None):
"""
Write DataFrame to a comma-separated values (csv) file
Expand All @@ -1100,10 +1101,9 @@ def to_csv(self, path_or_buf, sep=",", na_rep='', cols=None,
Missing data representation
cols : sequence, optional
Columns to write
col_aliases : sequence, default None
Optional column aliases to be written instead of column names
header : boolean, default True
Write out column names
header : boolean or list of string, default True
Write out column names. If a list of string is given it is
assumed to be aliases for the column names
index : boolean, default True
Write row names (index)
index_label : string or sequence, default None
Expand Down Expand Up @@ -1137,16 +1137,15 @@ def to_csv(self, path_or_buf, sep=",", na_rep='', cols=None,
else:
csvout = csv.writer(f, lineterminator='\n', delimiter=sep)
self._helper_csvexcel(csvout, na_rep=na_rep, cols=cols,
col_aliases=col_aliases, header=header,
index=index, index_label=index_label)
header=header, index=index,
index_label=index_label)

finally:
if close:
f.close()

def to_excel(self, excel_writer, sheet_name='sheet1', na_rep='',
cols=None, col_aliases=None, header=True, index=True,
index_label=None):
cols=None, header=True, index=True, index_label=None):
"""
Write DataFrame to a excel sheet
Expand All @@ -1160,8 +1159,9 @@ def to_excel(self, excel_writer, sheet_name='sheet1', na_rep='',
Missing data rep'n
cols : sequence, optional
Columns to write
header : boolean, default True
Write out column names
header : boolean or list of string, default True
Write out column names. If a list of string is given it is
assumed to be aliases for the column names
index : boolean, default True
Write row names (index)
index_label : string or sequence, default None
Expand All @@ -1186,8 +1186,8 @@ def to_excel(self, excel_writer, sheet_name='sheet1', na_rep='',
need_save = True
excel_writer.cur_sheet = sheet_name
self._helper_csvexcel(excel_writer, na_rep=na_rep, cols=cols,
col_aliases=col_aliases, header=header,
index=index, index_label=index_label)
header=header, index=index,
index_label=index_label)
if need_save:
excel_writer.save()

Expand Down
10 changes: 6 additions & 4 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3103,13 +3103,16 @@ def test_to_csv_from_csv(self):

# column aliases
col_aliases = Index(['AA', 'X', 'Y', 'Z'])
self.frame2.to_csv(path, header=True, col_aliases=col_aliases)
self.frame2.to_csv(path, header=col_aliases)
rs = DataFrame.from_csv(path)
xp = self.frame2.copy()
xp.columns = col_aliases

assert_frame_equal(xp, rs)

self.assertRaises(ValueError, self.frame2.to_csv, path,
header=['AA', 'X'])

os.remove(path)

def test_to_csv_multiindex(self):
Expand Down Expand Up @@ -3296,10 +3299,9 @@ def test_to_excel_from_excel(self):

# column aliases
col_aliases = Index(['AA', 'X', 'Y', 'Z'])
self.frame2.to_excel(path, 'test1', header=True,
col_aliases=col_aliases)
self.frame2.to_excel(path, 'test1', header=col_aliases)
reader = ExcelFile(path)
rs = reader.parse('test1')
rs = reader.parse('test1', index_col=0)
xp = self.frame2.copy()
xp.columns = col_aliases
assert_frame_equal(xp, rs)
Expand Down

0 comments on commit 7923fa5

Please sign in to comment.