@@ -775,35 +775,6 @@ def _pop_header_name(row, index_col):
775775 return none_fill (row [i ]), row [:i ] + ['' ] + row [i + 1 :]
776776
777777
778- def _conv_value (val ):
779- """ Convert numpy types to Python types for the Excel writers.
780-
781- Parameters
782- ----------
783- val : object
784- Value to be written into cells
785-
786- Returns
787- -------
788- If val is a numpy int, float, or bool, then the equivalent Python
789- types are returned. :obj:`datetime`, :obj:`date`, and :obj:`timedelta`
790- are passed and formatting must be handled in the writer. :obj:`str`
791- representation is returned for all other types.
792- """
793- if is_integer (val ):
794- val = int (val )
795- elif is_float (val ):
796- val = float (val )
797- elif is_bool (val ):
798- val = bool (val )
799- elif isinstance (val , (datetime , date , timedelta )):
800- pass
801- else :
802- val = compat .to_str (val )
803-
804- return val
805-
806-
807778@add_metaclass (abc .ABCMeta )
808779class ExcelWriter (object ):
809780 """
@@ -949,6 +920,39 @@ def _get_sheet_name(self, sheet_name):
949920 'cur_sheet property' )
950921 return sheet_name
951922
923+ def _value_with_fmt (self , val ):
924+ """Convert numpy types to Python types for the Excel writers.
925+
926+ Parameters
927+ ----------
928+ val : object
929+ Value to be written into cells
930+
931+ Returns
932+ -------
933+ Tuple with the first element being the converted value and the second
934+ being an optional format
935+ """
936+ fmt = None
937+
938+ if is_integer (val ):
939+ val = int (val )
940+ elif is_float (val ):
941+ val = float (val )
942+ elif is_bool (val ):
943+ val = bool (val )
944+ elif isinstance (val , datetime ):
945+ fmt = self .datetime_format
946+ elif isinstance (val , date ):
947+ fmt = self .date_format
948+ elif isinstance (val , timedelta ):
949+ val = val .total_seconds () / float (86400 )
950+ fmt = '0'
951+ else :
952+ val = compat .to_str (val )
953+
954+ return val , fmt
955+
952956 @classmethod
953957 def check_extension (cls , ext ):
954958 """checks that path's extension against the Writer's supported
@@ -1378,13 +1382,9 @@ def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0,
13781382 row = startrow + cell .row + 1 ,
13791383 column = startcol + cell .col + 1
13801384 )
1381- xcell .value = _conv_value (cell .val )
1382-
1383- if isinstance (cell .val , timedelta ):
1384- delta = cell .val
1385- xcell .value = delta .total_seconds () / float (86400 )
1386- # Set format to prevent conversion to datetime
1387- xcell .number_format = '0'
1385+ xcell .value , fmt = self ._value_with_fmt (cell .val )
1386+ if fmt :
1387+ xcell .number_format = fmt
13881388
13891389 style_kwargs = {}
13901390 if cell .style :
@@ -1471,25 +1471,16 @@ def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0,
14711471 style_dict = {}
14721472
14731473 for cell in cells :
1474- val = _conv_value (cell .val )
1475-
1476- num_format_str = None
1477- if isinstance (cell .val , datetime ):
1478- num_format_str = self .datetime_format
1479- elif isinstance (cell .val , date ):
1480- num_format_str = self .date_format
1481- elif isinstance (cell .val , timedelta ):
1482- delta = cell .val
1483- val = delta .total_seconds () / float (86400 )
1474+ val , fmt = self ._value_with_fmt (cell .val )
14841475
14851476 stylekey = json .dumps (cell .style )
1486- if num_format_str :
1487- stylekey += num_format_str
1477+ if fmt :
1478+ stylekey += fmt
14881479
14891480 if stylekey in style_dict :
14901481 style = style_dict [stylekey ]
14911482 else :
1492- style = self ._convert_to_style (cell .style , num_format_str )
1483+ style = self ._convert_to_style (cell .style , fmt )
14931484 style_dict [stylekey ] = style
14941485
14951486 if cell .mergestart is not None and cell .mergeend is not None :
@@ -1747,26 +1738,17 @@ def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0,
17471738 wks .freeze_panes (* (freeze_panes ))
17481739
17491740 for cell in cells :
1750- val = _conv_value (cell .val )
1751-
1752- num_format_str = None
1753- if isinstance (cell .val , datetime ):
1754- num_format_str = self .datetime_format
1755- elif isinstance (cell .val , date ):
1756- num_format_str = self .date_format
1757- elif isinstance (cell .val , timedelta ):
1758- delta = cell .val
1759- val = delta .total_seconds () / float (86400 )
1741+ val , fmt = self ._value_with_fmt (cell .val )
17601742
17611743 stylekey = json .dumps (cell .style )
1762- if num_format_str :
1763- stylekey += num_format_str
1744+ if fmt :
1745+ stylekey += fmt
17641746
17651747 if stylekey in style_dict :
17661748 style = style_dict [stylekey ]
17671749 else :
17681750 style = self .book .add_format (
1769- _XlsxStyler .convert (cell .style , num_format_str ))
1751+ _XlsxStyler .convert (cell .style , fmt ))
17701752 style_dict [stylekey ] = style
17711753
17721754 if cell .mergestart is not None and cell .mergeend is not None :
0 commit comments