Repository: pandas-dev/pandas

Issue:
display.precision not honored for column headers
#### Code Sample, a copy-pastable example if possible

```python
import pandas as pd
import numpy as np
pd.options.display.precision = 3
labels = np.random.rand(10)
p = pd.DataFrame(np.random.randn(10,10),index=labels,columns=labels)
p

```
#### Problem description

The above code works just fine in a standard console, but in a jupyter notebook the float valued column headers are not displayed with a precision of 3.  This appears to be due to the notebook_repr_html option, since setting that to False again honors the precision.  Note that the same applies to other formatting options, and if I switch to a MultiIndex it looks fine. I posted this as a question on the pydata discussion site and Joris Van den Bossche confirmed that it was an issue and suggested that I report it here.

#### Expected Output

#### Output of ``pd.show_versions()``

<details>

[paste the output of ``pd.show_versions()`` here below this line]

</details>



Discussion:
Thanks for reporting!

>  Note that the same applies to other formatting options

What do you mean exactly with this? That also other formatting options don't work with the notebook repr?

Yes. I couldn't remember them off the top of my head, but I think that there's a float_format option and an eng_float_format, or something like that. All behave the same way.

Thanks, Paul

Sent from my iPhone

On Aug 18, 2017, at 7:01 AM, Joris Van den Bossche <notifications@github.com<mailto:notifications@github.com>> wrote:


Thanks for reporting!

Note that the same applies to other formatting options

What do you mean exactly with this? That also other formatting options don't work with the notebook repr?

-
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub<https://github.com/pandas-dev/pandas/issues/17280#issuecomment-323361774>, or mute the thread<https://github.com/notifications/unsubscribe-auth/AEG2KrjA9m1I944PDF6vhXglCny7ADzRks5sZZkQgaJpZM4O7jej>.

I believe this is not fixed in pandas 0.23.0 at least. Unless it should be seen as a Jupyter notebook issue after all. If we `print(p)` rather than simply `p`, the issue does not manifest (but in that case the display is more primitive than without the `print`).

Resolving patch:
diff --git a/pandas/io/formats/html.py b/pandas/io/formats/html.py
--- a/pandas/io/formats/html.py
+++ b/pandas/io/formats/html.py
@@ -70,6 +70,9 @@ def row_levels(self):
         # not showing (row) index
         return 0
 
+    def _get_columns_formatted_values(self):
+        return self.columns
+
     @property
     def is_truncated(self):
         return self.fmt.is_truncated
@@ -292,7 +295,7 @@ def _write_col_header(self, indent):
                     row.append(self.columns.name or '')
                 else:
                     row.append('')
-            row.extend(self.columns)
+            row.extend(self._get_columns_formatted_values())
             align = self.fmt.justify
 
             if truncate_h:
@@ -494,6 +497,9 @@ class NotebookFormatter(HTMLFormatter):
     def _get_formatted_values(self):
         return {i: self.fmt._format_col(i) for i in range(self.ncols)}
 
+    def _get_columns_formatted_values(self):
+        return self.columns.format()
+
     def write_style(self):
         # We use the "scoped" attribute here so that the desired
         # style properties for the data frame are not then applied


Test patch:
diff --git a/pandas/tests/io/formats/test_to_html.py b/pandas/tests/io/formats/test_to_html.py
--- a/pandas/tests/io/formats/test_to_html.py
+++ b/pandas/tests/io/formats/test_to_html.py
@@ -633,3 +633,13 @@ def test_to_html_invalid_classes_type(classes):
 
     with pytest.raises(TypeError, match=msg):
         df.to_html(classes=classes)
+
+
+def test_to_html_round_column_headers():
+    # GH 17280
+    df = DataFrame([1], columns=[0.55555])
+    with pd.option_context('display.precision', 3):
+        html = df.to_html(notebook=False)
+        notebook = df.to_html(notebook=True)
+    assert "0.55555" in html
+    assert "0.556" in notebook