From 499d3d9018830a1e8fd03c148219ce1405427634 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Tue, 31 Aug 2021 13:55:10 +0200 Subject: [PATCH 1/4] bug fix and test --- pandas/io/formats/style_render.py | 4 +++- pandas/tests/io/formats/style/test_style.py | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py index 4f1e98225aafe..d812fe59f05a2 100644 --- a/pandas/io/formats/style_render.py +++ b/pandas/io/formats/style_render.py @@ -393,7 +393,9 @@ def _translate_header( for c, name in enumerate(self.data.index.names) ] - if len(self.data.columns) <= max_cols: + if not clabels: + blank_len = 0 + elif len(self.data.columns) <= max_cols: blank_len = len(clabels[0]) else: blank_len = len(clabels[0]) + 1 # to allow room for `...` trim col diff --git a/pandas/tests/io/formats/style/test_style.py b/pandas/tests/io/formats/style/test_style.py index 5022a1eaa2c6e..e81ed39f90187 100644 --- a/pandas/tests/io/formats/style/test_style.py +++ b/pandas/tests/io/formats/style/test_style.py @@ -187,6 +187,12 @@ def test_render_trimming_mi(): assert {"attributes": 'colspan="2"'}.items() <= ctx["head"][0][2].items() +def test_render_empty_mi(): + # GH 43305 + df = DataFrame(index=MultiIndex.from_arrays([["A", "A"], [1, 2]])) + df.style.to_html() + + @pytest.mark.parametrize("comprehensive", [True, False]) @pytest.mark.parametrize("render", [True, False]) @pytest.mark.parametrize("deepcopy", [True, False]) From 32895c590a5830e934e6f01a3697aa2d475a1678 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Tue, 31 Aug 2021 14:09:52 +0200 Subject: [PATCH 2/4] bug fix and test --- pandas/tests/io/formats/style/test_style.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/io/formats/style/test_style.py b/pandas/tests/io/formats/style/test_style.py index e81ed39f90187..762f6beba2b9e 100644 --- a/pandas/tests/io/formats/style/test_style.py +++ b/pandas/tests/io/formats/style/test_style.py @@ -189,7 +189,7 @@ def test_render_trimming_mi(): def test_render_empty_mi(): # GH 43305 - df = DataFrame(index=MultiIndex.from_arrays([["A", "A"], [1, 2]])) + df = DataFrame(index=MultiIndex.from_product([["A"], [0, 1]], names=[None, "one"])) df.style.to_html() From 0d043fe768b811f42ff1a59ee1947d7c3161a77e Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Tue, 31 Aug 2021 14:16:39 +0200 Subject: [PATCH 3/4] whats new --- doc/source/whatsnew/v1.4.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index be647e344f270..b28b4a663ba54 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -381,7 +381,7 @@ Styler - Bug in :meth:`.Styler.to_html` where the ``Styler`` object was updated if the ``to_html`` method was called with some args (:issue:`43034`) - Bug in :meth:`.Styler.copy` where ``uuid`` was not previously copied (:issue:`40675`) - Bug in :meth:`Styler.apply` where functions which returned Series objects were not correctly handled in terms of aligning their index labels (:issue:`13657`, :issue:`42014`) -- +- Bug when rendering an empty DataFrame with a named index (:issue:`43305`). Other ^^^^^ From ee0e38eb5e899dcfdd1a063674bad91828dcddcb Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Tue, 31 Aug 2021 15:18:53 +0200 Subject: [PATCH 4/4] expected --- pandas/tests/io/formats/style/test_style.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/pandas/tests/io/formats/style/test_style.py b/pandas/tests/io/formats/style/test_style.py index 762f6beba2b9e..352f00292ce24 100644 --- a/pandas/tests/io/formats/style/test_style.py +++ b/pandas/tests/io/formats/style/test_style.py @@ -1,5 +1,6 @@ import copy import re +from textwrap import dedent import numpy as np import pytest @@ -190,7 +191,18 @@ def test_render_trimming_mi(): def test_render_empty_mi(): # GH 43305 df = DataFrame(index=MultiIndex.from_product([["A"], [0, 1]], names=[None, "one"])) - df.style.to_html() + expected = dedent( + """\ + > + + +   + one + + + """ + ) + assert expected in df.style.to_html() @pytest.mark.parametrize("comprehensive", [True, False])