ENH: omit CSSStyle rules for hidden table elements (#43619)
State: closed

- [x] closes #43619
- [x] tests added / passed (but canceled due to another error)
- [x] Ensure all linting tests pass, see [here](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit) for how to run them
- [x] whatsnew entry

I couldn't run all tests, because of the following problem, which isn't related to my changes:
```
pandas/tests/io/test_orc.py terminate called after throwing an instance of 'orc::TimezoneError'
  what():  Can't open /etc/localtime
Fatal Python error: Aborted
``` 

Labels:
name: Performance
description: Memory or execution speed performance
name: Styler
description: conditional formatting using DataFrame.style

Comments:
You don't have to, but might also like to try and solve the same issue for the index or column headers, that might get CSS for ids if `applymap_index(lambda v: "color: blue;")` is used..
> You don't have to, but might also like to try and solve the same issue for the index or column headers, that might get CSS for ids if `applymap_index(lambda v: "color: blue;")` is used..

I also implemented it for column and index headers.
do we have an asv for this? can you show the perf change (and add asv if we don't have).
> do we have an asv for this? can you show the perf change (and add asv if we don't have).
I will have a look at this and make a suggestion for a more general asv that can also test this.
Note that this can also improve browser performance as well, not just pandas performance.
> lgtm. i will take a look at the asvs, but even if the performance improvement is marginal it makes better HTML anyway.

sure, just covering bases
@rendner you can use the code in #43687 to see the rendering performance enhancement. I expect minimal, but the performance in HTML might be better for large dataframes.

To execute it see the contributing to pandas page running the performance test suite: https://pandas.pydata.org/docs/development/contributing_codebase.html

```
  >$ cd asv_bench
  >asv_bench$ asv continuous -f 1.1 upstream/master HEAD -b style.Render -E virtualenv
```

@attack68 Thank you.

>asv continuous -f 1.1 upstream/master HEAD -b style.Render -E virtualenv

The command gave me the following error `Unknown commit upstream/master`.
So I used this one `asv continuous -f 1.1 011c016f6 HEAD -b style.Render -E virtualenv`.
`011c016f6` is the first commit before my changes, which you can verify here: https://github.com/rendner/pandas/commits/enh-43619

Here are the results:
```
       before           after         ratio
     [011c016f]       [fe9e44b9]
     <master>         <enh-43619>
-      21.1±0.2ms       19.0±0.3ms     0.90  io.style.Render.time_apply_format_hide_render(36, 12)
-      75.5±0.8ms       61.6±0.6ms     0.82  io.style.Render.time_apply_format_hide_render(24, 120)
-      40.7±0.4ms       33.0±0.7ms     0.81  io.style.Render.time_apply_format_hide_render(12, 120)
-       110±0.5ms       88.1±0.4ms     0.80  io.style.Render.time_apply_format_hide_render(36, 120)

SOME BENCHMARKS HAVE CHANGED SIGNIFICANTLY.
PERFORMANCE INCREASED.
```
I guess the performance for hidden cells could be further improved by moving the [data_element_visible](https://github.com/rendner/pandas/blob/enh-43619/pandas/io/formats/style_render.py#L596) check at the start of the for loop. And if not visible continue with the next cell. But I'm unsure how much we would gain.
> 
> I guess the performance for hidden cells could be further improved by moving the [data_element_visible](https://github.com/rendner/pandas/blob/enh-43619/pandas/io/formats/style_render.py#L596) check at the start of the for loop. And if not visible continue with the next cell. But I'm unsure how much we would gain.

This is good, don't need anything else. Im surprised it gets this much improvement but the asv does specifically hide almost all elements. @jreback happy to merge?
thanks @rendner