pandas: BUG: Rounding on float16 type not working

  • 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.


Problem description

I’m trying to round a column to 4 decimal places to store them in the DB. I must use np.float16 type to work with as I need the smallest possible precision, but it’s not working (see output section), if I don’t cast the values and leave them as np.float64 type It’ll work correctly. I think it’s a bug of Pandas as if I run:

np.array([0.37324, 0.12321, 0.23]).astype(np.float16).round(4)

It works as expected, so It doesn’t seem to be a problem for Numpy.

Code Sample

import pandas as pd
import numpy as np
df = pd.DataFrame(np.array([0.37324, 0.12321, 0.23]), columns=['A'])
df.astype(np.float16).round(4)

Running:

pd.DataFrame(np.array([0.37324, 0.12321, 0.23]).astype(np.float16), columns=['A']).round({'A': 4})

Doesn’t work either as it has the same output.

Current Output

          A
0  0.373291
1  0.123230
2  0.229980

Expected Output

          A
0  0.3732
1  0.1232
2  0.23

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None python : 3.7.5.final.0 python-bits : 64 OS : Linux OS-release : 5.3.0-62-generic machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : es_AR.UTF-8 LOCALE : es_AR.UTF-8 pandas : 1.0.5 numpy : 1.18.1 pytz : 2019.3 dateutil : 2.8.1 pip : 18.1 setuptools : 40.8.0 Cython : None pytest : 5.3.5 hypothesis : None sphinx : None blosc : None feather : None xlsxwriter : None lxml.etree : None html5lib : None pymysql : None psycopg2 : None jinja2 : 2.11.1 IPython : None pandas_datareader: None bs4 : None bottleneck : None fastparquet : None gcsfs : None lxml.etree : None matplotlib : None numexpr : None odfpy : None openpyxl : None pandas_gbq : None pyarrow : None pytables : None pytest : 5.3.5 pyxlsb : None s3fs : None scipy : 1.4.1 sqlalchemy : None tables : None tabulate : None xarray : None xlrd : None xlwt : None xlsxwriter : None numba : None

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 15 (7 by maintainers)

Most upvoted comments

Hi Jeff,

I don’t know ! This issue was reproducible, clear and … closed with no action.

The best I can do here is to inform you, then cast my series to float64 and move on.

No hard feelings here, I’m glad this bug was reported because I know why it’s not working as I expected.

Regards,

Hi,

The issue is still present on the latest release of pandas with float32.

Regards,