From b6252f3189a913d1b6be5f063f2850e9e0ed5371 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sat, 4 Dec 2021 07:52:40 +0100 Subject: [PATCH 1/4] mi names --- pandas/tests/io/formats/style/test_style.py | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/pandas/tests/io/formats/style/test_style.py b/pandas/tests/io/formats/style/test_style.py index e793857989ac1..1e4f40f3b6e6f 100644 --- a/pandas/tests/io/formats/style/test_style.py +++ b/pandas/tests/io/formats/style/test_style.py @@ -1037,7 +1037,7 @@ def test_get_level_lengths_un_sorted(self): tm.assert_dict_equal(result, expected) def test_mi_sparse_index_names(self): - # TODO this test is verbose can be minimised to more directly target test + # Test the class names and displayed value are correct on rendering MI names df = DataFrame( {"A": [1, 2]}, index=MultiIndex.from_arrays( @@ -1049,28 +1049,19 @@ def test_mi_sparse_index_names(self): expected = [ { "class": "index_name level0", - "value": "idx_level_0", - "type": "th", - "is_visible": True, "display_value": "idx_level_0", }, { "class": "index_name level1", - "value": "idx_level_1", - "type": "th", - "is_visible": True, "display_value": "idx_level_1", }, { "class": "blank col0", - "value": self.blank_value, - "type": "th", - "is_visible": True, "display_value": self.blank_value, }, ] - - assert head == expected + for i, expected_dict in enumerate(expected): + assert expected_dict.items() <= head[i].items() def test_mi_sparse_column_names(self): df = DataFrame( From 4f980f2a27b5005c8a7712bbdb6247f3c0e7fbe6 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sat, 4 Dec 2021 08:21:10 +0100 Subject: [PATCH 2/4] mi names --- pandas/tests/io/formats/style/test_style.py | 67 +++++---------------- 1 file changed, 16 insertions(+), 51 deletions(-) diff --git a/pandas/tests/io/formats/style/test_style.py b/pandas/tests/io/formats/style/test_style.py index 1e4f40f3b6e6f..5dd227bd3e22e 100644 --- a/pandas/tests/io/formats/style/test_style.py +++ b/pandas/tests/io/formats/style/test_style.py @@ -1071,60 +1071,25 @@ def test_mi_sparse_column_names(self): names=["idx_level_0", "idx_level_1"], ), columns=MultiIndex.from_arrays( - [["C1", "C1", "C2", "C2"], [1, 0, 1, 0]], names=["col_0", "col_1"] + [["C1", "C1", "C2", "C2"], [1, 0, 1, 0]], names=["colnam_0", "colnam_1"] ), ) result = Styler(df, cell_ids=False)._translate(True, True) - head = result["head"][1] - expected = [ - { - "class": "blank", - "value": self.blank_value, - "display_value": self.blank_value, - "type": "th", - "is_visible": True, - }, - { - "class": "index_name level1", - "value": "col_1", - "display_value": "col_1", - "is_visible": True, - "type": "th", - }, - { - "class": "col_heading level1 col0", - "display_value": "1", - "is_visible": True, - "type": "th", - "value": 1, - "attributes": "", - }, - { - "class": "col_heading level1 col1", - "display_value": "0", - "is_visible": True, - "type": "th", - "value": 0, - "attributes": "", - }, - { - "class": "col_heading level1 col2", - "display_value": "1", - "is_visible": True, - "type": "th", - "value": 1, - "attributes": "", - }, - { - "class": "col_heading level1 col3", - "display_value": "0", - "is_visible": True, - "type": "th", - "value": 0, - "attributes": "", - }, - ] - assert head == expected + + for level in [0, 1]: + head = result["head"][level] + expected = [ + { + "class": "blank", + "display_value": self.blank_value, + }, + { + "class": f"index_name level{level}", + "display_value": f"colnam_{level}", + }, + ] + for i, expected_dict in enumerate(expected): + assert expected_dict.items() <= head[i].items() def test_hide_column_headers(self): ctx = self.styler.hide(axis="columns")._translate(True, True) From 94061c1363666b05cc124bdd7b28fee3cdffb51f Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sat, 4 Dec 2021 08:24:59 +0100 Subject: [PATCH 3/4] remove duplicated test --- pandas/tests/io/formats/style/test_style.py | 30 --------------------- 1 file changed, 30 deletions(-) diff --git a/pandas/tests/io/formats/style/test_style.py b/pandas/tests/io/formats/style/test_style.py index 5dd227bd3e22e..3fb163448ff8b 100644 --- a/pandas/tests/io/formats/style/test_style.py +++ b/pandas/tests/io/formats/style/test_style.py @@ -574,36 +574,6 @@ def test_index_name(self): } assert expected.items() <= result["head"][1][0].items() - def test_multiindex_name(self): - # https://github.com/pandas-dev/pandas/issues/11655 - df = DataFrame({"A": [1, 2], "B": [3, 4], "C": [5, 6]}) - result = df.set_index(["A", "B"]).style._translate(True, True) - - expected = [ - { - "class": "index_name level0", - "type": "th", - "value": "A", - "is_visible": True, - "display_value": "A", - }, - { - "class": "index_name level1", - "type": "th", - "value": "B", - "is_visible": True, - "display_value": "B", - }, - { - "class": "blank col0", - "type": "th", - "value": self.blank_value, - "is_visible": True, - "display_value": self.blank_value, - }, - ] - assert result["head"][1] == expected - def test_numeric_columns(self): # https://github.com/pandas-dev/pandas/issues/12125 # smoke test for _translate From d1863f005d96971400824ce437887b497258b420 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sat, 4 Dec 2021 08:29:05 +0100 Subject: [PATCH 4/4] add visibility check --- pandas/tests/io/formats/style/test_style.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pandas/tests/io/formats/style/test_style.py b/pandas/tests/io/formats/style/test_style.py index 3fb163448ff8b..fa054ff7ca6c0 100644 --- a/pandas/tests/io/formats/style/test_style.py +++ b/pandas/tests/io/formats/style/test_style.py @@ -1020,14 +1020,17 @@ def test_mi_sparse_index_names(self): { "class": "index_name level0", "display_value": "idx_level_0", + "is_visible": True, }, { "class": "index_name level1", "display_value": "idx_level_1", + "is_visible": True, }, { "class": "blank col0", "display_value": self.blank_value, + "is_visible": True, }, ] for i, expected_dict in enumerate(expected): @@ -1052,10 +1055,12 @@ def test_mi_sparse_column_names(self): { "class": "blank", "display_value": self.blank_value, + "is_visible": True, }, { "class": f"index_name level{level}", "display_value": f"colnam_{level}", + "is_visible": True, }, ] for i, expected_dict in enumerate(expected):