Skip to content

Commit d9fb698

Browse files
author
AntonioAndraues
committed
BUG: set_precision format fixed (pandas-dev#13257)
BUG: set_precision format fixed (pandas-dev#13257)
1 parent d320ef7 commit d9fb698

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

pandas/io/formats/style.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ def __init__(
151151

152152
def default_display_func(x):
153153
if is_float(x):
154-
return "{:>.{precision}g}".format(x, precision=self.precision)
154+
display_format="{0:.{precision}f}".format(x, precision=self.precision)
155+
return display_format.rstrip('0').rstrip('.')
155156
else:
156157
return x
157158

pandas/tests/io/formats/test_style.py

+28
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,7 @@ def test_precision(self):
10621062
s2 = s.set_precision(4)
10631063
assert s is s2
10641064
assert s.precision == 4
1065+
10651066

10661067
def test_apply_none(self):
10671068
def f(x):
@@ -1156,6 +1157,33 @@ def test_display_format_raises(self):
11561157
df.style.format(5)
11571158
with pytest.raises(TypeError):
11581159
df.style.format(True)
1160+
1161+
def test_display_set_precision(self):
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"
1169+
assert ctx["body"][0][2]["display_value"] == "2"
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"
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"
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+
# assert
11591187

11601188
def test_display_subset(self):
11611189
df = pd.DataFrame([[0.1234, 0.1234], [1.1234, 1.1234]], columns=["a", "b"])

0 commit comments

Comments
 (0)