Skip to content

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

@EmperorArthur

Description

@EmperorArthur
  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions