BUG: float formatters ignore nullable floating extension type · Issue #39336 · pandas-dev/pandas (original) (raw)

Problem description

Pandas float formatters ignore the nullable floating extension types Float64 which was introduced with 1.2.0.
In particular, this is extremely confusing when using DataFrame.convert_dtypes since the default is now with convert_floating=True which converts all float colums to Float64 columns.

That is, the following does not produce the expected float formatting:

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

gives

      value
0  0.123457
1  1.123457
2  2.123457

I am not sure, whether this classifies as a bug or missing documentation (or both) however having not heard of this new nullable floating type Float64 I did not notice this subtle difference at first. Also, I did not expect DataFrame.convert_dtypes() to result in a non-formatable DataFrame. So, if Float64 is not expected to format as float64, the documentation should make this clear.
However, I believe the best would be that Float64 formats just as the regular float dtype.

Expected Output

The formatting should be applied to the Float64 dtype just as when using the float dtype such as

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

which gives

  value
0  0.12
1  1.12
2  2.12

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 3e89b4c4b1580aa890023fc550774e63d499da25 python : 3.8.5.final.0 python-bits : 64 OS : Linux OS-release : 5.10.9-arch1-1 Version : #1 SMP PREEMPT Tue, 19 Jan 2021 22:06:06 +0000 machine : x86_64 processor : byteorder : little LC_ALL : None LANG : en_DK.UTF-8 LOCALE : en_DK.UTF-8 pandas : 1.2.0 numpy : 1.19.2 pytz : 2020.5 dateutil : 2.8.1 pip : 20.3.3 setuptools : 51.0.0.post20201207 Cython : None pytest : None hypothesis : None sphinx : None blosc : None feather : None xlsxwriter : None lxml.etree : None html5lib : None pymysql : None psycopg2 : None jinja2 : None IPython : None pandas_datareader: None bs4 : None bottleneck : None fsspec : None fastparquet : None gcsfs : None matplotlib : 3.3.2 numexpr : None odfpy : None openpyxl : None pandas_gbq : None pyarrow : None pyxlsb : None s3fs : None scipy : 1.5.2 sqlalchemy : None tables : None tabulate : None xarray : None xlrd : None xlwt : None numba : None