1
1
import numpy as np
2
2
import pytest
3
3
4
- from pandas import DataFrame
4
+ from pandas import (
5
+ DataFrame ,
6
+ MultiIndex ,
7
+ )
5
8
6
9
pytest .importorskip ("jinja2" )
7
10
from pandas .io .formats .style import Styler
@@ -22,19 +25,17 @@ def styler(df):
22
25
23
26
24
27
@pytest .mark .parametrize (
25
- "ttips " ,
28
+ "data, columns, index " ,
26
29
[
27
- DataFrame ( # Test basic reindex and ignoring blank
28
- data = [["Min" , "Max" ], [np .nan , "" ]],
29
- columns = ["A" , "C" ],
30
- index = ["x" , "y" ],
31
- ),
32
- DataFrame ( # Test non-referenced columns, reversed col names, short index
33
- data = [["Max" , "Min" , "Bad-Col" ]], columns = ["C" , "A" , "D" ], index = ["x" ]
34
- ),
30
+ # Test basic reindex and ignoring blank
31
+ ([["Min" , "Max" ], [np .nan , "" ]], ["A" , "C" ], ["x" , "y" ]),
32
+ # Test non-referenced columns, reversed col names, short index
33
+ ([["Max" , "Min" , "Bad-Col" ]], ["C" , "A" , "D" ], ["x" ]),
35
34
],
36
35
)
37
- def test_tooltip_render (ttips , styler ):
36
+ def test_tooltip_render (data , columns , index , styler ):
37
+ ttips = DataFrame (data = data , columns = columns , index = index )
38
+
38
39
# GH 21266
39
40
result = styler .set_tooltips (ttips ).to_html ()
40
41
@@ -64,6 +65,7 @@ def test_tooltip_ignored(styler):
64
65
result = styler .to_html () # no set_tooltips() creates no <span>
65
66
assert '<style type="text/css">\n </style>' in result
66
67
assert '<span class="pd-t"></span>' not in result
68
+ assert 'title="' not in result
67
69
68
70
69
71
def test_tooltip_css_class (styler ):
@@ -83,3 +85,95 @@ def test_tooltip_css_class(styler):
83
85
props = "color:green;color:red;" ,
84
86
).to_html ()
85
87
assert "#T_ .another-class {\n color: green;\n color: red;\n }" in result
88
+
89
+
90
+ @pytest .mark .parametrize (
91
+ "data, columns, index" ,
92
+ [
93
+ # Test basic reindex and ignoring blank
94
+ ([["Min" , "Max" ], [np .nan , "" ]], ["A" , "C" ], ["x" , "y" ]),
95
+ # Test non-referenced columns, reversed col names, short index
96
+ ([["Max" , "Min" , "Bad-Col" ]], ["C" , "A" , "D" ], ["x" ]),
97
+ ],
98
+ )
99
+ def test_tooltip_render_as_title (data , columns , index , styler ):
100
+ ttips = DataFrame (data = data , columns = columns , index = index )
101
+ # GH 56605
102
+ result = styler .set_tooltips (ttips , as_title_attribute = True ).to_html ()
103
+
104
+ # test css not added
105
+ assert "#T_ .pd-t {\n visibility: hidden;\n " not in result
106
+
107
+ # test 'Min' tooltip added as title attribute and css does not exist
108
+ assert "#T_ #T__row0_col0:hover .pd-t {\n visibility: visible;\n }" not in result
109
+ assert '#T_ #T__row0_col0 .pd-t::after {\n content: "Min";\n }' not in result
110
+ assert 'class="data row0 col0" title="Min">0</td>' in result
111
+
112
+ # test 'Max' tooltip added as title attribute and css does not exist
113
+ assert "#T_ #T__row0_col2:hover .pd-t {\n visibility: visible;\n }" not in result
114
+ assert '#T_ #T__row0_col2 .pd-t::after {\n content: "Max";\n }' not in result
115
+ assert 'class="data row0 col2" title="Max">2</td>' in result
116
+
117
+ # test Nan, empty string and bad column ignored
118
+ assert "#T_ #T__row1_col0:hover .pd-t {\n visibility: visible;\n }" not in result
119
+ assert "#T_ #T__row1_col1:hover .pd-t {\n visibility: visible;\n }" not in result
120
+ assert "#T_ #T__row0_col1:hover .pd-t {\n visibility: visible;\n }" not in result
121
+ assert "#T_ #T__row1_col2:hover .pd-t {\n visibility: visible;\n }" not in result
122
+ assert "Bad-Col" not in result
123
+ assert 'class="data row0 col1" >1</td>' in result
124
+ assert 'class="data row1 col0" >3</td>' in result
125
+ assert 'class="data row1 col1" >4</td>' in result
126
+ assert 'class="data row1 col2" >5</td>' in result
127
+ assert 'class="data row2 col0" >6</td>' in result
128
+ assert 'class="data row2 col1" >7</td>' in result
129
+ assert 'class="data row2 col2" >8</td>' in result
130
+
131
+
132
+ def test_tooltip_render_as_title_with_hidden_index_level ():
133
+ df = DataFrame (
134
+ data = [[0 , 1 , 2 ], [3 , 4 , 5 ], [6 , 7 , 8 ]],
135
+ columns = ["A" , "B" , "C" ],
136
+ index = MultiIndex .from_arrays (
137
+ [["x" , "y" , "z" ], [1 , 2 , 3 ], ["aa" , "bb" , "cc" ]],
138
+ names = ["alpha" , "num" , "char" ],
139
+ ),
140
+ )
141
+ ttips = DataFrame (
142
+ # Test basic reindex and ignoring blank, and hide level 2 (num) from index
143
+ data = [["Min" , "Max" ], [np .nan , "" ]],
144
+ columns = ["A" , "C" ],
145
+ index = MultiIndex .from_arrays (
146
+ [["x" , "y" ], [1 , 2 ], ["aa" , "bb" ]], names = ["alpha" , "num" , "char" ]
147
+ ),
148
+ )
149
+ styler = Styler (df , uuid_len = 0 )
150
+ styler = styler .hide (axis = 0 , level = - 1 , names = True )
151
+ # GH 56605
152
+ result = styler .set_tooltips (ttips , as_title_attribute = True ).to_html ()
153
+
154
+ # test css not added
155
+ assert "#T_ .pd-t {\n visibility: hidden;\n " not in result
156
+
157
+ # test 'Min' tooltip added as title attribute and css does not exist
158
+ assert "#T_ #T__row0_col0:hover .pd-t {\n visibility: visible;\n }" not in result
159
+ assert '#T_ #T__row0_col0 .pd-t::after {\n content: "Min";\n }' not in result
160
+ assert 'class="data row0 col0" title="Min">0</td>' in result
161
+
162
+ # test 'Max' tooltip added as title attribute and css does not exist
163
+ assert "#T_ #T__row0_col2:hover .pd-t {\n visibility: visible;\n }" not in result
164
+ assert '#T_ #T__row0_col2 .pd-t::after {\n content: "Max";\n }' not in result
165
+ assert 'class="data row0 col2" title="Max">2</td>' in result
166
+
167
+ # test Nan, empty string and bad column ignored
168
+ assert "#T_ #T__row1_col0:hover .pd-t {\n visibility: visible;\n }" not in result
169
+ assert "#T_ #T__row1_col1:hover .pd-t {\n visibility: visible;\n }" not in result
170
+ assert "#T_ #T__row0_col1:hover .pd-t {\n visibility: visible;\n }" not in result
171
+ assert "#T_ #T__row1_col2:hover .pd-t {\n visibility: visible;\n }" not in result
172
+ assert "Bad-Col" not in result
173
+ assert 'class="data row0 col1" >1</td>' in result
174
+ assert 'class="data row1 col0" >3</td>' in result
175
+ assert 'class="data row1 col1" >4</td>' in result
176
+ assert 'class="data row1 col2" >5</td>' in result
177
+ assert 'class="data row2 col0" >6</td>' in result
178
+ assert 'class="data row2 col1" >7</td>' in result
179
+ assert 'class="data row2 col2" >8</td>' in result
0 commit comments