Skip to content

Commit bb25a97

Browse files
jtratnerjeffreystarr
authored andcommitted
BUG/ENH: Fix to_excel representation of inf values
1 parent 1061db0 commit bb25a97

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
@@ -158,6 +158,9 @@ API Changes
158158
- all offset operations now return ``Timestamp`` types (rather than datetime), Business/Week frequencies were incorrect (:issue:`4069`)
159159
- ``Series.iteritems()`` is now lazy (returns an iterator rather than a list). This was the documented behavior prior to 0.14. (:issue:`6760`)
160160
- ``Panel.shift`` now uses ``NDFrame.shift``. It no longer drops the ``nan`` data and retains its original shape. (:issue:`4867`)
161+
- ``to_excel`` now converts ``np.inf`` into a string representation,
162+
customizable by the ``inf_rep`` keyword argument (Excel has no native inf
163+
representation) (:issue:`6782`)
161164

162165
Deprecations
163166
~~~~~~~~~~~~

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
@@ -1149,7 +1149,7 @@ def to_csv(self, path_or_buf=None, sep=",", na_rep='', float_format=None,
11491149
def to_excel(self, excel_writer, sheet_name='Sheet1', na_rep='',
11501150
float_format=None, columns=None, header=True, index=True,
11511151
index_label=None, startrow=0, startcol=0, engine=None,
1152-
merge_cells=True, encoding=None):
1152+
merge_cells=True, encoding=None, inf_rep='inf'):
11531153
"""
11541154
Write DataFrame to a excel sheet
11551155
@@ -1188,6 +1188,9 @@ def to_excel(self, excel_writer, sheet_name='Sheet1', na_rep='',
11881188
encoding of the resulting excel file. Only necessary for xlwt,
11891189
other writers support unicode natively.
11901190
cols : kwarg only alias of columns [deprecated]
1191+
inf_rep : string, default 'inf'
1192+
Representation for infinity (there is no native representation for
1193+
infinity in Excel)
11911194
11921195
Notes
11931196
-----
@@ -1201,7 +1204,7 @@ def to_excel(self, excel_writer, sheet_name='Sheet1', na_rep='',
12011204
>>> writer.save()
12021205
"""
12031206
from pandas.io.excel import ExcelWriter
1204-
1207+
12051208
need_save = False
12061209
if encoding == None:
12071210
encoding = 'ascii'
@@ -1217,7 +1220,8 @@ def to_excel(self, excel_writer, sheet_name='Sheet1', na_rep='',
12171220
float_format=float_format,
12181221
index=index,
12191222
index_label=index_label,
1220-
merge_cells=merge_cells)
1223+
merge_cells=merge_cells,
1224+
inf_rep=inf_rep)
12211225
formatted_cells = formatter.get_formatted_cells()
12221226
excel_writer.write_cells(formatted_cells, sheet_name,
12231227
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)