-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: DataFrame.to_string()
- customer formatter ***not*** called for all values in column
#45177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Please note: I am happy to make this change myself if someone can point me to the appropriate file/method to change. (Went looking through the code myself but it was not entirely clear to me where the change should go). I understand there may be issues of compatibility with existing user code. If this is a concern, there are alternative ways to solve this problem. For example, parameter |
This looks like a deliberate decision. Values are formatted like this:
This happens around pandas/pandas/io/formats/format.py Line 1371 in ff0f8ba
|
@phofl At any rate, now that I know where to look I will play with the code and run a few tests and see what changes I may think of. Thanks for the quick response. All the best. --Daniel |
Can kind of see both sides here. Would probably need a deprecation cycle if we would change that so that users can adjust their code |
For what its worth the formatting functions available in Their logic is as follows:
This gives a very flexible function that can be used to control many aspects of the display. In your particular example, if you wanted to deal with formatting |
Pandas version checks
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
I have confirmed this bug exists on the master branch of pandas.
Reproducible Example
Issue Description
Per Pandas documentation for
DataFrame.to_string
, theformatters
parameter is anote: "apply to columns’ elements" (it does not say "apply to only some elements")
The "Reproducible example" code demonstrates that the formatter is not called for all elements in the column. See output below.
Specifically it appears that customer formatters are not called for the following types:
float
,NoneType
, andNaN
. This is not a big deal forfloat
because the parameterfloat_format
allows one to install a formatter function for floats as well. However this prevents the user from custom formattingNoneType
andNaN
with their formatting function.In the reproducible example given, the user has chosen to format the column as left justified, however because
None
andNaN
values are not passed to the customer formatter, the user is unable to accomplish this task.Here is the output from the above reproducible example:
Expected Behavior
Custom formatter functions should be called for all elements in the specified column.
Since installation of custom formatters is already split between two paramters (
formatters
andfloat_format
) it may be reasonable to passNoneType
to theformatters
andNaN
to thefloat_format
formatter. Alternatively there should be a way to pass every element to one single formatter function for the column, regardless of the element's type.Installed Versions
INSTALLED VERSIONS
commit : 66e3805
python : 3.8.5.final.0
python-bits : 64
OS : Linux
OS-release : 5.10.60.1-microsoft-standard-WSL2
Version : #1 SMP Wed Aug 25 23:20:18 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.3.5
numpy : 1.19.2
pytz : 2020.1
dateutil : 2.8.1
pip : 21.3.1
setuptools : 45.2.0
Cython : None
pytest : 6.0.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.1
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 3.0.1
IPython : 8.0.0.dev
pandas_datareader: 0.10.0
bs4 : 4.9.3
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.4.2
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.7.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : None
numba : None
The text was updated successfully, but these errors were encountered: