Skip to content

Commit f459a48

Browse files
tomneepPingviinituutti
authored andcommitted
BUG: Fix float formatting when a string is passed as float_format arg (pandas-dev#22308)
1 parent e444cdb commit f459a48

File tree

7 files changed

+81
-0
lines changed

7 files changed

+81
-0
lines changed

doc/source/whatsnew/v0.24.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,7 @@ Notice how we now instead output ``np.nan`` itself instead of a stringified form
13991399
- Bug in :meth:`read_excel()` in which extraneous header names were extracted, even though none were specified (:issue:`11733`)
14001400
- Bug in :meth:`read_excel()` in which ``index_col=None`` was not being respected and parsing index columns anyway (:issue:`20480`)
14011401
- Bug in :meth:`read_excel()` in which ``usecols`` was not being validated for proper column names when passed in as a string (:issue:`20480`)
1402+
- :func:`DataFrame.to_string()`, :func:`DataFrame.to_html()`, :func:`DataFrame.to_latex()` will correctly format output when a string is passed as the ``float_format`` argument (:issue:`21625`, :issue:`22270`)
14021403

14031404
Plotting
14041405
^^^^^^^^

pandas/io/formats/format.py

+2
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,8 @@ def __init__(self, *args, **kwargs):
960960
# float_format is expected to be a string
961961
# formatter should be used to pass a function
962962
if self.float_format is not None and self.formatter is None:
963+
# GH21625, GH22270
964+
self.fixed_width = False
963965
if callable(self.float_format):
964966
self.formatter = self.float_format
965967
self.float_format = None
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>x</th>
6+
</tr>
7+
</thead>
8+
<tbody>
9+
<tr>
10+
<th>0</th>
11+
<td>0.200</td>
12+
</tr>
13+
</tbody>
14+
</table>
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>x</th>
6+
</tr>
7+
</thead>
8+
<tbody>
9+
<tr>
10+
<th>0</th>
11+
<td>100</td>
12+
</tr>
13+
</tbody>
14+
</table>

pandas/tests/io/formats/test_format.py

+12
Original file line numberDiff line numberDiff line change
@@ -1359,6 +1359,18 @@ def test_to_string_float_formatting(self):
13591359
'1 2.512000e-01')
13601360
assert df_s == expected
13611361

1362+
def test_to_string_float_format_no_fixed_width(self):
1363+
1364+
# GH 21625
1365+
df = DataFrame({'x': [0.19999]})
1366+
expected = ' x\n0 0.200'
1367+
assert df.to_string(float_format='%.3f') == expected
1368+
1369+
# GH 22270
1370+
df = DataFrame({'x': [100.0]})
1371+
expected = ' x\n0 100'
1372+
assert df.to_string(float_format='%.0f') == expected
1373+
13621374
def test_to_string_small_float_values(self):
13631375
df = DataFrame({'a': [1.5, 1e-17, -5.5e-7]})
13641376

pandas/tests/io/formats/test_to_html.py

+12
Original file line numberDiff line numberDiff line change
@@ -465,3 +465,15 @@ def test_to_html_with_id(self):
465465
name='myindexname'))
466466
result = df.to_html(index_names=False, table_id="TEST_ID")
467467
assert ' id="TEST_ID"' in result
468+
469+
def test_to_html_float_format_no_fixed_width(self, datapath):
470+
471+
# GH 21625
472+
df = DataFrame({'x': [0.19999]})
473+
expected = expected_html(datapath, 'gh21625_expected_output')
474+
assert df.to_html(float_format='%.3f') == expected
475+
476+
# GH 22270
477+
df = DataFrame({'x': [100.0]})
478+
expected = expected_html(datapath, 'gh22270_expected_output')
479+
assert df.to_html(float_format='%.0f') == expected

pandas/tests/io/formats/test_to_latex.py

+26
Original file line numberDiff line numberDiff line change
@@ -708,3 +708,29 @@ def test_to_latex_multiindex_empty_name(self):
708708
\end{tabular}
709709
"""
710710
assert observed == expected
711+
712+
def test_to_latex_float_format_no_fixed_width(self):
713+
714+
# GH 21625
715+
df = DataFrame({'x': [0.19999]})
716+
expected = r"""\begin{tabular}{lr}
717+
\toprule
718+
{} & x \\
719+
\midrule
720+
0 & 0.200 \\
721+
\bottomrule
722+
\end{tabular}
723+
"""
724+
assert df.to_latex(float_format='%.3f') == expected
725+
726+
# GH 22270
727+
df = DataFrame({'x': [100.0]})
728+
expected = r"""\begin{tabular}{lr}
729+
\toprule
730+
{} & x \\
731+
\midrule
732+
0 & 100 \\
733+
\bottomrule
734+
\end{tabular}
735+
"""
736+
assert df.to_latex(float_format='%.0f') == expected

0 commit comments

Comments
 (0)