Skip to content

Commit

Permalink
ENH: format string for to_csv #1525
Browse files Browse the repository at this point in the history
  • Loading branch information
changhiskhan committed Aug 20, 2012
1 parent f919568 commit 3942eb0
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 8 deletions.
22 changes: 16 additions & 6 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,8 @@ def to_panel(self):
to_wide = deprecate('to_wide', to_panel)

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

Expand Down Expand Up @@ -1089,13 +1090,15 @@ def _helper_csvexcel(self, writer, na_rep=None, cols=None,
val = series[col][j]
if isnull(val):
val = na_rep
if float_format is not None and com.is_float(val):
val = float_format % val

row_fields.append(val)

writer.writerow(row_fields)

def to_csv(self, path_or_buf, sep=",", na_rep='', cols=None,
header=True, index=True, index_label=None,
def to_csv(self, path_or_buf, sep=",", na_rep='', float_format=None,
cols=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 @@ -1106,6 +1109,8 @@ def to_csv(self, path_or_buf, sep=",", na_rep='', cols=None,
File path
na_rep : string, default ''
Missing data representation
float_format : string, default None
Format string for floating point numbers
cols : sequence, optional
Columns to write
header : boolean or list of string, default True
Expand Down Expand Up @@ -1143,7 +1148,8 @@ def to_csv(self, path_or_buf, sep=",", na_rep='', cols=None,
delimiter=sep, encoding=encoding)
else:
csvout = csv.writer(f, lineterminator='\n', delimiter=sep)
self._helper_csvexcel(csvout, na_rep=na_rep, cols=cols,
self._helper_csvexcel(csvout, na_rep=na_rep,
float_format=float_format, cols=cols,
header=header, index=index,
index_label=index_label)

Expand All @@ -1152,7 +1158,8 @@ def to_csv(self, path_or_buf, sep=",", na_rep='', cols=None,
f.close()

def to_excel(self, excel_writer, sheet_name='sheet1', na_rep='',
cols=None, header=True, index=True, index_label=None):
float_format=None, cols=None, header=True, index=True,
index_label=None):
"""
Write DataFrame to a excel sheet
Expand All @@ -1164,6 +1171,8 @@ def to_excel(self, excel_writer, sheet_name='sheet1', na_rep='',
Name of sheet which will contain DataFrame
na_rep : string, default ''
Missing data representation
float_format : string, default None
Format string for floating point numbers
cols : sequence, optional
Columns to write
header : boolean or list of string, default True
Expand Down Expand Up @@ -1192,7 +1201,8 @@ def to_excel(self, excel_writer, sheet_name='sheet1', na_rep='',
excel_writer = ExcelWriter(excel_writer)
need_save = True
excel_writer.cur_sheet = sheet_name
self._helper_csvexcel(excel_writer, na_rep=na_rep, cols=cols,
self._helper_csvexcel(excel_writer, na_rep=na_rep,
float_format=float_format, cols=cols,
header=header, index=index,
index_label=index_label)
if need_save:
Expand Down
8 changes: 6 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2355,7 +2355,8 @@ def from_csv(cls, path, sep=',', parse_dates=True, header=None,
result.index.name = result.name = None
return result

def to_csv(self, path, index=True, sep=",", na_rep='', header=False,
def to_csv(self, path, index=True, sep=",", na_rep='',
float_format=None, header=False,
index_label=None, mode='w', nanRep=None, encoding=None):
"""
Write Series to a comma-separated values (csv) file
Expand All @@ -2365,6 +2366,8 @@ def to_csv(self, path, index=True, sep=",", na_rep='', header=False,
path : string file path or file handle / StringIO
na_rep : string, default ''
Missing data representation
float_format : string, default None
Format string for floating point numbers
header : boolean, default False
Write out series name
index : boolean, default True
Expand All @@ -2382,7 +2385,8 @@ def to_csv(self, path, index=True, sep=",", na_rep='', header=False,
"""
from pandas.core.frame import DataFrame
df = DataFrame(self)
df.to_csv(path, index=index, sep=sep, na_rep=na_rep, header=header,
df.to_csv(path, index=index, sep=sep, na_rep=na_rep,
float_format=float_format, header=header,
index_label=index_label, mode=mode, nanRep=nanRep,
encoding=encoding)

Expand Down
30 changes: 30 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3300,6 +3300,20 @@ def test_to_csv_stringio(self):
recons = pan.read_csv(buf, index_col=0)
assert_frame_equal(recons, self.frame)

def test_to_csv_float_format(self):
filename = '__tmp__.csv'
df = DataFrame([[0.123456, 0.234567, 0.567567],
[12.32112, 123123.2, 321321.2]],
index=['A', 'B'], columns=['X', 'Y', 'Z'])
df.to_csv(filename, float_format='%.2f')

rs = pan.read_csv(filename, index_col=0)
xp = DataFrame([[0.12, 0.23, 0.57],
[12.32, 123123.20, 321321.20]],
index=['A', 'B'], columns=['X', 'Y', 'Z'])
assert_frame_equal(rs, xp)
os.remove(filename)

def test_to_excel_from_excel(self):
try:
import xlwt
Expand Down Expand Up @@ -3478,6 +3492,22 @@ def test_to_excel_multiindex(self):

os.remove(path)

def test_to_excel_float_format(self):
for ext in ['xls', 'xlsx']:
filename = '__tmp__.' + ext
df = DataFrame([[0.123456, 0.234567, 0.567567],
[12.32112, 123123.2, 321321.2]],
index=['A', 'B'], columns=['X', 'Y', 'Z'])
df.to_excel(filename, 'test1', float_format='%.2f')

reader = ExcelFile(filename)
rs = reader.parse('test1', index_col=None)
xp = DataFrame([[0.12, 0.23, 0.57],
[12.32, 123123.20, 321321.20]],
index=['A', 'B'], columns=['X', 'Y', 'Z'])
assert_frame_equal(rs, xp)
os.remove(filename)

def test_info(self):
io = StringIO()
self.frame.info(buf=io)
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2018,6 +2018,16 @@ def test_to_csv_stringio(self):
def test_to_dict(self):
self.assert_(np.array_equal(Series(self.ts.to_dict()), self.ts))

def test_to_csv_float_format(self):
filename = '__tmp__.csv'
ser = Series([0.123456, 0.234567, 0.567567])
ser.to_csv(filename, float_format='%.2f')

rs = Series.from_csv(filename)
xp = Series([0.12, 0.23, 0.57])
assert_series_equal(rs, xp)
os.remove(filename)

def test_clip(self):
val = self.ts.median()

Expand Down

0 comments on commit 3942eb0

Please sign in to comment.