From 7e30dfed5fa8202cb30a23cbc1d74576d54e72d9 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Tue, 10 Aug 2021 11:46:31 +0200 Subject: [PATCH 01/17] max rows --- pandas/core/config_init.py | 26 ++++++++++++++ pandas/io/formats/style.py | 9 +++++ pandas/io/formats/style_render.py | 56 ++++++++++++++++++++++++++----- 3 files changed, 83 insertions(+), 8 deletions(-) diff --git a/pandas/core/config_init.py b/pandas/core/config_init.py index 27b898782fbef..97fb44b50141b 100644 --- a/pandas/core/config_init.py +++ b/pandas/core/config_init.py @@ -762,6 +762,18 @@ def register_converter_cb(key): trimming will occur over columns, rows or both if needed. """ +styler_max_rows = """ +: int + The maximum number of rows that will be rendered. May still be reduced to + satsify ``max_elements``, which takes precedence. +""" + +styler_max_cols = """ +: int + The maximum number of columns that will be rendered. May still be reduced to + satsify ``max_elements``, which takes precedence. +""" + with cf.config_prefix("styler"): cf.register_option("sparse.index", True, styler_sparse_index_doc, validator=bool) @@ -775,3 +787,17 @@ def register_converter_cb(key): styler_max_elements, validator=is_nonnegative_int, ) + + cf.register_option( + "render.max_rows", + None, + styler_max_rows, + validator=is_nonnegative_int, + ) + + cf.register_option( + "render.max_cols", + None, + styler_max_cols, + validator=is_nonnegative_int, + ) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 1a891d76a376c..29081fc309043 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -828,6 +828,8 @@ def to_html( table_attributes: str | None = None, sparse_index: bool | None = None, sparse_columns: bool | None = None, + max_rows: int | None = None, + max_cols: int | None = None, encoding: str | None = None, doctype_html: bool = False, exclude_styles: bool = False, @@ -864,6 +866,11 @@ def to_html( will display each explicit level element in a hierarchical key for each column. Defaults to ``pandas.options.styler.sparse.columns`` value. + .. versionadded:: 1.4.0 + max_rows, max_cols : int, optional + The maximum rows and/or columns that will be rendered. Defaults to + ``pandas.options.styler.render.max_rows/max_cols``. + .. versionadded:: 1.4.0 encoding : str, optional Character encoding setting for file output, and HTML meta tags, @@ -900,6 +907,8 @@ def to_html( html = self._render_html( sparse_index=sparse_index, sparse_columns=sparse_columns, + max_rows=max_rows, + max_cols=max_cols, exclude_styles=exclude_styles, encoding=encoding if encoding else "utf-8", doctype_html=doctype_html, diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py index e89d4519543c6..b448b295f0708 100644 --- a/pandas/io/formats/style_render.py +++ b/pandas/io/formats/style_render.py @@ -110,14 +110,21 @@ def __init__( tuple[int, int], Callable[[Any], str] ] = defaultdict(lambda: partial(_default_formatter, precision=def_precision)) - def _render_html(self, sparse_index: bool, sparse_columns: bool, **kwargs) -> str: + def _render_html( + self, + sparse_index: bool, + sparse_columns: bool, + max_rows: int | None = None, + max_cols: int | None = None, + **kwargs, + ) -> str: """ Renders the ``Styler`` including all applied styles to HTML. Generates a dict with necessary kwargs passed to jinja2 template. """ self._compute() # TODO: namespace all the pandas keys - d = self._translate(sparse_index, sparse_columns) + d = self._translate(sparse_index, sparse_columns, max_rows, max_cols) d.update(kwargs) return self.template_html.render( **d, @@ -157,7 +164,14 @@ def _compute(self): r = func(self)(*args, **kwargs) return r - def _translate(self, sparse_index: bool, sparse_cols: bool, blank: str = " "): + def _translate( + self, + sparse_index: bool, + sparse_cols: bool, + max_rows: int | None = None, + max_cols: int | None = None, + blank: str = " ", + ): """ Process Styler data and settings into a dict for template rendering. @@ -172,6 +186,10 @@ def _translate(self, sparse_index: bool, sparse_cols: bool, blank: str = "  sparse_cols : bool Whether to sparsify the columns or print all hierarchical column elements. Upstream defaults are typically to `pandas.options.styler.sparse.columns`. + blank : str + Entry to top-left blank cells. + max_rows, max_cols : int, optional + Specific max rows and cols. max_elements always take precedence in render. Returns ------- @@ -197,8 +215,14 @@ def _translate(self, sparse_index: bool, sparse_cols: bool, blank: str = "  } max_elements = get_option("styler.render.max_elements") + max_rows = max_rows if max_rows else get_option("styler.render.max_rows") + max_cols = max_cols if max_cols else get_option("styler.render.max_cols") max_rows, max_cols = _get_trimming_maximums( - len(self.data.index), len(self.data.columns), max_elements + len(self.data.index), + len(self.data.columns), + max_elements, + max_rows, + max_cols, ) head = self._translate_header( @@ -772,16 +796,27 @@ def _element( } -def _get_trimming_maximums(rn, cn, max_elements, scaling_factor=0.8): +def _get_trimming_maximums( + rn, + cn, + max_elements, + max_rows=None, + max_cols=None, + scaling_factor=0.8, +): """ - Recursively reduce the number of rows and columns to satisfy max elements. + Recursively reduce the number of rows and columns to satisfy max_elements. Parameters ---------- rn, cn : int - The number of input rows / columns + The number of input rows / columns. max_elements : int - The number of allowable elements + The number of allowable elements. + max_rows, max_cols : int, optional + Directly specify an initial maximum rows or columns before compression. + scaling_factor : float + Factor at which to reduce the number of rows / columns to fit. Returns ------- @@ -795,6 +830,11 @@ def scale_down(rn, cn): else: return int(rn * scaling_factor), cn + if max_rows: + rn = max_rows if rn > max_rows else rn + if max_cols: + cn = max_cols if cn > max_cols else cn + while rn * cn > max_elements: rn, cn = scale_down(rn, cn) From 4f78bc497660bfcfa3a99fbeb9aa087e0f50e6c4 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Tue, 10 Aug 2021 16:42:11 +0200 Subject: [PATCH 02/17] addt to docs --- doc/source/user_guide/options.rst | 2 ++ pandas/io/formats/style_render.py | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/doc/source/user_guide/options.rst b/doc/source/user_guide/options.rst index 62a347acdaa34..d16e05726c3fa 100644 --- a/doc/source/user_guide/options.rst +++ b/doc/source/user_guide/options.rst @@ -489,6 +489,8 @@ styler.sparse.columns True "Sparsify" MultiIndex displ in Styler output. styler.render.max_elements 262144 Maximum number of datapoints that Styler will render trimming either rows, columns or both to fit. +styler.render.max_rows None Maximum number of rows that Styler will render. +styler.render.max_cols None Maximum number of columns that Styler will render. ======================================= ============ ================================== diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py index b448b295f0708..9c6132b35d988 100644 --- a/pandas/io/formats/style_render.py +++ b/pandas/io/formats/style_render.py @@ -805,14 +805,14 @@ def _get_trimming_maximums( scaling_factor=0.8, ): """ - Recursively reduce the number of rows and columns to satisfy max_elements. + Recursively reduce the number of rows and columns to satisfy max elements. Parameters ---------- rn, cn : int - The number of input rows / columns. + The number of input rows / columns max_elements : int - The number of allowable elements. + The number of allowable elements max_rows, max_cols : int, optional Directly specify an initial maximum rows or columns before compression. scaling_factor : float From 3ccdfd7c9f1e32c3c89961533d726b3fd6b9e551 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Tue, 10 Aug 2021 18:28:04 +0200 Subject: [PATCH 03/17] add test --- pandas/tests/io/formats/style/test_style.py | 32 ++++++++++++++++----- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/pandas/tests/io/formats/style/test_style.py b/pandas/tests/io/formats/style/test_style.py index 3c042e130981c..8ac345e600bab 100644 --- a/pandas/tests/io/formats/style/test_style.py +++ b/pandas/tests/io/formats/style/test_style.py @@ -153,20 +153,38 @@ def test_trimming_maximum(): assert (rn, cn) == (250, 3) -def test_render_trimming(): +@pytest.mark.parametrize( + "option, val", + [ + ("styler.render.max_elements", 6), + ("styler.render.max_rows", 3), + ], +) +def test_render_trimming_rows(option, val): + # test auto and specific trimming of rows df = DataFrame(np.arange(120).reshape(60, 2)) - with pd.option_context("styler.render.max_elements", 6): + with pd.option_context(option, val): ctx = df.style._translate(True, True) assert len(ctx["head"][0]) == 3 # index + 2 data cols assert len(ctx["body"]) == 4 # 3 data rows + trimming row assert len(ctx["body"][0]) == 3 # index + 2 data cols - df = DataFrame(np.arange(120).reshape(12, 10)) - with pd.option_context("styler.render.max_elements", 6): + +@pytest.mark.parametrize( + "option, val", + [ + ("styler.render.max_elements", 6), + ("styler.render.max_cols", 2), + ], +) +def test_render_trimming_cols(option, val): + # test auto and specific trimming of cols + df = DataFrame(np.arange(30).reshape(3, 10)) + with pd.option_context(option, val): ctx = df.style._translate(True, True) - assert len(ctx["head"][0]) == 4 # index + 2 data cols + trimming row - assert len(ctx["body"]) == 4 # 3 data rows + trimming row - assert len(ctx["body"][0]) == 4 # index + 2 data cols + trimming row + assert len(ctx["head"][0]) == 4 # index + 2 data cols + trimming col + assert len(ctx["body"]) == 3 # 3 data rows + assert len(ctx["body"][0]) == 4 # index + 2 data cols + trimming col def test_render_trimming_mi(): From 7bd12e09ae3b903d8518eaf35dddef36bccdcad4 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Tue, 10 Aug 2021 18:40:10 +0200 Subject: [PATCH 04/17] add test --- pandas/tests/io/formats/style/test_style.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pandas/tests/io/formats/style/test_style.py b/pandas/tests/io/formats/style/test_style.py index 8ac345e600bab..d2cb900b9d404 100644 --- a/pandas/tests/io/formats/style/test_style.py +++ b/pandas/tests/io/formats/style/test_style.py @@ -152,6 +152,9 @@ def test_trimming_maximum(): rn, cn = _get_trimming_maximums(1000, 3, 750, scaling_factor=0.5) assert (rn, cn) == (250, 3) + rn, cn = _get_trimming_maximums(100, 100, 200, 4, 6, scaling_factor=0.5) + assert (rn, cn) == (4, 6) + @pytest.mark.parametrize( "option, val", From adbefdde8babc45a4d087aef7902218002e04ceb Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Tue, 10 Aug 2021 19:42:00 +0200 Subject: [PATCH 05/17] add test --- pandas/tests/io/formats/style/test_html.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pandas/tests/io/formats/style/test_html.py b/pandas/tests/io/formats/style/test_html.py index 9983017652919..83ff0a5daf07f 100644 --- a/pandas/tests/io/formats/style/test_html.py +++ b/pandas/tests/io/formats/style/test_html.py @@ -400,3 +400,16 @@ def test_sparse_options(sparse_index, sparse_columns): assert (html1 == default_html) is (sparse_index and sparse_columns) html2 = styler.to_html(sparse_index=sparse_index, sparse_columns=sparse_columns) assert html1 == html2 + + +@pytest.mark.parametrize("rows", [True, False]) +@pytest.mark.parametrize("cols", [True, False]) +def test_maximums(styler_mi, rows, cols): + result = styler_mi.to_html( + max_rows=2 if rows else None, + max_cols=2 if cols else None, + ) + + assert ">5" in result # [[0,1], [4,5]] always visible + assert (">8" in result) is not rows # first trimmed vertical element + assert (">2" in result) is not cols # first trimmed horizontal element From e6eaa69cb17d1e6a61758e8d6edd0699c0a215da Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Tue, 10 Aug 2021 19:47:12 +0200 Subject: [PATCH 06/17] doc edit --- pandas/core/config_init.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/config_init.py b/pandas/core/config_init.py index 97fb44b50141b..159008dedb355 100644 --- a/pandas/core/config_init.py +++ b/pandas/core/config_init.py @@ -763,13 +763,13 @@ def register_converter_cb(key): """ styler_max_rows = """ -: int +: int, optional The maximum number of rows that will be rendered. May still be reduced to satsify ``max_elements``, which takes precedence. """ styler_max_cols = """ -: int +: int, optional The maximum number of columns that will be rendered. May still be reduced to satsify ``max_elements``, which takes precedence. """ From 5f1406eda4a837cac22730d283616add2468f502 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Wed, 11 Aug 2021 07:39:54 +0200 Subject: [PATCH 07/17] change cols -->> columns --- doc/source/user_guide/options.rst | 2 +- pandas/core/config_init.py | 6 +++--- pandas/io/formats/style.py | 6 +++--- pandas/io/formats/style_render.py | 2 +- pandas/tests/io/formats/style/test_html.py | 2 +- pandas/tests/io/formats/style/test_style.py | 2 +- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/doc/source/user_guide/options.rst b/doc/source/user_guide/options.rst index d16e05726c3fa..667cb62fde120 100644 --- a/doc/source/user_guide/options.rst +++ b/doc/source/user_guide/options.rst @@ -490,7 +490,7 @@ styler.sparse.columns True "Sparsify" MultiIndex displ styler.render.max_elements 262144 Maximum number of datapoints that Styler will render trimming either rows, columns or both to fit. styler.render.max_rows None Maximum number of rows that Styler will render. -styler.render.max_cols None Maximum number of columns that Styler will render. +styler.render.max_columns None Maximum number of columns that Styler will render. ======================================= ============ ================================== diff --git a/pandas/core/config_init.py b/pandas/core/config_init.py index 159008dedb355..52b6c53e6efec 100644 --- a/pandas/core/config_init.py +++ b/pandas/core/config_init.py @@ -768,7 +768,7 @@ def register_converter_cb(key): satsify ``max_elements``, which takes precedence. """ -styler_max_cols = """ +styler_max_columns = """ : int, optional The maximum number of columns that will be rendered. May still be reduced to satsify ``max_elements``, which takes precedence. @@ -796,8 +796,8 @@ def register_converter_cb(key): ) cf.register_option( - "render.max_cols", + "render.max_columns", None, - styler_max_cols, + styler_max_columns, validator=is_nonnegative_int, ) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 29081fc309043..936b2d5be0ec4 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -829,7 +829,7 @@ def to_html( sparse_index: bool | None = None, sparse_columns: bool | None = None, max_rows: int | None = None, - max_cols: int | None = None, + max_columns: int | None = None, encoding: str | None = None, doctype_html: bool = False, exclude_styles: bool = False, @@ -867,7 +867,7 @@ def to_html( column. Defaults to ``pandas.options.styler.sparse.columns`` value. .. versionadded:: 1.4.0 - max_rows, max_cols : int, optional + max_rows, max_columns : int, optional The maximum rows and/or columns that will be rendered. Defaults to ``pandas.options.styler.render.max_rows/max_cols``. @@ -908,7 +908,7 @@ def to_html( sparse_index=sparse_index, sparse_columns=sparse_columns, max_rows=max_rows, - max_cols=max_cols, + max_cols=max_columns, exclude_styles=exclude_styles, encoding=encoding if encoding else "utf-8", doctype_html=doctype_html, diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py index 9c6132b35d988..7c99bf4d37ccf 100644 --- a/pandas/io/formats/style_render.py +++ b/pandas/io/formats/style_render.py @@ -216,7 +216,7 @@ def _translate( max_elements = get_option("styler.render.max_elements") max_rows = max_rows if max_rows else get_option("styler.render.max_rows") - max_cols = max_cols if max_cols else get_option("styler.render.max_cols") + max_cols = max_cols if max_cols else get_option("styler.render.max_columns") max_rows, max_cols = _get_trimming_maximums( len(self.data.index), len(self.data.columns), diff --git a/pandas/tests/io/formats/style/test_html.py b/pandas/tests/io/formats/style/test_html.py index 83ff0a5daf07f..866c7c17aa49e 100644 --- a/pandas/tests/io/formats/style/test_html.py +++ b/pandas/tests/io/formats/style/test_html.py @@ -407,7 +407,7 @@ def test_sparse_options(sparse_index, sparse_columns): def test_maximums(styler_mi, rows, cols): result = styler_mi.to_html( max_rows=2 if rows else None, - max_cols=2 if cols else None, + max_columns=2 if cols else None, ) assert ">5" in result # [[0,1], [4,5]] always visible diff --git a/pandas/tests/io/formats/style/test_style.py b/pandas/tests/io/formats/style/test_style.py index d2cb900b9d404..34a4118b422cd 100644 --- a/pandas/tests/io/formats/style/test_style.py +++ b/pandas/tests/io/formats/style/test_style.py @@ -177,7 +177,7 @@ def test_render_trimming_rows(option, val): "option, val", [ ("styler.render.max_elements", 6), - ("styler.render.max_cols", 2), + ("styler.render.max_columns", 2), ], ) def test_render_trimming_cols(option, val): From b25a191e169c2191413f6228ed7b018f829883c5 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Wed, 11 Aug 2021 07:40:53 +0200 Subject: [PATCH 08/17] change cols -->> columns --- pandas/io/formats/style.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 936b2d5be0ec4..91947fc244da2 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -869,7 +869,7 @@ def to_html( .. versionadded:: 1.4.0 max_rows, max_columns : int, optional The maximum rows and/or columns that will be rendered. Defaults to - ``pandas.options.styler.render.max_rows/max_cols``. + ``pandas.options.styler.render.max_rows/max_columns``. .. versionadded:: 1.4.0 encoding : str, optional From c7e7e55d1930afbf21787a4b5ccf4406e15802a8 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Mon, 23 Aug 2021 15:00:13 +0200 Subject: [PATCH 09/17] add to docs --- doc/source/user_guide/options.rst | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/doc/source/user_guide/options.rst b/doc/source/user_guide/options.rst index 667cb62fde120..b8099c673fbc5 100644 --- a/doc/source/user_guide/options.rst +++ b/doc/source/user_guide/options.rst @@ -489,8 +489,10 @@ styler.sparse.columns True "Sparsify" MultiIndex displ in Styler output. styler.render.max_elements 262144 Maximum number of datapoints that Styler will render trimming either rows, columns or both to fit. -styler.render.max_rows None Maximum number of rows that Styler will render. -styler.render.max_columns None Maximum number of columns that Styler will render. +styler.render.max_rows None Maximum number of rows that Styler will render. By default + this is dynamic based on ``max_elements``. +styler.render.max_columns None Maximum number of columns that Styler will render. By default + this is dynamic based on ``max_elements``. ======================================= ============ ================================== From 9a889cbce9a3a5843b9112f34e3ce535c9afe957 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sun, 29 Aug 2021 09:30:18 +0200 Subject: [PATCH 10/17] whats new --- doc/source/whatsnew/v1.4.0.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 450ecc85c725b..25d724838aaf9 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -73,8 +73,9 @@ Styler - Styling of indexing has been added, with :meth:`.Styler.apply_index` and :meth:`.Styler.applymap_index`. These mirror the signature of the methods already used to style data values, and work with both HTML and LaTeX format (:issue:`41893`). - :meth:`.Styler.bar` introduces additional arguments to control alignment and display (:issue:`26070`, :issue:`36419`), and it also validates the input arguments ``width`` and ``height`` (:issue:`42511`). - :meth:`.Styler.to_latex` introduces keyword argument ``environment``, which also allows a specific "longtable" entry through a separate jinja2 template (:issue:`41866`). - - :meth:`.Styler.to_html` introduces keyword arguments ``sparse_index`` and ``sparse_columns`` (:issue:`41946`) + - :meth:`.Styler.to_html` introduces keyword arguments ``sparse_index``, ``sparse_columns``, ``max_rows`` and ``max_columns`` (:issue:`41946` :issue:`42972`) - Keyword argument ``level`` is added to :meth:`.Styler.hide_index` and :meth:`.Styler.hide_columns` for optionally controlling hidden levels in a MultiIndex (:issue:`25475`) + - Global options have been extended to configure default ``Styler`` properties including maximum display options (:issue:`41395`) There are also bug fixes and deprecations listed below. From 0c0671693c0e1288bd888de035f6b9d83464bd77 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Tue, 31 Aug 2021 13:26:06 +0200 Subject: [PATCH 11/17] typing fix --- pandas/io/formats/style_render.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py index 9217fa8c0e792..994084cc71e50 100644 --- a/pandas/io/formats/style_render.py +++ b/pandas/io/formats/style_render.py @@ -844,7 +844,7 @@ def _get_trimming_maximums( max_rows=None, max_cols=None, scaling_factor=0.8, -): +) -> tuple[int, int]: """ Recursively reduce the number of rows and columns to satisfy max elements. From 5ff4b4761b49efae4870062d4d054d67489a0db7 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Tue, 7 Sep 2021 17:59:50 +0200 Subject: [PATCH 12/17] doc fix --- pandas/io/formats/style.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 8118ef3afd8c3..debaeaf97b887 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -930,10 +930,19 @@ def to_html( Set, or overwrite, the caption on Styler before rendering. .. versionadded:: 1.4.0 - max_rows, max_columns : int, optional - The maximum rows and/or columns that will be rendered. Defaults to + max_rows : int, optional + The maximum number of rows that will be rendered. Defaults to ``pandas.options.styler.render.max_rows/max_columns``. + .. versionadded:: 1.4.0 + max_columns : int, optional + The maximum number of columns that will be rendered. Defaults to + ``pandas.options.styler.render.max_columns``, which is None. + + Rows and columns may be reduced if the number of total elements is + large. This value is set to ``pandas.options.styler.render.max_elements``, + which is 262144 (18 bit browser rendering). + .. versionadded:: 1.4.0 encoding : str, optional Character encoding setting for file output, and HTML meta tags. From 038ba17c00340129d93f1b8b820c927790d0b124 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Tue, 7 Sep 2021 18:13:36 +0200 Subject: [PATCH 13/17] tests expand --- pandas/tests/io/formats/style/test_style.py | 25 +++++++++++++-------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/pandas/tests/io/formats/style/test_style.py b/pandas/tests/io/formats/style/test_style.py index b7c35fdd711d4..295a07049c280 100644 --- a/pandas/tests/io/formats/style/test_style.py +++ b/pandas/tests/io/formats/style/test_style.py @@ -150,15 +150,22 @@ def test_mi_styler_sparsify_options(mi_styler): assert html1 != html2 -def test_trimming_maximum(): - rn, cn = _get_trimming_maximums(100, 100, 100, scaling_factor=0.5) - assert (rn, cn) == (12, 6) - - rn, cn = _get_trimming_maximums(1000, 3, 750, scaling_factor=0.5) - assert (rn, cn) == (250, 3) - - rn, cn = _get_trimming_maximums(100, 100, 200, 4, 6, scaling_factor=0.5) - assert (rn, cn) == (4, 6) +@pytest.mark.parametrize( + "rn, cn, max_els, max_rows, max_cols, exp_rn, exp_cn", + [ + (100, 100, 100, None, None, 12, 6), # reduce to (12, 6) < 100 elements + (1000, 3, 750, None, None, 250, 3), # dynamically reduce rows to 250, keep cols + (4, 1000, 500, None, None, 4, 125), # dynamically reduce cols to 125, keep rows + (1000, 3, 750, 10, None, 10, 3), # overwrite above dynamics with max_row + (4, 1000, 500, None, 5, 4, 5), # overwrite above dynamics with max_col + (100, 100, 700, 50, 50, 25, 25), # rows cols below given maxes so < 700 elmts + ], +) +def test_trimming_maximum(rn, cn, max_els, max_rows, max_cols, exp_rn, exp_cn): + rn, cn = _get_trimming_maximums( + rn, cn, max_els, max_rows, max_cols, scaling_factor=0.5 + ) + assert (rn, cn) == (exp_rn, exp_cn) @pytest.mark.parametrize( From b8638fec391d93dd505a119bc61c0abcecef0a82 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Wed, 8 Sep 2021 00:02:25 +0200 Subject: [PATCH 14/17] fix tests due to conflict in substring names --- doc/source/user_guide/options.rst | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/doc/source/user_guide/options.rst b/doc/source/user_guide/options.rst index 16861eac078e1..59319fda8045c 100644 --- a/doc/source/user_guide/options.rst +++ b/doc/source/user_guide/options.rst @@ -38,11 +38,11 @@ and so passing in a substring will work - as long as it is unambiguous: .. ipython:: python - pd.get_option("display.max_rows") - pd.set_option("display.max_rows", 101) - pd.get_option("display.max_rows") - pd.set_option("max_r", 102) - pd.get_option("display.max_rows") + pd.get_option("display.chop_threshold") + pd.set_option("display.chop_threshold", 2) + pd.get_option("display.chop_threshold") + pd.set_option("chop", 4) + pd.get_option("display.chop_threshold") The following will **not work** because it matches multiple option names, e.g. @@ -52,7 +52,7 @@ The following will **not work** because it matches multiple option names, e.g. :okexcept: try: - pd.get_option("column") + pd.get_option("max") except KeyError as e: print(e) @@ -153,27 +153,27 @@ lines are replaced by an ellipsis. .. ipython:: python df = pd.DataFrame(np.random.randn(7, 2)) - pd.set_option("max_rows", 7) + pd.set_option("display.max_rows", 7) df - pd.set_option("max_rows", 5) + pd.set_option("display.max_rows", 5) df - pd.reset_option("max_rows") + pd.reset_option("display.max_rows") Once the ``display.max_rows`` is exceeded, the ``display.min_rows`` options determines how many rows are shown in the truncated repr. .. ipython:: python - pd.set_option("max_rows", 8) - pd.set_option("min_rows", 4) + pd.set_option("display.max_rows", 8) + pd.set_option("display.min_rows", 4) # below max_rows -> all rows shown df = pd.DataFrame(np.random.randn(7, 2)) df # above max_rows -> only min_rows (4) rows shown df = pd.DataFrame(np.random.randn(9, 2)) df - pd.reset_option("max_rows") - pd.reset_option("min_rows") + pd.reset_option("display.max_rows") + pd.reset_option("display.min_rows") ``display.expand_frame_repr`` allows for the representation of dataframes to stretch across pages, wrapped over the full column vs row-wise. @@ -193,13 +193,13 @@ dataframes to stretch across pages, wrapped over the full column vs row-wise. .. ipython:: python df = pd.DataFrame(np.random.randn(10, 10)) - pd.set_option("max_rows", 5) + pd.set_option("display.max_rows", 5) pd.set_option("large_repr", "truncate") df pd.set_option("large_repr", "info") df pd.reset_option("large_repr") - pd.reset_option("max_rows") + pd.reset_option("display.max_rows") ``display.max_colwidth`` sets the maximum width of columns. Cells of this length or longer will be truncated with an ellipsis. From c5263431d0194d20954175df53b8eb051c3f4f43 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Wed, 8 Sep 2021 20:02:30 +0200 Subject: [PATCH 15/17] fix tests --- pandas/tests/io/formats/test_format.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index 500f8bf5ff159..c3811309987f8 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -542,25 +542,25 @@ def test_auto_detect(self): index = range(10) df = DataFrame(index=index, columns=cols) with option_context("mode.sim_interactive", True): - with option_context("max_rows", None): + with option_context("display.max_rows", None): with option_context("max_columns", None): # Wrap around with None assert has_expanded_repr(df) - with option_context("max_rows", 0): + with option_context("display.max_rows", 0): with option_context("max_columns", 0): # Truncate with auto detection. assert has_horizontally_truncated_repr(df) index = range(int(term_height * fac)) df = DataFrame(index=index, columns=cols) - with option_context("max_rows", 0): + with option_context("display.max_rows", 0): with option_context("max_columns", None): # Wrap around with None assert has_expanded_repr(df) # Truncate vertically assert has_vertically_truncated_repr(df) - with option_context("max_rows", None): + with option_context("display.max_rows", None): with option_context("max_columns", 0): assert has_horizontally_truncated_repr(df) From 3a6dd5881a996dfe14e04b46ce6dadb08c53bf90 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Wed, 8 Sep 2021 21:22:17 +0200 Subject: [PATCH 16/17] fix tests --- pandas/tests/io/formats/test_format.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index c3811309987f8..95da68510be6b 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -543,25 +543,25 @@ def test_auto_detect(self): df = DataFrame(index=index, columns=cols) with option_context("mode.sim_interactive", True): with option_context("display.max_rows", None): - with option_context("max_columns", None): + with option_context("display.max_columns", None): # Wrap around with None assert has_expanded_repr(df) with option_context("display.max_rows", 0): - with option_context("max_columns", 0): + with option_context("display.max_columns", 0): # Truncate with auto detection. assert has_horizontally_truncated_repr(df) index = range(int(term_height * fac)) df = DataFrame(index=index, columns=cols) with option_context("display.max_rows", 0): - with option_context("max_columns", None): + with option_context("display.max_columns", None): # Wrap around with None assert has_expanded_repr(df) # Truncate vertically assert has_vertically_truncated_repr(df) with option_context("display.max_rows", None): - with option_context("max_columns", 0): + with option_context("display.max_columns", 0): assert has_horizontally_truncated_repr(df) def test_to_string_repr_unicode(self): From 5d6c6d8f8a250b9364c4a83f07c1a19856097f03 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Wed, 8 Sep 2021 22:46:43 +0200 Subject: [PATCH 17/17] fix tests --- pandas/tests/series/test_repr.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/series/test_repr.py b/pandas/tests/series/test_repr.py index 0d5c3bc21c609..555342dd39005 100644 --- a/pandas/tests/series/test_repr.py +++ b/pandas/tests/series/test_repr.py @@ -169,7 +169,7 @@ def test_repr_should_return_str(self): def test_repr_max_rows(self): # GH 6863 - with option_context("max_rows", None): + with option_context("display.max_rows", None): str(Series(range(1001))) # should not raise exception def test_unicode_string_with_unicode(self):