-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: Styler.apply(map)_index
made compatible with Styler.to_excel
#41995
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
Merged
jreback
merged 54 commits into
pandas-dev:master
from
attack68:styler_apply_index_specific_excel
Oct 19, 2021
Merged
Changes from 38 commits
Commits
Show all changes
54 commits
Select commit
Hold shift + click to select a range
ece8855
add apply across index
attack68 a3a88e5
add applymap across index
attack68 066e4f3
improve docs
attack68 50dbcc7
Merge remote-tracking branch 'upstream/master' into styler_apply_inde…
attack68 ef5839f
Merge remote-tracking branch 'upstream/master' into styler_apply_inde…
attack68 5e4c1c0
add column header styling and amend tests
attack68 20ac7e0
doc sharing
attack68 26c5340
doc fix
attack68 2437b72
doc fix
attack68 312a6e6
collapse the cellstyle maps
attack68 553426f
add basic test
attack68 f01dfee
add basic test
attack68 f940165
parametrise tests
attack68 6f5b46c
test for raises ValueError
attack68 75cf6ca
test html working
attack68 d654139
test html working
attack68 728091a
Merge remote-tracking branch 'upstream/master' into styler_apply_inde…
attack68 17787ef
whats new 1.4.0
attack68 e2240f9
make apply_header compatible with to_excel
attack68 6a198d2
whats new
attack68 248de0d
Merge remote-tracking branch 'upstream/master' into styler_apply_inde…
attack68 8bc3e53
Merge branch 'styler_apply_index_specific' into styler_apply_index_sp…
attack68 321458e
Merge remote-tracking branch 'upstream/master' into styler_apply_inde…
attack68 c104c3d
Merge branch 'styler_apply_index_specific' into styler_apply_index_sp…
attack68 13b5fe5
add tests for new cases
attack68 ed58798
Merge remote-tracking branch 'upstream/master' into styler_apply_inde…
attack68 95a9a20
Merge branch 'styler_apply_index_specific' into styler_apply_index_sp…
attack68 8d8e88f
update tests
attack68 e17d766
Merge remote-tracking branch 'upstream/master' into styler_apply_inde…
attack68 c56d736
Merge branch 'styler_apply_index_specific' into styler_apply_index_sp…
attack68 f70bf65
Merge remote-tracking branch 'upstream/master' into styler_apply_inde…
attack68 228246b
Merge branch 'styler_apply_index_specific' into styler_apply_index_sp…
attack68 ec8c59e
Merge remote-tracking branch 'upstream/master' into styler_apply_inde…
attack68 aa6591f
Merge remote-tracking branch 'upstream/master' into styler_apply_inde…
attack68 27a5c7a
Merge remote-tracking branch 'upstream/master' into styler_apply_inde…
attack68 da91d9b
Merge remote-tracking branch 'upstream/master' into styler_apply_inde…
attack68 0832cbe
Merge remote-tracking branch 'upstream/master' into styler_apply_inde…
attack68 c495aae
Merge remote-tracking branch 'upstream/master' into styler_apply_inde…
attack68 96c6261
Merge remote-tracking branch 'upstream/master' into styler_apply_inde…
attack68 889d794
Merge remote-tracking branch 'upstream/master' into styler_apply_inde…
attack68 79eb10c
Merge remote-tracking branch 'upstream/master' into styler_apply_inde…
attack68 d3fd856
excel styler tests
attack68 702a2e0
add tests
attack68 77704cb
add tests
attack68 cd9fe8b
Merge remote-tracking branch 'upstream/master' into styler_apply_inde…
attack68 fe42e80
function
attack68 6391576
function
attack68 ab5e6a0
Merge remote-tracking branch 'upstream/master' into styler_apply_inde…
attack68 ba4340c
new CssExcelCell class
attack68 20d0063
direct args
attack68 0913da9
comment on styler import jinja2 for to_excel
attack68 09600f8
Merge remote-tracking branch 'upstream/master' into styler_apply_inde…
attack68 9f8b8fa
Merge remote-tracking branch 'upstream/master' into styler_apply_inde…
attack68 04eebeb
Merge remote-tracking branch 'upstream/master' into styler_apply_inde…
attack68 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -472,6 +472,7 @@ def __init__( | |
self.na_rep = na_rep | ||
if not isinstance(df, DataFrame): | ||
self.styler = df | ||
self.styler._compute() | ||
df = df.data | ||
if style_converter is None: | ||
style_converter = CSSToExcelConverter() | ||
|
@@ -552,6 +553,9 @@ def _format_header_mi(self) -> Iterable[ExcelCell]: | |
if self.index and isinstance(self.df.index, MultiIndex): | ||
coloffset = len(self.df.index[0]) - 1 | ||
|
||
styles = None if self.styler is None else self.styler.ctx_columns | ||
xlstyle = self.header_style | ||
|
||
if self.merge_cells: | ||
# Format multi-index as a merged cells. | ||
for lnum, name in enumerate(columns.names): | ||
|
@@ -568,11 +572,14 @@ def _format_header_mi(self) -> Iterable[ExcelCell]: | |
values = levels.take(level_codes) | ||
for i, span_val in spans.items(): | ||
spans_multiple_cells = span_val > 1 | ||
if styles: | ||
css = ";".join(a + ":" + str(v) for (a, v) in styles[lnum, i]) | ||
xlstyle = self.style_converter(css) | ||
yield ExcelCell( | ||
row=lnum, | ||
col=coloffset + i + 1, | ||
val=values[i], | ||
style=self.header_style, | ||
style=xlstyle, | ||
mergestart=lnum if spans_multiple_cells else None, | ||
mergeend=( | ||
coloffset + i + span_val if spans_multiple_cells else None | ||
|
@@ -582,7 +589,10 @@ def _format_header_mi(self) -> Iterable[ExcelCell]: | |
# 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, self.header_style) | ||
if styles: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. worth making a function to do this conversion as this is repeated below |
||
css = ";".join(a + ":" + str(v) for (a, v) in styles[lnum, i]) | ||
xlstyle = self.style_converter(css) | ||
yield ExcelCell(lnum, coloffset + i + 1, v, xlstyle) | ||
|
||
self.rowcounter = lnum | ||
|
||
|
@@ -606,10 +616,13 @@ def _format_header_regular(self) -> Iterable[ExcelCell]: | |
else: | ||
colnames = self.header | ||
|
||
styles = None if self.styler is None else self.styler.ctx_columns | ||
xlstyle = self.header_style | ||
for colindex, colname in enumerate(colnames): | ||
yield ExcelCell( | ||
self.rowcounter, colindex + coloffset, colname, self.header_style | ||
) | ||
if styles: | ||
css = ";".join(a + ":" + str(v) for (a, v) in styles[0, colindex]) | ||
xlstyle = self.style_converter(css) | ||
yield ExcelCell(self.rowcounter, colindex + coloffset, colname, xlstyle) | ||
|
||
def _format_header(self) -> Iterable[ExcelCell]: | ||
if isinstance(self.columns, MultiIndex): | ||
|
@@ -667,8 +680,13 @@ def _format_regular_rows(self) -> Iterable[ExcelCell]: | |
if isinstance(self.df.index, PeriodIndex): | ||
index_values = self.df.index.to_timestamp() | ||
|
||
styles = None if self.styler is None else self.styler.ctx_index | ||
xlstyle = self.header_style | ||
for idx, idxval in enumerate(index_values): | ||
yield ExcelCell(self.rowcounter + idx, 0, idxval, self.header_style) | ||
if styles: | ||
css = ";".join(a + ":" + str(v) for (a, v) in styles[idx, 0]) | ||
xlstyle = self.style_converter(css) | ||
yield ExcelCell(self.rowcounter + idx, 0, idxval, xlstyle) | ||
|
||
coloffset = 1 | ||
else: | ||
|
@@ -703,6 +721,9 @@ def _format_hierarchical_rows(self) -> Iterable[ExcelCell]: | |
for cidx, name in enumerate(index_labels): | ||
yield ExcelCell(self.rowcounter - 1, cidx, name, self.header_style) | ||
|
||
styles = None if self.styler is None else self.styler.ctx_index | ||
xlstyle = self.header_style | ||
|
||
if self.merge_cells: | ||
# Format hierarchical rows as merged cells. | ||
level_strs = self.df.index.format( | ||
|
@@ -722,11 +743,16 @@ def _format_hierarchical_rows(self) -> Iterable[ExcelCell]: | |
|
||
for i, span_val in spans.items(): | ||
spans_multiple_cells = span_val > 1 | ||
if styles: | ||
css = ";".join( | ||
a + ":" + str(v) for (a, v) in styles[i, gcolidx] | ||
) | ||
xlstyle = self.style_converter(css) | ||
yield ExcelCell( | ||
row=self.rowcounter + i, | ||
col=gcolidx, | ||
val=values[i], | ||
style=self.header_style, | ||
style=xlstyle, | ||
mergestart=( | ||
self.rowcounter + i + span_val - 1 | ||
if spans_multiple_cells | ||
|
@@ -740,11 +766,16 @@ def _format_hierarchical_rows(self) -> Iterable[ExcelCell]: | |
# Format hierarchical rows with non-merged values. | ||
for indexcolvals in zip(*self.df.index): | ||
for idx, indexcolval in enumerate(indexcolvals): | ||
if styles: | ||
css = ";".join( | ||
a + ":" + str(v) for (a, v) in styles[idx, gcolidx] | ||
) | ||
xlstyle = self.style_converter(css) | ||
yield ExcelCell( | ||
row=self.rowcounter + idx, | ||
col=gcolidx, | ||
val=indexcolval, | ||
style=self.header_style, | ||
style=xlstyle, | ||
) | ||
gcolidx += 1 | ||
|
||
|
@@ -756,20 +787,14 @@ def _has_aliases(self) -> bool: | |
return is_list_like(self.header) | ||
|
||
def _generate_body(self, coloffset: int) -> Iterable[ExcelCell]: | ||
if self.styler is None: | ||
styles = None | ||
else: | ||
styles = self.styler._compute().ctx | ||
if not styles: | ||
styles = None | ||
styles = None if self.styler is None else self.styler.ctx | ||
xlstyle = None | ||
|
||
# Write the body of the frame data series by series. | ||
for colidx in range(len(self.columns)): | ||
series = self.df.iloc[:, colidx] | ||
for i, val in enumerate(series): | ||
if styles is not None: | ||
css = ";".join([a + ":" + str(v) for (a, v) in styles[i, colidx]]) | ||
if styles: | ||
css = ";".join(a + ":" + str(v) for (a, v) in styles[i, colidx]) | ||
xlstyle = self.style_converter(css) | ||
yield ExcelCell(self.rowcounter + i, colidx + coloffset, val, xlstyle) | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this needed? init doesn't do anything really except for set things
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this clause is only called when you do
styler.to_excel
. It needs_compute
to calculate the styles added. Previously, this was done in_generate_body
below, but, since, it is now needed for both body and headers I brought it to the obvious point: initialisation, to avoid having to unnecessarily do it twice.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could maybe move this to
styler.to_excel
and maybe do the pre-calc there before passing to ExcelFormatter. Might be more sensible like that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried it and it didn't work. The new tests failed when using a custom ExcelFormatter, the styles computation needs to be done within ExcelFormatter.