Repository: pandas-dev/pandas

Issue:
to_excel() TyperError: Unsupported type <class 'pandas._libs.tslibs.period.Period'> in write()
#### Code Sample, a copy-pastable example if possible

```python
writer = pd.ExcelWriter(Path().resolve() / 'output' / 'out.xlsx')
for dftup in exportlist:
    dftup[0].to_excel(writer, sheet_name=dftup[2])
```
#### Problem description
Note that a similar issue was covered in  in #19242, but with a different data type.
The above code takes a list of tuples which contain the DataFrames (in position 0) and the desired sheet names (in position 2) I would like to write. These DataFrames have MultiIndex columns whose levels are type PeriodIndex. The first level is an index of yearly Periods (freq='Y') and the second level an index of Quarterly periods (freq='Q').

When I try to write the DataFrames to excel, I get the following error (I can post the whole trace if needed). Any ideas?

```
TypeError: Unsupported type <class 'pandas._libs.tslibs.period.Period'> in write()
```

#### Expected Output

#### Output of ``pd.show_versions()``

<details>

INSTALLED VERSIONS
------------------
commit: None
python: 3.7.3.final.0
python-bits: 64
OS: Darwin
OS-release: 18.6.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.24.2
pytest: 4.5.0
pip: 19.1.1
setuptools: 41.0.1
Cython: 0.29.8
numpy: 1.16.4
scipy: 1.2.1
pyarrow: None
xarray: None
IPython: 7.5.0
sphinx: 2.0.1
patsy: 0.5.1
dateutil: 2.8.0
pytz: 2019.1
blosc: None
bottleneck: 1.2.1
tables: 3.5.1
numexpr: 2.6.9
feather: None
matplotlib: 3.0.3
openpyxl: 2.6.2
xlrd: 1.2.0
xlwt: 1.3.0
xlsxwriter: 1.1.8
lxml.etree: 4.3.3
bs4: 4.7.1
html5lib: 1.0.1
sqlalchemy: 1.3.3
pymysql: None
psycopg2: 2.7.6.1 (dt dec pq3 ext lo64)
jinja2: 2.10.1
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None

</details>





Discussion:
If you take a look at #19244 could probably extend that to make sure this is supported. Want to try a PR?
Sure thing - I'll have a look and create a PR.
Ok so I have identified the source of the bug - but I have a question before I do a PR. It looks like the Excel IO has been changed a bit on the master branch from what the current stable 0.24.2 release is. I know where to make the change in both versions, but am not sure which I should push.

Thanks
only PR to master

Resolving patch:
diff --git a/pandas/io/excel/_xlsxwriter.py b/pandas/io/excel/_xlsxwriter.py
--- a/pandas/io/excel/_xlsxwriter.py
+++ b/pandas/io/excel/_xlsxwriter.py
@@ -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,


Test patch:
diff --git a/pandas/tests/io/excel/test_writers.py b/pandas/tests/io/excel/test_writers.py
--- a/pandas/tests/io/excel/test_writers.py
+++ b/pandas/tests/io/excel/test_writers.py
@@ -1162,6 +1162,22 @@ def test_path_local_path(self, engine, ext):
                                        path="foo.{ext}".format(ext=ext))
         tm.assert_frame_equal(result, df)
 
+    def test_merged_cell_custom_objects(self, engine, merge_cells, ext):
+        # see GH-27006
+        mi = MultiIndex.from_tuples([(pd.Period('2018'), pd.Period('2018Q1')),
+                                     (pd.Period('2018'), pd.Period('2018Q2'))])
+        expected = DataFrame(np.ones((2, 2)), columns=mi)
+        expected.to_excel(self.path)
+        result = pd.read_excel(self.path, header=[0, 1],
+                               index_col=0, convert_float=False)
+        # need to convert PeriodIndexes to standard Indexes for assert equal
+        expected.columns.set_levels([[str(i) for i in mi.levels[0]],
+                                     [str(i) for i in mi.levels[1]]],
+                                    level=[0, 1],
+                                    inplace=True)
+        expected.index = expected.index.astype(np.float64)
+        tm.assert_frame_equal(expected, result)
+
 
 class TestExcelWriterEngineTests: