Skip to content

BUG: float formatters ignore "object" extension type; Other issues in same code #39850

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

Open
2 of 3 tasks
EmperorArthur opened this issue Feb 16, 2021 · 2 comments
Open
2 of 3 tasks
Labels
Bug Output-Formatting __repr__ of pandas objects, to_string

Comments

@EmperorArthur
Copy link

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

import pandas as pd
df = pd.DataFrame([0.123456789, 1.123456789, 2.123456789], columns=["value"])
df = df.astype({"value": "object"})
print(df.to_string(formatters=["{:.2f}".format]))

Problem description

This probably has the same root cause as Bug #39336, except it affects object types.
This matters because it is unexpected behavior to the user, and it is possible for Pandas to guess dtypes incorrectly.

Without significant research and finding Bug #39336, I would have never guessed that the way to stop the default float formatter was to do df = df.astype({"value": float}).

Expected Output

value
0 0.123457
1 1.123457
2 2.123457

Other Information

I am seeing the problem in pandas.io.formats.format.GenericArrayFormatter._format_strings.

The for i, v in enumerate(vals):... loop should probably adjusted to just use self.formatter if it exists and bypass all the other checks. If a user has defined their own formatter, then Pandas should not try to second guess the user based on data type or if it is a NaN value.

For example:

df = pd.DataFrame([None, "Test"], columns=["value"])
print(df.to_string(formatters=[lambda x: "The object is {}".format(type(x))]))

Does not do what a user expects.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 7d32926
python : 3.8.5.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-65-generic
Version : #73-Ubuntu SMP Mon Jan 18 17:25:17 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.2.2
numpy : 1.20.1
pytz : 2021.1
dateutil : 2.8.1
pip : 20.2.4
setuptools : 44.0.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : 1.0.1
pymysql : None
psycopg2 : 2.8.6 (dt dec pq3 ext lo64)
jinja2 : 2.11.3
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : 0.8.5
fastparquet : None
gcsfs : None
matplotlib : 3.3.4
numexpr : None
odfpy : None
openpyxl : 3.0.6
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.0
sqlalchemy : 1.3.23
tables : None
tabulate : None
xarray : 0.16.1
xlrd : None
xlwt : None
numba : None

@EmperorArthur EmperorArthur added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 16, 2021
@mzeitlin11
Copy link
Member

Thanks for reporting and investigating this @EmperorArthur! Contributions to clean this all up would be very welcome!

@mzeitlin11 mzeitlin11 added Output-Formatting __repr__ of pandas objects, to_string and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 1, 2021
@mzeitlin11 mzeitlin11 added this to the Contributions Welcome milestone Jul 1, 2021
@EmperorArthur
Copy link
Author

Looking at it with fresh eyes, to_unit is not needed. Let me fix that.

@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Output-Formatting __repr__ of pandas objects, to_string
Projects
None yet
Development

No branches or pull requests

3 participants