Skip to content

Commit 8931f62

Browse files
author
AntonioAndraues
committed
BUG: set_precision format fixed (pandas-dev#13257)
BUG: set_precision format fixed (pandas-dev#13257) fix fix pep8 fix pep8 black pandas whatsnew fix small
1 parent d320ef7 commit 8931f62

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

doc/source/whatsnew/v1.0.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ ExtensionArray
201201
Other
202202
^^^^^
203203
- Trying to set the ``display.precision``, ``display.max_rows`` or ``display.max_columns`` using :meth:`set_option` to anything but a ``None`` or a positive int will raise a ``ValueError`` (:issue:`23348`)
204-
204+
- Bug in pandas.io.formats.style : fixed function default_display_func because wasn`t displaying decimals digits as supposed (:issue:`13257`)
205205

206206
.. _whatsnew_1000.contributors:
207207

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

+26
Original file line numberDiff line numberDiff line change
@@ -1157,6 +1157,32 @@ 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+
df = pd.DataFrame(data=[[1.0, 2.0090], [3.2121, 4.566]], columns=["a", "b"])
1162+
s = Styler(df)
1163+
1164+
ctx = s.set_precision(1)._translate()
1165+
1166+
assert s.precision == 1
1167+
assert ctx["body"][0][1]["display_value"] == "1"
1168+
assert ctx["body"][0][2]["display_value"] == "2"
1169+
assert ctx["body"][1][1]["display_value"] == "3.2"
1170+
assert ctx["body"][1][2]["display_value"] == "4.6"
1171+
1172+
ctx = s.set_precision(2)._translate()
1173+
assert s.precision == 2
1174+
assert ctx["body"][0][1]["display_value"] == "1"
1175+
assert ctx["body"][0][2]["display_value"] == "2.01"
1176+
assert ctx["body"][1][1]["display_value"] == "3.21"
1177+
assert ctx["body"][1][2]["display_value"] == "4.57"
1178+
1179+
ctx = s.set_precision(3)._translate()
1180+
assert s.precision == 3
1181+
assert ctx["body"][0][1]["display_value"] == "1"
1182+
assert ctx["body"][0][2]["display_value"] == "2.009"
1183+
assert ctx["body"][1][1]["display_value"] == "3.212"
1184+
assert ctx["body"][1][2]["display_value"] == "4.566"
1185+
11601186
def test_display_subset(self):
11611187
df = pd.DataFrame([[0.1234, 0.1234], [1.1234, 1.1234]], columns=["a", "b"])
11621188
ctx = df.style.format(

0 commit comments

Comments
 (0)