# Buggy code
def _format_value(self, val):
    if is_scalar(val) and missing.isna(val):
        val = self.na_rep
    elif is_float(val):
        if missing.isposinf_scalar(val):
            val = self.inf_rep
        elif missing.isneginf_scalar(val):
            val = "-{inf}".format(inf=self.inf_rep)
        elif self.float_format is not None:
            val = float(self.float_format % val)
    return val


# Traceback (TypeError)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-4e18a4be2a71> in <module>()
----> 1 df.to_excel('test.xlsx')

/home/silvio/prod34/lib/python3.4/site-packages/pandas/core/frame.py in to_excel(self, excel_writer, sheet_name, na_rep, float_format, cols, header, index, index_label, startrow, startcol, engine, merge_cells)
1202         formatted_cells = formatter.get_formatted_cells()
1203         excel_writer.write_cells(formatted_cells, sheet_name,
-> 1204                                  startrow=startrow, startcol=startcol)
1205         if need_save:
1206             excel_writer.save()

/home/silvio/prod34/lib/python3.4/site-packages/pandas/io/excel.py in write_cells(self, cells, sheet_name, startrow, startcol)
771                 wks.write(startrow + cell.row,
772                           startcol + cell.col,
--> 773                           cell.val, style)
774
775     def _convert_to_style(self, style_dict, num_format_str=None):

/home/silvio/prod34/lib/python3.4/site-packages/xlsxwriter/worksheet.py in cell_wrapper(self, *args, **kwargs)
55             if len(args):
56                 int(args[0])
---> 57             return method(self, *args, **kwargs)
58         except ValueError:
59             # First arg isn't an int, convert to A1 notation.

/home/silvio/prod34/lib/python3.4/site-packages/xlsxwriter/worksheet.py in write(self, row, col, *args)
374         # Write datetime objects.
375         if isinstance(token, date_types):
--> 376             return self.write_datetime(row, col, *args)
377
378         # Write number types.

/home/silvio/prod34/lib/python3.4/site-packages/xlsxwriter/worksheet.py in cell_wrapper(self, *args, **kwargs)
55             if len(args):
56                 int(args[0])
---> 57             return method(self, *args, **kwargs)
58         except ValueError:
59             # First arg isn't an int, convert to A1 notation.

/home/silvio/prod34/lib/python3.4/site-packages/xlsxwriter/worksheet.py in write_datetime(self, row, col, date, cell_format)
666
667         # Convert datetime to an Excel date.
--> 668         number = self._convert_date_time(date)
669
670         # Add the default date format.

/home/silvio/prod34/lib/python3.4/site-packages/xlsxwriter/worksheet.py in _convert_date_time(self, dt_obj)
3265     def _convert_date_time(self, dt_obj):
3266         # Convert a datetime object to an Excel serial date and time.
-> 3267         return datetime_to_excel_datetime(dt_obj, self.date_1904)
3268
3269     def _options_changed(self):

/home/silvio/prod34/lib/python3.4/site-packages/xlsxwriter/utility.py in datetime_to_excel_datetime(dt_obj, date_1904)
577
578     # Convert a Python datetime.datetime value to an Excel date number.
--> 579     delta = dt_obj - epoch
580     excel_time = (delta.days
581                   + (float(delta.seconds)

/home/silvio/prod34/lib/python3.4/site-packages/pandas/tslib.cpython-34m.so in pandas.tslib._Timestamp.__sub__ (pandas/tslib.c:11918)()

TypeError: can't subtract offset-naive and offset-aware datetimes

# Fixed code
def _format_value(self, val):
    if is_scalar(val) and missing.isna(val):
        val = self.na_rep
    elif is_float(val):
        if missing.isposinf_scalar(val):
            val = self.inf_rep
        elif missing.isneginf_scalar(val):
            val = "-{inf}".format(inf=self.inf_rep)
        elif self.float_format is not None:
            val = float(self.float_format % val)
    if getattr(val, "tzinfo", None) is not None:
        raise ValueError(
            "Excel does not support datetimes with "
            "timezones. Please ensure that datetimes "
            "are timezone unaware before writing to Excel."
        )
    return val