@@ -775,35 +775,6 @@ def _pop_header_name(row, index_col):
775
775
return none_fill (row [i ]), row [:i ] + ['' ] + row [i + 1 :]
776
776
777
777
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
-
807
778
@add_metaclass (abc .ABCMeta )
808
779
class ExcelWriter (object ):
809
780
"""
@@ -949,6 +920,39 @@ def _get_sheet_name(self, sheet_name):
949
920
'cur_sheet property' )
950
921
return sheet_name
951
922
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
+
952
956
@classmethod
953
957
def check_extension (cls , ext ):
954
958
"""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,
1378
1382
row = startrow + cell .row + 1 ,
1379
1383
column = startcol + cell .col + 1
1380
1384
)
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
1388
1388
1389
1389
style_kwargs = {}
1390
1390
if cell .style :
@@ -1471,25 +1471,16 @@ def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0,
1471
1471
style_dict = {}
1472
1472
1473
1473
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 )
1484
1475
1485
1476
stylekey = json .dumps (cell .style )
1486
- if num_format_str :
1487
- stylekey += num_format_str
1477
+ if fmt :
1478
+ stylekey += fmt
1488
1479
1489
1480
if stylekey in style_dict :
1490
1481
style = style_dict [stylekey ]
1491
1482
else :
1492
- style = self ._convert_to_style (cell .style , num_format_str )
1483
+ style = self ._convert_to_style (cell .style , fmt )
1493
1484
style_dict [stylekey ] = style
1494
1485
1495
1486
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,
1747
1738
wks .freeze_panes (* (freeze_panes ))
1748
1739
1749
1740
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 )
1760
1742
1761
1743
stylekey = json .dumps (cell .style )
1762
- if num_format_str :
1763
- stylekey += num_format_str
1744
+ if fmt :
1745
+ stylekey += fmt
1764
1746
1765
1747
if stylekey in style_dict :
1766
1748
style = style_dict [stylekey ]
1767
1749
else :
1768
1750
style = self .book .add_format (
1769
- _XlsxStyler .convert (cell .style , num_format_str ))
1751
+ _XlsxStyler .convert (cell .style , fmt ))
1770
1752
style_dict [stylekey ] = style
1771
1753
1772
1754
if cell .mergestart is not None and cell .mergeend is not None :
0 commit comments