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
Formatting a column #204
Comments
Hi Dror, Thank you for the detailed report and examples. The import pandas as pd
from pandas import ExcelWriter
df = pd.DataFrame([[123456, 2], [3, 4]],
columns=['First column', 'Second column'])
writer = ExcelWriter('test.xlsx', engine='xlsxwriter')
df.to_excel(writer)
formater = writer.book.add_format({'num_format': '#,##0'})
sheet = writer.book.worksheets()[0]
sheet.write('B5', 1234.8888)
sheet.set_column(1, 1, 40, formater)
writer.save() The issue is that pandas already applies a format to the numbers in cells B1 and B3. In cases like this XlsxWriter behaves like Excel. Cell formatting overwrites Row formatting which overwrites Column formatting. So since there is already a cell format you won't get the column format. Note however that cell B5 does have column format. It is possible to specify the cell number format in pandas but that will apply to all cells with data. Apart from that, or using XlsxWriter outside of pandas, there isn't a workaround. Regards, John |
So Can you provide some link to the Pandas documentation on the matter? Where is it setting the format of a cell? |
Yes. Pandas sets a cell level format and in Excel cell format overrides row format overrides column format.
I was wrong about that. Pandas only sets a cell format for headers and for cells that contain either a The reason that it isn't is due to a bug/feature: all cells in the Pandas XlsxWriter handler have a default I'll submit a bug report and fix to Pandas. In the meantime you can workaround it by patching your local copy of the diff --git a/pandas/io/excel.py b/pandas/io/excel.py
index e81c279..637937d 100644
--- a/pandas/io/excel.py
+++ b/pandas/io/excel.py
@@ -815,6 +815,10 @@ class _XlsxWriter(ExcelWriter):
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()
I'll post an update here when I submit a fix to Pandas. John |
Wonderful! Worked like a charm. |
Hi Dror, I've submitted a bug report and patch to pandas: pandas-dev/pandas#9167 I'll close this issue since it isn't strictly an XlsxWriter issue and you can add a +1 to the pandas issue so that you get an update when it is closed. Thanks for the report. John |
This is fixed and merged in pandas now. |
Great news! Thanks for the effort.
|
I fail to format a column when exporting from pandas.DataFrame. Consider the following minimal example:
As you can note, the number in cell F5 is well-formatted, but the number in B2 (in column 1) is not formatted (and the width of the column is not changed).
In contrast, a similar approach works when it doesn't come from Pandas:
What am I doing wrong?
Thanks in advance,
Dror
The text was updated successfully, but these errors were encountered: