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 all commits
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ I/O
- Bug in :func:`read_hdf` closing stores that it didn't open when Exceptions are raised (:issue:`28699`)
- Bug in :meth:`DataFrame.read_json` where using ``orient="index"`` would not maintain the order (:issue:`28557`)
- Bug in :meth:`DataFrame.to_html` where the length of the ``formatters`` argument was not verified (:issue:`28469`)
- Bug in :meth:`pandas.io.formats.style.Styler` formatting for floating values not displaying decimals correctly (:issue:`13257`)

Plotting
^^^^^^^^
Expand Down
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
41 changes: 32 additions & 9 deletions pandas/tests/io/formats/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -1157,28 +1157,51 @@ def test_display_format_raises(self):
with pytest.raises(TypeError):
df.style.format(True)

def test_display_set_precision(self):
# Issue #13257
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(
{"a": "{:0.1f}", "b": "{0:.2%}"}, subset=pd.IndexSlice[0, :]
)._translate()
expected = "0.1"
assert ctx["body"][0][1]["display_value"] == expected
assert ctx["body"][1][1]["display_value"] == "1.1234"
assert ctx["body"][0][2]["display_value"] == "12.34%"

raw_11 = "1.1234"
ctx = df.style.format("{:0.1f}", subset=pd.IndexSlice[0, :])._translate()
raw_11 = "1.123400"
assert ctx["body"][0][1]["display_value"] == expected
assert ctx["body"][1][1]["display_value"] == raw_11
assert ctx["body"][0][2]["display_value"] == "12.34%"

ctx = df.style.format("{:0.1f}", subset=pd.IndexSlice[0, :])._translate()
assert ctx["body"][0][1]["display_value"] == expected
assert ctx["body"][1][1]["display_value"] == raw_11

ctx = df.style.format("{:0.1f}", subset=pd.IndexSlice["a"])._translate()
assert ctx["body"][0][1]["display_value"] == expected
assert ctx["body"][0][2]["display_value"] == "0.1234"
assert ctx["body"][0][2]["display_value"] == "0.123400"

ctx = df.style.format("{:0.1f}", subset=pd.IndexSlice[0, "a"])._translate()
assert ctx["body"][0][1]["display_value"] == expected
Expand All @@ -1189,8 +1212,8 @@ def test_display_subset(self):
)._translate()
assert ctx["body"][0][1]["display_value"] == expected
assert ctx["body"][1][1]["display_value"] == "1.1"
assert ctx["body"][0][2]["display_value"] == "0.1234"
assert ctx["body"][1][2]["display_value"] == "1.1234"
assert ctx["body"][0][2]["display_value"] == "0.123400"
assert ctx["body"][1][2]["display_value"] == raw_11

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