From f30ca4e572de64ca2d5f7165ba27e4afbabf0cec Mon Sep 17 00:00:00 2001 From: Danny Kim Date: Wed, 19 Sep 2018 16:19:35 +1000 Subject: [PATCH 1/5] ENH: Making header_style a property of ExcelFormatter #22758 --- pandas/io/formats/excel.py | 45 ++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/pandas/io/formats/excel.py b/pandas/io/formats/excel.py index 0bc268bc18b95..d6fcfb2207cf9 100644 --- a/pandas/io/formats/excel.py +++ b/pandas/io/formats/excel.py @@ -34,15 +34,6 @@ def __init__(self, row, col, val, style=None, mergestart=None, self.mergeend = mergeend -header_style = {"font": {"bold": True}, - "borders": {"top": "thin", - "right": "thin", - "bottom": "thin", - "left": "thin"}, - "alignment": {"horizontal": "center", - "vertical": "top"}} - - class CSSToExcelConverter(object): """A callable for converting CSS declarations to ExcelWriter styles @@ -389,6 +380,16 @@ def __init__(self, df, na_rep='', float_format=None, cols=None, self.merge_cells = merge_cells self.inf_rep = inf_rep + @property + def header_style(self): + return {"font": {"bold": True}, + "borders": {"top": "thin", + "right": "thin", + "bottom": "thin", + "left": "thin"}, + "alignment": {"horizontal": "center", + "vertical": "top"}} + def _format_value(self, val): if is_scalar(val) and missing.isna(val): val = self.na_rep @@ -427,7 +428,7 @@ def _format_header_mi(self): # Format multi-index as a merged cells. for lnum in range(len(level_lengths)): name = columns.names[lnum] - yield ExcelCell(lnum, coloffset, name, header_style) + yield ExcelCell(lnum, coloffset, name, self.header_style) for lnum, (spans, levels, labels) in enumerate(zip( level_lengths, columns.levels, columns.labels)): @@ -435,16 +436,16 @@ def _format_header_mi(self): for i in spans: if spans[i] > 1: yield ExcelCell(lnum, coloffset + i + 1, values[i], - header_style, lnum, + self.header_style, lnum, coloffset + i + spans[i]) else: yield ExcelCell(lnum, coloffset + i + 1, values[i], - header_style) + self.header_style) else: # Format in legacy format with dots to indicate levels. for i, values in enumerate(zip(*level_strs)): v = ".".join(map(pprint_thing, values)) - yield ExcelCell(lnum, coloffset + i + 1, v, header_style) + yield ExcelCell(lnum, coloffset + i + 1, v, self.header_style) self.rowcounter = lnum @@ -469,7 +470,7 @@ def _format_header_regular(self): for colindex, colname in enumerate(colnames): yield ExcelCell(self.rowcounter, colindex + coloffset, colname, - header_style) + self.header_style) def _format_header(self): if isinstance(self.columns, ABCMultiIndex): @@ -482,7 +483,8 @@ def _format_header(self): row = [x if x is not None else '' for x in self.df.index.names] + [''] * len(self.columns) if reduce(lambda x, y: x and y, map(lambda x: x != '', row)): - gen2 = (ExcelCell(self.rowcounter, colindex, val, header_style) + gen2 = (ExcelCell(self.rowcounter, colindex, val, + self.header_style) for colindex, val in enumerate(row)) self.rowcounter += 1 return itertools.chain(gen, gen2) @@ -518,7 +520,7 @@ def _format_regular_rows(self): if index_label and self.header is not False: yield ExcelCell(self.rowcounter - 1, 0, index_label, - header_style) + self.header_style) # write index_values index_values = self.df.index @@ -526,7 +528,8 @@ def _format_regular_rows(self): index_values = self.df.index.to_timestamp() for idx, idxval in enumerate(index_values): - yield ExcelCell(self.rowcounter + idx, 0, idxval, header_style) + yield ExcelCell(self.rowcounter + idx, 0, idxval, + self.header_style) coloffset = 1 else: @@ -562,7 +565,7 @@ def _format_hierarchical_rows(self): for cidx, name in enumerate(index_labels): yield ExcelCell(self.rowcounter - 1, cidx, name, - header_style) + self.header_style) if self.merge_cells: # Format hierarchical rows as merged cells. @@ -581,12 +584,12 @@ def _format_hierarchical_rows(self): for i in spans: if spans[i] > 1: yield ExcelCell(self.rowcounter + i, gcolidx, - values[i], header_style, + values[i], self.header_style, self.rowcounter + i + spans[i] - 1, gcolidx) else: yield ExcelCell(self.rowcounter + i, gcolidx, - values[i], header_style) + values[i], self.header_style) gcolidx += 1 else: @@ -594,7 +597,7 @@ def _format_hierarchical_rows(self): for indexcolvals in zip(*self.df.index): for idx, indexcolval in enumerate(indexcolvals): yield ExcelCell(self.rowcounter + idx, gcolidx, - indexcolval, header_style) + indexcolval, self.header_style) gcolidx += 1 for cell in self._generate_body(gcolidx): From b8e9365e599e6a4fe5c56925199a57183d0c8b73 Mon Sep 17 00:00:00 2001 From: Danny Kim Date: Wed, 19 Sep 2018 19:19:30 +1000 Subject: [PATCH 2/5] DOC: Start 0.250 whatsnew #22759 --- doc/source/whatsnew.rst | 2 + doc/source/whatsnew/v0.25.0.txt | 178 ++++++++++++++++++++++++++++++++ 2 files changed, 180 insertions(+) create mode 100644 doc/source/whatsnew/v0.25.0.txt diff --git a/doc/source/whatsnew.rst b/doc/source/whatsnew.rst index 8672685b3ebb4..6a661c5f9dd15 100644 --- a/doc/source/whatsnew.rst +++ b/doc/source/whatsnew.rst @@ -18,6 +18,8 @@ What's New These are new features and improvements of note in each release. +.. include:: whatsnew/v0.25.0.txt + .. include:: whatsnew/v0.24.0.txt .. include:: whatsnew/v0.23.4.txt diff --git a/doc/source/whatsnew/v0.25.0.txt b/doc/source/whatsnew/v0.25.0.txt new file mode 100644 index 0000000000000..f9ca145731231 --- /dev/null +++ b/doc/source/whatsnew/v0.25.0.txt @@ -0,0 +1,178 @@ +.. _whatsnew_0250: + +v0.25.0 (Month XX, 2018) +------------------------ + +.. _whatsnew_0250.enhancements: + +New features +~~~~~~~~~~~~ + +.. _whatsnew_0250.enhancements.other: + +Other Enhancements +^^^^^^^^^^^^^^^^^^ +- :class:`ExcelFormatter` now has a `header_style` property. Was previously in global scope (:issue:`22758`). +- +- + +.. _whatsnew_0250.api_breaking: + + +Backwards incompatible API changes +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. _whatsnew_0250.api.other: + +Other API Changes +^^^^^^^^^^^^^^^^^ + +- +- +- + +.. _whatsnew_0250.deprecations: + +Deprecations +~~~~~~~~~~~~ + +- +- +- + +.. _whatsnew_0250.prior_deprecations: + +Removal of prior version deprecations/changes +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +- +- +- + +.. _whatsnew_0250.performance: + +Performance Improvements +~~~~~~~~~~~~~~~~~~~~~~~~ + +- +- +- + +.. _whatsnew_0250.docs: + +Documentation Changes +~~~~~~~~~~~~~~~~~~~~~ + +- +- +- + +.. _whatsnew_0250.bug_fixes: + +Bug Fixes +~~~~~~~~~ + +Categorical +^^^^^^^^^^^ + +- +- +- + +Datetimelike +^^^^^^^^^^^^ + +- +- +- + +Timedelta +^^^^^^^^^ + +- +- +- + +Timezones +^^^^^^^^^ + +- +- +- + +Offsets +^^^^^^^ + +- +- +- + +Numeric +^^^^^^^ + +- +- +- + +Strings +^^^^^^^ + +- +- +- + +Indexing +^^^^^^^^ + +- +- +- + +MultiIndex +^^^^^^^^^^ + +- +- +- + +I/O +^^^ + +- +- +- + +Plotting +^^^^^^^^ + +- +- +- + +Groupby/Resample/Rolling +^^^^^^^^^^^^^^^^^^^^^^^^ + +- +- +- + +Sparse +^^^^^^ + +- +- +- + +Reshaping +^^^^^^^^^ + +- +- +- + +Other +^^^^^ + +- +- +- From 65aeba3470187357d131a07faeaf6df0d7b333fa Mon Sep 17 00:00:00 2001 From: Danny Kim Date: Wed, 19 Sep 2018 23:39:14 +1000 Subject: [PATCH 3/5] Revert "DOC: Start 0.250 whatsnew #22759" This reverts commit b8e9365e599e6a4fe5c56925199a57183d0c8b73. --- doc/source/whatsnew.rst | 2 - doc/source/whatsnew/v0.25.0.txt | 178 -------------------------------- 2 files changed, 180 deletions(-) delete mode 100644 doc/source/whatsnew/v0.25.0.txt diff --git a/doc/source/whatsnew.rst b/doc/source/whatsnew.rst index 6a661c5f9dd15..8672685b3ebb4 100644 --- a/doc/source/whatsnew.rst +++ b/doc/source/whatsnew.rst @@ -18,8 +18,6 @@ What's New These are new features and improvements of note in each release. -.. include:: whatsnew/v0.25.0.txt - .. include:: whatsnew/v0.24.0.txt .. include:: whatsnew/v0.23.4.txt diff --git a/doc/source/whatsnew/v0.25.0.txt b/doc/source/whatsnew/v0.25.0.txt deleted file mode 100644 index f9ca145731231..0000000000000 --- a/doc/source/whatsnew/v0.25.0.txt +++ /dev/null @@ -1,178 +0,0 @@ -.. _whatsnew_0250: - -v0.25.0 (Month XX, 2018) ------------------------- - -.. _whatsnew_0250.enhancements: - -New features -~~~~~~~~~~~~ - -.. _whatsnew_0250.enhancements.other: - -Other Enhancements -^^^^^^^^^^^^^^^^^^ -- :class:`ExcelFormatter` now has a `header_style` property. Was previously in global scope (:issue:`22758`). -- -- - -.. _whatsnew_0250.api_breaking: - - -Backwards incompatible API changes -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -.. _whatsnew_0250.api.other: - -Other API Changes -^^^^^^^^^^^^^^^^^ - -- -- -- - -.. _whatsnew_0250.deprecations: - -Deprecations -~~~~~~~~~~~~ - -- -- -- - -.. _whatsnew_0250.prior_deprecations: - -Removal of prior version deprecations/changes -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -- -- -- - -.. _whatsnew_0250.performance: - -Performance Improvements -~~~~~~~~~~~~~~~~~~~~~~~~ - -- -- -- - -.. _whatsnew_0250.docs: - -Documentation Changes -~~~~~~~~~~~~~~~~~~~~~ - -- -- -- - -.. _whatsnew_0250.bug_fixes: - -Bug Fixes -~~~~~~~~~ - -Categorical -^^^^^^^^^^^ - -- -- -- - -Datetimelike -^^^^^^^^^^^^ - -- -- -- - -Timedelta -^^^^^^^^^ - -- -- -- - -Timezones -^^^^^^^^^ - -- -- -- - -Offsets -^^^^^^^ - -- -- -- - -Numeric -^^^^^^^ - -- -- -- - -Strings -^^^^^^^ - -- -- -- - -Indexing -^^^^^^^^ - -- -- -- - -MultiIndex -^^^^^^^^^^ - -- -- -- - -I/O -^^^ - -- -- -- - -Plotting -^^^^^^^^ - -- -- -- - -Groupby/Resample/Rolling -^^^^^^^^^^^^^^^^^^^^^^^^ - -- -- -- - -Sparse -^^^^^^ - -- -- -- - -Reshaping -^^^^^^^^^ - -- -- -- - -Other -^^^^^ - -- -- -- From 37e187cbd402b8367c4add4986b22697b645d794 Mon Sep 17 00:00:00 2001 From: Danny Kim Date: Wed, 19 Sep 2018 23:48:36 +1000 Subject: [PATCH 4/5] DOC: Updating whatsnew v0.24.0 to reflect that header_style is now a property of ExcelFormatter #22758 --- doc/source/whatsnew/v0.24.0.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 3a44b0260153c..739f6b3f090fc 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -185,6 +185,7 @@ Other Enhancements - :class:`Resampler` now is iterable like :class:`GroupBy` (:issue:`15314`). - :meth:`Series.resample` and :meth:`DataFrame.resample` have gained the :meth:`Resampler.quantile` (:issue:`15023`). - :meth:`Index.to_frame` now supports overriding column name(s) (:issue:`22580`). +- :class:`ExcelFormatter` now has a `header_style` property. Was previously in global scope (:issue:`22758`). .. _whatsnew_0240.api_breaking: From 2f1c6df8bd25adfcf70f9dbe3a240519583ab67e Mon Sep 17 00:00:00 2001 From: Danny Kim Date: Thu, 20 Sep 2018 06:49:56 +1000 Subject: [PATCH 5/5] Revert "DOC: Updating whatsnew v0.24.0 to reflect that header_style is now a property of ExcelFormatter #22758" This reverts commit 37e187cbd402b8367c4add4986b22697b645d794. --- doc/source/whatsnew/v0.24.0.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 739f6b3f090fc..3a44b0260153c 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -185,7 +185,6 @@ Other Enhancements - :class:`Resampler` now is iterable like :class:`GroupBy` (:issue:`15314`). - :meth:`Series.resample` and :meth:`DataFrame.resample` have gained the :meth:`Resampler.quantile` (:issue:`15023`). - :meth:`Index.to_frame` now supports overriding column name(s) (:issue:`22580`). -- :class:`ExcelFormatter` now has a `header_style` property. Was previously in global scope (:issue:`22758`). .. _whatsnew_0240.api_breaking: