Skip to content

Commit 8442843

Browse files
AntonioAndrauesMateusz Górski
authored and
Mateusz Górski
committed
BUG: set_precision format fixed (pandas-dev#13257) (pandas-dev#27934)
1 parent de2e992 commit 8442843

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ I/O
355355
- Bug in :func:`read_hdf` closing stores that it didn't open when Exceptions are raised (:issue:`28699`)
356356
- Bug in :meth:`DataFrame.read_json` where using ``orient="index"`` would not maintain the order (:issue:`28557`)
357357
- Bug in :meth:`DataFrame.to_html` where the length of the ``formatters`` argument was not verified (:issue:`28469`)
358+
- Bug in :meth:`pandas.io.formats.style.Styler` formatting for floating values not displaying decimals correctly (:issue:`13257`)
358359

359360
Plotting
360361
^^^^^^^^

pandas/io/formats/style.py

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

157157
def default_display_func(x):
158158
if is_float(x):
159-
return "{:>.{precision}g}".format(x, precision=self.precision)
159+
display_format = "{0:.{precision}f}".format(x, precision=self.precision)
160+
return display_format
160161
else:
161162
return x
162163

pandas/tests/io/formats/test_style.py

+32-9
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)