@@ -1157,28 +1157,51 @@ def test_display_format_raises(self):
11571157 with pytest .raises (TypeError ):
11581158 df .style .format (True )
11591159
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+
11601187 def test_display_subset (self ):
11611188 df = pd .DataFrame ([[0.1234 , 0.1234 ], [1.1234 , 1.1234 ]], columns = ["a" , "b" ])
11621189 ctx = df .style .format (
11631190 {"a" : "{:0.1f}" , "b" : "{0:.2%}" }, subset = pd .IndexSlice [0 , :]
11641191 )._translate ()
11651192 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"
11721194 assert ctx ["body" ][0 ][1 ]["display_value" ] == expected
11731195 assert ctx ["body" ][1 ][1 ]["display_value" ] == raw_11
1196+ assert ctx ["body" ][0 ][2 ]["display_value" ] == "12.34%"
11741197
11751198 ctx = df .style .format ("{:0.1f}" , subset = pd .IndexSlice [0 , :])._translate ()
11761199 assert ctx ["body" ][0 ][1 ]["display_value" ] == expected
11771200 assert ctx ["body" ][1 ][1 ]["display_value" ] == raw_11
11781201
11791202 ctx = df .style .format ("{:0.1f}" , subset = pd .IndexSlice ["a" ])._translate ()
11801203 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 "
11821205
11831206 ctx = df .style .format ("{:0.1f}" , subset = pd .IndexSlice [0 , "a" ])._translate ()
11841207 assert ctx ["body" ][0 ][1 ]["display_value" ] == expected
@@ -1189,8 +1212,8 @@ def test_display_subset(self):
11891212 )._translate ()
11901213 assert ctx ["body" ][0 ][1 ]["display_value" ] == expected
11911214 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
11941217
11951218 def test_display_dict (self ):
11961219 df = pd .DataFrame ([[0.1234 , 0.1234 ], [1.1234 , 1.1234 ]], columns = ["a" , "b" ])
0 commit comments