Skip to content

BUG: XlsxWriter ignoring formats on numpy types if merged cells #27006

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 28, 2019
2 changes: 1 addition & 1 deletion pandas/io/excel/_xlsxwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0,
startcol + cell.col,
startrow + cell.mergestart,
startcol + cell.mergeend,
cell.val, style)
val, style)
else:
wks.write(startrow + cell.row,
startcol + cell.col,
Expand Down
30 changes: 30 additions & 0 deletions pandas/tests/io/excel/test_xlsxwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
from pandas import DataFrame
from pandas.util.testing import ensure_clean

from pandas.core.indexes.multi import MultiIndex
from pandas.core.indexes.period import Period, PeriodIndex
import numpy as np

from pandas.io.excel import ExcelWriter

xlsxwriter = pytest.importorskip("xlsxwriter")
Expand Down Expand Up @@ -63,3 +67,29 @@ def test_write_append_mode_raises(ext):
with ensure_clean(ext) as f:
with pytest.raises(ValueError, match=msg):
ExcelWriter(f, engine='xlsxwriter', mode='a')


def test_merged_cell_custom_objects(ext):
# Test that custom object types residing within merged (grouped)
# cells are converted to python data types before being passed to
# the xlsxwriter package. Test for issue #27006

# create a custom object type, and place it in a grouped dataframe
pixy = PeriodIndex(['2018', '2018', '2018', '2018',
'2019', '2019', '2019', '2019'], freq='Y')
pixq = PeriodIndex(['2018Q1', '2018Q2', '2018Q3', '2018Q4',
'2019Q1', '2019Q2', '2019Q3', '2019Q4'], freq='Q')
pixarr = [pixy, pixq]
mipix = MultiIndex.from_arrays(pixarr, names=['year', 'quarter'])
df = DataFrame(np.random.rand(2, len(mipix)), columns=mipix)

# write the dataframe to excel
try:
with ensure_clean(ext) as path:
writer = ExcelWriter(path)
df.to_excel(writer, sheet_name='test')
passed = True
except TypeError:
passed = False

assert passed