diff --git a/doc/source/whatsnew/v0.16.0.txt b/doc/source/whatsnew/v0.16.0.txt index 3246a78dc14eb..839a055bf2a63 100644 --- a/doc/source/whatsnew/v0.16.0.txt +++ b/doc/source/whatsnew/v0.16.0.txt @@ -144,6 +144,10 @@ Bug Fixes - DataFrame now properly supports simultaneous ``copy`` and ``dtype`` arguments in constructor (:issue:`9099`) - Bug in read_csv when using skiprows on a file with CR line endings with the c engine. (:issue:`9079`) -- isnull now detects NaT in PeriodIndex (:issue:`9129`) +- isnull now detects ``NaT`` in PeriodIndex (:issue:`9129`) - Bug in groupby ``.nth()`` with a multiple column groupby (:issue:`8979`) + - Fixed division by zero error for ``Series.kurt()`` when all values are equal (:issue:`9197`) + + +- Fixed issue in the ``xlsxwriter`` engine where it added a default 'General' format to cells if no other format wass applied. This prevented other row or column formatting being applied. (:issue:`9167`) diff --git a/pandas/io/excel.py b/pandas/io/excel.py index 2ece91b5dea11..acec411a2e546 100644 --- a/pandas/io/excel.py +++ b/pandas/io/excel.py @@ -1274,6 +1274,10 @@ def _convert_to_style(self, style_dict, num_format_str=None): num_format_str: optional number format string """ + # If there is no formatting we don't create a format object. + if num_format_str is None and style_dict is None: + return None + # Create a XlsxWriter format object. xl_format = self.book.add_format() diff --git a/pandas/io/tests/test_excel.py b/pandas/io/tests/test_excel.py index 4f97cef3d46d3..634402d891e53 100644 --- a/pandas/io/tests/test_excel.py +++ b/pandas/io/tests/test_excel.py @@ -1353,6 +1353,47 @@ class XlsxWriterTests(ExcelWriterBase, tm.TestCase): engine_name = 'xlsxwriter' check_skip = staticmethod(_skip_if_no_xlsxwriter) + def test_column_format(self): + # Test that column formats are applied to cells. Test for issue #9167. + # Applicable to xlsxwriter only. + _skip_if_no_xlsxwriter() + + import warnings + with warnings.catch_warnings(): + # Ignore the openpyxl lxml warning. + warnings.simplefilter("ignore") + _skip_if_no_openpyxl() + import openpyxl + + with ensure_clean(self.ext) as path: + frame = DataFrame({'A': [123456, 123456], + 'B': [123456, 123456]}) + + writer = ExcelWriter(path) + frame.to_excel(writer) + + # Add a number format to col B and ensure it is applied to cells. + num_format = '#,##0' + write_workbook = writer.book + write_worksheet = write_workbook.worksheets()[0] + col_format = write_workbook.add_format({'num_format': num_format}) + write_worksheet.set_column('B:B', None, col_format) + writer.save() + + read_workbook = openpyxl.load_workbook(path) + read_worksheet = read_workbook.get_sheet_by_name(name='Sheet1') + + # Get the number format from the cell. This method is backward + # compatible with older versions of openpyxl. + cell = read_worksheet.cell('B2') + + try: + read_num_format = cell.style.number_format._format_code + except: + read_num_format = cell.style.number_format + + self.assertEqual(read_num_format, num_format) + class OpenpyxlTests_NoMerge(ExcelWriterBase, tm.TestCase): ext = '.xlsx'