@@ -1157,28 +1157,51 @@ def test_display_format_raises(self):
1157
1157
with pytest .raises (TypeError ):
1158
1158
df .style .format (True )
1159
1159
1160
+ def test_display_set_precision (self ):
1161
+ # Issue #13257
1162
+ df = pd .DataFrame (data = [[1.0 , 2.0090 ], [3.2121 , 4.566 ]], columns = ["a" , "b" ])
1163
+ s = Styler (df )
1164
+
1165
+ ctx = s .set_precision (1 )._translate ()
1166
+
1167
+ assert s .precision == 1
1168
+ assert ctx ["body" ][0 ][1 ]["display_value" ] == "1.0"
1169
+ assert ctx ["body" ][0 ][2 ]["display_value" ] == "2.0"
1170
+ assert ctx ["body" ][1 ][1 ]["display_value" ] == "3.2"
1171
+ assert ctx ["body" ][1 ][2 ]["display_value" ] == "4.6"
1172
+
1173
+ ctx = s .set_precision (2 )._translate ()
1174
+ assert s .precision == 2
1175
+ assert ctx ["body" ][0 ][1 ]["display_value" ] == "1.00"
1176
+ assert ctx ["body" ][0 ][2 ]["display_value" ] == "2.01"
1177
+ assert ctx ["body" ][1 ][1 ]["display_value" ] == "3.21"
1178
+ assert ctx ["body" ][1 ][2 ]["display_value" ] == "4.57"
1179
+
1180
+ ctx = s .set_precision (3 )._translate ()
1181
+ assert s .precision == 3
1182
+ assert ctx ["body" ][0 ][1 ]["display_value" ] == "1.000"
1183
+ assert ctx ["body" ][0 ][2 ]["display_value" ] == "2.009"
1184
+ assert ctx ["body" ][1 ][1 ]["display_value" ] == "3.212"
1185
+ assert ctx ["body" ][1 ][2 ]["display_value" ] == "4.566"
1186
+
1160
1187
def test_display_subset (self ):
1161
1188
df = pd .DataFrame ([[0.1234 , 0.1234 ], [1.1234 , 1.1234 ]], columns = ["a" , "b" ])
1162
1189
ctx = df .style .format (
1163
1190
{"a" : "{:0.1f}" , "b" : "{0:.2%}" }, subset = pd .IndexSlice [0 , :]
1164
1191
)._translate ()
1165
1192
expected = "0.1"
1166
- assert ctx ["body" ][0 ][1 ]["display_value" ] == expected
1167
- assert ctx ["body" ][1 ][1 ]["display_value" ] == "1.1234"
1168
- assert ctx ["body" ][0 ][2 ]["display_value" ] == "12.34%"
1169
-
1170
- raw_11 = "1.1234"
1171
- ctx = df .style .format ("{:0.1f}" , subset = pd .IndexSlice [0 , :])._translate ()
1193
+ raw_11 = "1.123400"
1172
1194
assert ctx ["body" ][0 ][1 ]["display_value" ] == expected
1173
1195
assert ctx ["body" ][1 ][1 ]["display_value" ] == raw_11
1196
+ assert ctx ["body" ][0 ][2 ]["display_value" ] == "12.34%"
1174
1197
1175
1198
ctx = df .style .format ("{:0.1f}" , subset = pd .IndexSlice [0 , :])._translate ()
1176
1199
assert ctx ["body" ][0 ][1 ]["display_value" ] == expected
1177
1200
assert ctx ["body" ][1 ][1 ]["display_value" ] == raw_11
1178
1201
1179
1202
ctx = df .style .format ("{:0.1f}" , subset = pd .IndexSlice ["a" ])._translate ()
1180
1203
assert ctx ["body" ][0 ][1 ]["display_value" ] == expected
1181
- assert ctx ["body" ][0 ][2 ]["display_value" ] == "0.1234 "
1204
+ assert ctx ["body" ][0 ][2 ]["display_value" ] == "0.123400 "
1182
1205
1183
1206
ctx = df .style .format ("{:0.1f}" , subset = pd .IndexSlice [0 , "a" ])._translate ()
1184
1207
assert ctx ["body" ][0 ][1 ]["display_value" ] == expected
@@ -1189,8 +1212,8 @@ def test_display_subset(self):
1189
1212
)._translate ()
1190
1213
assert ctx ["body" ][0 ][1 ]["display_value" ] == expected
1191
1214
assert ctx ["body" ][1 ][1 ]["display_value" ] == "1.1"
1192
- assert ctx ["body" ][0 ][2 ]["display_value" ] == "0.1234 "
1193
- assert ctx ["body" ][1 ][2 ]["display_value" ] == "1.1234"
1215
+ assert ctx ["body" ][0 ][2 ]["display_value" ] == "0.123400 "
1216
+ assert ctx ["body" ][1 ][2 ]["display_value" ] == raw_11
1194
1217
1195
1218
def test_display_dict (self ):
1196
1219
df = pd .DataFrame ([[0.1234 , 0.1234 ], [1.1234 , 1.1234 ]], columns = ["a" , "b" ])
0 commit comments