Skip to content

BUG: set_precision format fixed (#13257) #27934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
BUG: set_precision format fixed (#13257)
Co-Authored-By: Tom Augspurger <TomAugspurger@users.noreply.github.com>
  • Loading branch information
AntonioAndraues and TomAugspurger committed Oct 15, 2019
commit e74e5cb021b0465826b450b6c3c7d3373cc742c8
3 changes: 2 additions & 1 deletion pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ def __init__(

def default_display_func(x):
if is_float(x):
return "{:>.{precision}g}".format(x, precision=self.precision)
display_format = "{0:.{precision}f}".format(x, precision=self.precision)
return display_format
else:
return x

Expand Down
26 changes: 26 additions & 0 deletions pandas/tests/io/formats/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,32 @@ def test_display_format_raises(self):
with pytest.raises(TypeError):
df.style.format(True)

def test_display_set_precision(self):
df = pd.DataFrame(data=[[1.0, 2.0090], [3.2121, 4.566]], columns=["a", "b"])
s = Styler(df)

ctx = s.set_precision(1)._translate()

assert s.precision == 1
assert ctx["body"][0][1]["display_value"] == "1.0"
assert ctx["body"][0][2]["display_value"] == "2.0"
assert ctx["body"][1][1]["display_value"] == "3.2"
assert ctx["body"][1][2]["display_value"] == "4.6"

ctx = s.set_precision(2)._translate()
assert s.precision == 2
assert ctx["body"][0][1]["display_value"] == "1.00"
assert ctx["body"][0][2]["display_value"] == "2.01"
assert ctx["body"][1][1]["display_value"] == "3.21"
assert ctx["body"][1][2]["display_value"] == "4.57"

ctx = s.set_precision(3)._translate()
assert s.precision == 3
assert ctx["body"][0][1]["display_value"] == "1.000"
assert ctx["body"][0][2]["display_value"] == "2.009"
assert ctx["body"][1][1]["display_value"] == "3.212"
assert ctx["body"][1][2]["display_value"] == "4.566"

def test_display_subset(self):
df = pd.DataFrame([[0.1234, 0.1234], [1.1234, 1.1234]], columns=["a", "b"])
ctx = df.style.format(
Expand Down