Skip to content

Commit f780104

Browse files
BUG: DataFrame.style displays wrong value for long integers #52272 FIX (#52686)
* BUG: DataFrame.style displays wrong value for long integers #52272. Fixed bug and added test to test_to_html and test_to_latex. * BUG: DataFrame.style displays wrong value for long integers #52272 add documentation * BUG: DataFrame.style displays wrong value for long integers #52272 FIX documentation and tests --------- Co-authored-by: Luciana Solorzano <[email protected]>
1 parent ff8e88a commit f780104

File tree

5 files changed

+36
-1
lines changed

5 files changed

+36
-1
lines changed

doc/source/whatsnew/v2.1.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ Numeric
312312

313313
Conversion
314314
^^^^^^^^^^
315+
- Bug in :func:`DataFrame.style.to_latex` and :func:`DataFrame.style.to_html` if the DataFrame contains integers with more digits than can be represented by floating point double precision (:issue:`52272`)
315316
- Bug in :meth:`ArrowDtype.numpy_dtype` returning nanosecond units for non-nanosecond ``pyarrow.timestamp`` and ``pyarrow.duration`` types (:issue:`51800`)
316317
- Bug in :meth:`DataFrame.info` raising ``ValueError`` when ``use_numba`` is set (:issue:`51922`)
317318
-

pandas/io/formats/style_render.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1747,7 +1747,7 @@ def _default_formatter(x: Any, precision: int, thousands: bool = False) -> Any:
17471747
if is_float(x) or is_complex(x):
17481748
return f"{x:,.{precision}f}" if thousands else f"{x:.{precision}f}"
17491749
elif is_integer(x):
1750-
return f"{x:,.0f}" if thousands else f"{x:.0f}"
1750+
return f"{x:,.0f}" if thousands else str(x)
17511751
return x
17521752

17531753

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<table border="1" class="dataframe">
2+
<thead>
3+
<tr style="text-align: right;">
4+
<th></th>
5+
<th>test</th>
6+
</tr>
7+
</thead>
8+
<tbody>
9+
<tr>
10+
<th>0</th>
11+
<td>1234567890123456789</td>
12+
</tr>
13+
</tbody>
14+
</table>

pandas/tests/io/formats/test_to_html.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,3 +896,11 @@ def test_to_html_float_format_object_col(datapath):
896896
result = df.to_html(float_format=lambda x: f"{x:,.0f}")
897897
expected = expected_html(datapath, "gh40024_expected_output")
898898
assert result == expected
899+
900+
901+
def test_to_html_float_point_double(datapath):
902+
# GH#52272
903+
df = DataFrame(data=[[1234567890123456789]], columns=["test"])
904+
result = df.to_html()
905+
expected = expected_html(datapath, "gh52272_expected_output")
906+
assert result == expected

pandas/tests/io/formats/test_to_latex.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,3 +1499,15 @@ def test_get_strrow_multindex_multicolumn(self, row_num, expected):
14991499
)
15001500

15011501
assert row_string_converter.get_strrow(row_num=row_num) == expected
1502+
1503+
def test_to_latex_exceeding_float_point_double(self):
1504+
df = DataFrame(data=[[1234567890123456789]], columns=["test"])
1505+
expected = _dedent(
1506+
r"""
1507+
\begin{tabular}{lr}
1508+
& test \\
1509+
0 & 1234567890123456789 \\
1510+
\end{tabular}
1511+
"""
1512+
)
1513+
assert df.style.to_latex() == expected

0 commit comments

Comments
 (0)