Skip to content

Commit d999e82

Browse files
authored
REGR: object column repr not respecting float format (#40850)
1 parent 9ab55b4 commit d999e82

File tree

5 files changed

+64
-3
lines changed

5 files changed

+64
-3
lines changed

doc/source/whatsnew/v1.2.4.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Fixed regressions
2020
- Fixed regression in (in)equality comparison of ``pd.NaT`` with a non-datetimelike numpy array returning a scalar instead of an array (:issue:`40722`)
2121
- Fixed regression in :meth:`DataFrame.where` not returning a copy in the case of an all True condition (:issue:`39595`)
2222
- Fixed regression in :meth:`DataFrame.replace` raising ``IndexError`` when ``regex`` was a multi-key dictionary (:issue:`39338`)
23-
-
23+
- Fixed regression in repr of floats in an ``object`` column not respecting ``float_format`` when printed in the console or outputted through :meth:`DataFrame.to_string`, :meth:`DataFrame.to_html`, and :meth:`DataFrame.to_latex` (:issue:`40024`)
2424

2525
.. ---------------------------------------------------------------------------
2626

pandas/io/formats/format.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1275,7 +1275,9 @@ def _format_strings(self) -> list[str]:
12751275
float_format = get_option("display.float_format")
12761276
if float_format is None:
12771277
precision = get_option("display.precision")
1278-
float_format = lambda x: f"{x: .{precision:d}f}"
1278+
float_format = lambda x: _trim_zeros_single_float(
1279+
f"{x: .{precision:d}f}"
1280+
)
12791281
else:
12801282
float_format = self.float_format
12811283

@@ -1331,7 +1333,7 @@ def _format(x):
13311333
if not is_float_type[i] and leading_space:
13321334
fmt_values.append(f" {_format(v)}")
13331335
elif is_float_type[i]:
1334-
fmt_values.append(_trim_zeros_single_float(float_format(v)))
1336+
fmt_values.append(float_format(v))
13351337
else:
13361338
if leading_space is False:
13371339
# False specifically, so that the default is

pandas/tests/io/formats/test_format.py

+15
Original file line numberDiff line numberDiff line change
@@ -2025,6 +2025,21 @@ def test_repr_str_float_truncation(self, data, expected):
20252025
result = repr(series)
20262026
assert result == expected
20272027

2028+
@pytest.mark.parametrize(
2029+
"float_format,expected",
2030+
[
2031+
("{:,.0f}".format, "0 1,000\n1 test\ndtype: object"),
2032+
("{:.4f}".format, "0 1000.0000\n1 test\ndtype: object"),
2033+
],
2034+
)
2035+
def test_repr_float_format_in_object_col(self, float_format, expected):
2036+
# GH#40024
2037+
df = Series([1000.0, "test"])
2038+
with option_context("display.float_format", float_format):
2039+
result = repr(df)
2040+
2041+
assert result == expected
2042+
20282043
def test_dict_entries(self):
20292044
df = DataFrame({"A": [{"a": 1, "b": 2}]})
20302045

pandas/tests/io/formats/test_to_html.py

+26
Original file line numberDiff line numberDiff line change
@@ -883,3 +883,29 @@ def test_to_html_na_rep_and_float_format(na_rep):
883883
</tbody>
884884
</table>"""
885885
assert result == expected
886+
887+
888+
def test_to_html_float_format_object_col():
889+
# GH#40024
890+
df = DataFrame(data={"x": [1000.0, "test"]})
891+
result = df.to_html(float_format=lambda x: f"{x:,.0f}")
892+
expected = """<table border="1" class="dataframe">
893+
<thead>
894+
<tr style="text-align: right;">
895+
<th></th>
896+
<th>x</th>
897+
</tr>
898+
</thead>
899+
<tbody>
900+
<tr>
901+
<th>0</th>
902+
<td>1,000</td>
903+
</tr>
904+
<tr>
905+
<th>1</th>
906+
<td>test</td>
907+
</tr>
908+
</tbody>
909+
</table>"""
910+
911+
assert result == expected

pandas/tests/io/formats/test_to_latex.py

+18
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,24 @@ def test_to_latex_column_format(self):
124124
)
125125
assert result == expected
126126

127+
def test_to_latex_float_format_object_col(self):
128+
# GH#40024
129+
ser = Series([1000.0, "test"])
130+
result = ser.to_latex(float_format="{:,.0f}".format)
131+
expected = _dedent(
132+
r"""
133+
\begin{tabular}{ll}
134+
\toprule
135+
{} & 0 \\
136+
\midrule
137+
0 & 1,000 \\
138+
1 & test \\
139+
\bottomrule
140+
\end{tabular}
141+
"""
142+
)
143+
assert result == expected
144+
127145
def test_to_latex_empty_tabular(self):
128146
df = DataFrame()
129147
result = df.to_latex()

0 commit comments

Comments
 (0)