Skip to content

Commit c8e997c

Browse files
committed
Consistent timedelta writing for all Excel engines
1 parent ceb9031 commit c8e997c

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

pandas/io/excel.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,12 @@ def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0,
13801380
)
13811381
xcell.value = _conv_value(cell.val)
13821382

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'
1388+
13831389
style_kwargs = {}
13841390
if cell.style:
13851391
key = str(cell.style)
@@ -1748,6 +1754,9 @@ def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0,
17481754
num_format_str = self.datetime_format
17491755
elif isinstance(cell.val, date):
17501756
num_format_str = self.date_format
1757+
elif isinstance(cell.val, timedelta):
1758+
delta = cell.val
1759+
val = delta.total_seconds() / float(86400)
17511760

17521761
stylekey = json.dumps(cell.style)
17531762
if num_format_str:

pandas/tests/io/test_excel.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,11 +1373,6 @@ def test_to_excel_interval_labels(self, merge_cells, engine, ext):
13731373

13741374
def test_to_excel_timedelta(self, merge_cells, engine, ext):
13751375
# GH 19242, GH9155 - test writing timedelta to xls
1376-
if engine == 'openpyxl':
1377-
pytest.xfail('Timedelta roundtrip broken with openpyxl')
1378-
if engine == 'xlsxwriter' and (sys.version_info[0] == 2 and
1379-
sys.platform.startswith('linux')):
1380-
pytest.xfail('Not working on linux with Py2 and xlsxwriter')
13811376
frame = DataFrame(np.random.randint(-10, 10, size=(20, 1)),
13821377
columns=['A'],
13831378
dtype=np.int64

0 commit comments

Comments
 (0)