Open shoyer opened 10 years ago
let's call this a bug then!
feel free to do a PR for this!
Some investigation reveals that the issue is that formatter
function must return a string. That seems like a reasonable choice, although it isn't what I guessed (I tried float_format=round
). So this will actually just be a doc fix -- expect a PR for that shortly.
I think you can do: float_format=lambda x: round(x)
or even better: float_float='%.0f'
@jreback I agree, it would be nice if those did work, but unfortunately neither of them do currently. That would definitely be worth doing, but my doc-fix PR should clarify this for now.
Is there any example on how to use the formatters field?
take
The reasons why this issue was not closed are mentioned in https://github.com/pandas-dev/pandas/pull/6054#issuecomment-33432118
Disagree this "fixes" 6052, but it does help! I wonder if there is a good place in the docs to add more about this?
I think probably we should catch bad input and re-raise it with better message... accepting string e.g.
'%0.f'
would also be nice.
Documentation now states:
float_format : one-parameter function or str, optional, default None
Formatter for floating point numbers. For example
``float_format="%.2f"`` and ``float_format="{{:0.2f}}".format`` will
both result in 0.1234 being formatted as 0.12.
Example
import pandas as pd
df = pd.DataFrame({'a': [1.0, 2.0]})
print(df.to_latex(float_format='%0.f'))
FutureWarning: In future versions `DataFrame.to_latex` is expected to utilise the base implementation of `Styler.to_latex` for formatting and rendering. The arguments signature may therefore change. It is recommended instead to use `DataFrame.style.to_latex` which also contains additional functionality.
print(df.to_latex(float_format='%0.f'))
\begin{tabular}{lr}
\toprule
{} & a \\
\midrule
0 & 1 \\
1 & 2 \\
\bottomrule
\end{tabular}
Which solves the part
accepting string e.g. '%0.f' would also be
We are just missing now the other part
we should catch bad input and re-raise it with better message
But due to the future warning, the usage of this function is going to change dramatically, so I will not work on this.
It appears that neither the
formatters
norfloat_format
arguments toDataFrame.to_latex
work if changed from their default values. Both raise the same exception:AttributeError: 'numpy.float64' object has no attribute 'decode'
.Note: neither of these arguments has test coverage in
pandas/tests/test_format.py
My test script: