Skip to content

Commit 7c073c4

Browse files
committed
Merge pull request #6812 from jtratner/excel-inf
BUG/ENH: Fix to_excel representation of inf values
2 parents b4f3f0f + 4d3a60f commit 7c073c4

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

doc/source/release.rst

+3
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ API Changes
153153
- all offset operations now return ``Timestamp`` types (rather than datetime), Business/Week frequencies were incorrect (:issue:`4069`)
154154
- ``Series.iteritems()`` is now lazy (returns an iterator rather than a list). This was the documented behavior prior to 0.14. (:issue:`6760`)
155155
- ``Panel.shift`` now uses ``NDFrame.shift``. It no longer drops the ``nan`` data and retains its original shape. (:issue:`4867`)
156+
- ``to_excel`` now converts ``np.inf`` into a string representation,
157+
customizable by the ``inf_rep`` keyword argument (Excel has no native inf
158+
representation) (:issue:`6782`)
156159

157160
Deprecations
158161
~~~~~~~~~~~~

pandas/core/format.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -1369,10 +1369,14 @@ class ExcelFormatter(object):
13691369
sequence should be given if the DataFrame uses MultiIndex.
13701370
merge_cells : boolean, default False
13711371
Format MultiIndex and Hierarchical Rows as merged cells.
1372+
inf_rep : string, default `'inf'`
1373+
representation for np.inf values (which aren't representable in Excel)
1374+
A `'-'` sign will be added in front of -inf.
13721375
"""
13731376

13741377
def __init__(self, df, na_rep='', float_format=None, cols=None,
1375-
header=True, index=True, index_label=None, merge_cells=False):
1378+
header=True, index=True, index_label=None, merge_cells=False,
1379+
inf_rep='inf'):
13761380
self.df = df
13771381
self.rowcounter = 0
13781382
self.na_rep = na_rep
@@ -1384,12 +1388,18 @@ def __init__(self, df, na_rep='', float_format=None, cols=None,
13841388
self.index_label = index_label
13851389
self.header = header
13861390
self.merge_cells = merge_cells
1391+
self.inf_rep = inf_rep
13871392

13881393
def _format_value(self, val):
13891394
if lib.checknull(val):
13901395
val = self.na_rep
1391-
if self.float_format is not None and com.is_float(val):
1392-
val = float(self.float_format % val)
1396+
elif com.is_float(val):
1397+
if np.isposinf(val):
1398+
val = '-%s' % self.inf_rep
1399+
elif np.isneginf(val):
1400+
val = self.inf_rep
1401+
elif self.float_format is not None:
1402+
val = float(self.float_format % val)
13931403
return val
13941404

13951405
def _format_header_mi(self):

pandas/core/frame.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -1155,7 +1155,7 @@ def to_csv(self, path_or_buf=None, sep=",", na_rep='', float_format=None,
11551155
def to_excel(self, excel_writer, sheet_name='Sheet1', na_rep='',
11561156
float_format=None, columns=None, header=True, index=True,
11571157
index_label=None, startrow=0, startcol=0, engine=None,
1158-
merge_cells=True, encoding=None):
1158+
merge_cells=True, encoding=None, inf_rep='inf'):
11591159
"""
11601160
Write DataFrame to a excel sheet
11611161
@@ -1194,6 +1194,9 @@ def to_excel(self, excel_writer, sheet_name='Sheet1', na_rep='',
11941194
encoding of the resulting excel file. Only necessary for xlwt,
11951195
other writers support unicode natively.
11961196
cols : kwarg only alias of columns [deprecated]
1197+
inf_rep : string, default 'inf'
1198+
Representation for infinity (there is no native representation for
1199+
infinity in Excel)
11971200
11981201
Notes
11991202
-----
@@ -1207,7 +1210,7 @@ def to_excel(self, excel_writer, sheet_name='Sheet1', na_rep='',
12071210
>>> writer.save()
12081211
"""
12091212
from pandas.io.excel import ExcelWriter
1210-
1213+
12111214
need_save = False
12121215
if encoding == None:
12131216
encoding = 'ascii'
@@ -1223,7 +1226,8 @@ def to_excel(self, excel_writer, sheet_name='Sheet1', na_rep='',
12231226
float_format=float_format,
12241227
index=index,
12251228
index_label=index_label,
1226-
merge_cells=merge_cells)
1229+
merge_cells=merge_cells,
1230+
inf_rep=inf_rep)
12271231
formatted_cells = formatter.get_formatted_cells()
12281232
excel_writer.write_cells(formatted_cells, sheet_name,
12291233
startrow=startrow, startcol=startcol)

pandas/io/tests/test_excel.py

+10
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,16 @@ def test_bool_types(self):
538538
recons = reader.parse('test1').astype(np_type)
539539
tm.assert_frame_equal(frame, recons)
540540

541+
def test_inf_roundtrip(self):
542+
_skip_if_no_xlrd()
543+
544+
frame = DataFrame([(1, np.inf), (2, 3), (5, -np.inf)])
545+
with ensure_clean(self.ext) as path:
546+
frame.to_excel(path, 'test1')
547+
reader = ExcelFile(path)
548+
recons = reader.parse('test1')
549+
tm.assert_frame_equal(frame, recons)
550+
541551
def test_sheets(self):
542552
_skip_if_no_xlrd()
543553

0 commit comments

Comments
 (0)