Repository: pandas-dev/pandas

Issue:
Pandas style.set_precision not working as expected
I expected the following code to give me a representation of my data that is rounded or truncated to 2 decimals.

```
import pandas as pd
import numpy as np

df = pd.DataFrame(data = np.random.randn(4,4), columns=['A','B','C','D'])
df.style.set_precision(2)
```

However, it will always return 2 digits instead, so for example, I want 2.37419 to be displayed as 2.37, but instead this code will give me 2.4. Am I overseeing something or is this a bug?

Note that the following code works as I expected, but then I change a general setting whereas I would like to apply it only to the current output:

```
pd.set_option('display.precision',2)
df
```

I'm using pandas version 0.18.1 in Jupyter notebook on Firefox, with python 2.7.11.final.0 and IPython 4.1.2 on 64-bits linux.



Discussion:
I believe this is just reusing something like this from python

``` python
>>> '{:.2g}'.format(2.37419)
'2.4'
```

Would take a PR fixing this to have the 2.37 like expected. I think we recently fixed a similar problem with the 'display.precision' option.

df = pd.DataFrame([123.4567, 234.5678, 345.6789, 456.7890],
                  index=['a','b','c','d'],
                  columns=['cost'])
df.style.set_precision(precision=2)

>>>
  | cost
-- | --
1.2e+02
2.3e+02
3.5e+02
4.6e+02

  | cost
-- | --
1.2e+02
2.3e+02
3.5e+02
4.6e+02



Resolving patch:
diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py
--- a/pandas/io/formats/style.py
+++ b/pandas/io/formats/style.py
@@ -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
 


Test patch:
diff --git a/pandas/tests/io/formats/test_style.py b/pandas/tests/io/formats/test_style.py
--- a/pandas/tests/io/formats/test_style.py
+++ b/pandas/tests/io/formats/test_style.py
@@ -1157,20 +1157,43 @@ 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
@@ -1178,7 +1201,7 @@ def test_display_subset(self):
 
         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
@@ -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"])