Repository: streamlit/streamlit

Issue:
Dataframe styling not applied consistently for floats
## The problem

This code:
```python
import streamlit as st
import pandas as pd

df = pd.DataFrame({'test': [3.14, 3.1]})
st.dataframe(df.style.format({'test': '{:.2f}'}))
```

Produces:
<img width="189" alt="Screen Shot 2020-02-20 at 9 46 38 AM" src="https://user-images.githubusercontent.com/709022/74963053-ec84a480-53c5-11ea-8ac1-18fdaf1f2536.png">

^the 3.14 entry is incorrectly formatted as `3.1400`.

## The cause

- `data_frame_proto._get_custom_display_values()` computes formatted values from a dataframe styler
- It sticks formatted values into the dataframe proto iff the formatted value does not equal the cell's unformatted value (see `has_custom_display_value()`).
- In this case, `"{:.2f}".format(3.14) == str(3.14)`, so the formatted value is not stored in the dataframe. (This is just a bandwidth and serialization optimization, so that e.g. if only one cell in a giant dataframe is formatted, we don't include formatted string versions of all its other cells.)
- BUT, on the frontend, floating point values that do not have custom format strings are then passed through `format.ts.toFormattedString()` (see `Dataframe.tsx getCellContentsGetter()`). This formats all floats with 4 decimals. For unstyled dataframes, that's a reasonable default. But for styled dataframes, we may actually override the user's styling preferences.

## Fixes

- The simplest fix is to remove the `has_custom_display_value` check in `data_frame_proto._get_custom_display_values()`, and pass formatted strings for all cells of a styled dataframe. This will increase the size of most styled dataframe protos.
- Alternately, we could avoid applying the default float formatting in the frontend in cases where it would override the user's style preferences. But this requires deeper thought about how best to implement!
Dataframe styling not applied consistently for floats
## The problem

This code:
```python
import streamlit as st
import pandas as pd

df = pd.DataFrame({'test': [3.14, 3.1]})
st.dataframe(df.style.format({'test': '{:.2f}'}))
```

Produces:
<img width="189" alt="Screen Shot 2020-02-20 at 9 46 38 AM" src="https://user-images.githubusercontent.com/709022/74963053-ec84a480-53c5-11ea-8ac1-18fdaf1f2536.png">

^the 3.14 entry is incorrectly formatted as `3.1400`.

## The cause

- `data_frame_proto._get_custom_display_values()` computes formatted values from a dataframe styler
- It sticks formatted values into the dataframe proto iff the formatted value does not equal the cell's unformatted value (see `has_custom_display_value()`).
- In this case, `"{:.2f}".format(3.14) == str(3.14)`, so the formatted value is not stored in the dataframe. (This is just a bandwidth and serialization optimization, so that e.g. if only one cell in a giant dataframe is formatted, we don't include formatted string versions of all its other cells.)
- BUT, on the frontend, floating point values that do not have custom format strings are then passed through `format.ts.toFormattedString()` (see `Dataframe.tsx getCellContentsGetter()`). This formats all floats with 4 decimals. For unstyled dataframes, that's a reasonable default. But for styled dataframes, we may actually override the user's styling preferences.

## Fixes

- The simplest fix is to remove the `has_custom_display_value` check in `data_frame_proto._get_custom_display_values()`, and pass formatted strings for all cells of a styled dataframe. This will increase the size of most styled dataframe protos.
- Alternately, we could avoid applying the default float formatting in the frontend in cases where it would override the user's style preferences. But this requires deeper thought about how best to implement!


Discussion:

Resolving patch:
diff --git a/e2e/scripts/st_dataframe_format.py b/e2e/scripts/st_dataframe_format.py
new file mode 100644
--- /dev/null
+++ b/e2e/scripts/st_dataframe_format.py
@@ -0,0 +1,19 @@
+# Copyright 2018-2020 Streamlit Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import streamlit as st
+import pandas as pd
+
+df = pd.DataFrame({"test": [3.14, 3.1]})
+st.dataframe(df.style.format({"test": "{:.2f}"}))
diff --git a/lib/streamlit/elements/data_frame_proto.py b/lib/streamlit/elements/data_frame_proto.py
--- a/lib/streamlit/elements/data_frame_proto.py
+++ b/lib/streamlit/elements/data_frame_proto.py
@@ -157,15 +157,19 @@ def _get_custom_display_values(df, translated_style):
     default_formatter = df.style._display_funcs[(0, 0)]
 
     def has_custom_display_value(cell):
-        value = str(cell["value"])
-        display_value = str(cell["display_value"])
-        if value == display_value:
+        value = cell["value"]
+        display_value = cell["display_value"]
+
+        if type(value) is type(display_value) and str(value) == str(display_value):
             return False
 
         # Pandas applies a default style to all float values, regardless
         # of whether they have a user-specified display format. We test
         # for that here.
-        return default_formatter(value) != display_value
+        return (
+            type(value) is not type(display_value)
+            or default_formatter(value) != display_value
+        )
 
     cell_selector_regex = re.compile(r"row(\d+)_col(\d+)")
     header_selector_regex = re.compile(r"level(\d+)_row(\d+)")


Test patch: