Skip to content

Commit

Permalink
BUG: Fix for extraneous default cell format in xlsxwriter files.
Browse files Browse the repository at this point in the history
Fix for issue in the xlsxwriter engine where is adds a default
'General' format to cells if no other format is applied. This
isn't a bug, per se, but it prevents other row or column formatting.

Closes #9167
  • Loading branch information
jmcnamara authored and jreback committed Jan 6, 2015
1 parent 389b022 commit 07a5735
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
6 changes: 5 additions & 1 deletion doc/source/whatsnew/v0.16.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
4 changes: 4 additions & 0 deletions pandas/io/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
41 changes: 41 additions & 0 deletions pandas/io/tests/test_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 07a5735

Please sign in to comment.