Skip to content

BUG: to_numeric str to float has missing digit precision #44145

Description

@NHanser

  • 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

import pandas as pd
pd.set_option("display.precision", 20)
df = pd.DataFrame(data=["0.01234567890123456789", "0.00498124967521367"], columns=["string"])
df["float"] = pd.to_numeric(df["string"])
df.head()

Issue Description

The conversion to string shall at least return a precision of 17 digits after decimal point.
The first number exceeds voluntarily the maximum precision of 17 digits allowed by FLOAT64 representation. The second number has exactly 17 digits

Expected Behavior

In 1.1.5 version, output:

                   string                   float
0  0.01234567890123456789  0.01234567890123457017
1     0.00498124967521367  0.00498124967521367066

When 1.2.0 version (and further versions) outputs:

                   string                   float
0  0.01234567890123456789  0.01234567890123450078
1     0.00498124967521367  0.00498124967521360040

I think the behaviour fro 1.1.5 should be the right one, keeping a 17 digits precision while converting string to float.

Installed Versions

Details

INSTALLED VERSIONS

commit : 945c9ed
python : 3.8.11.final.0
python-bits : 64
OS : Linux
OS-release : 3.10.0-693.el7.x86_64
Version : #1 SMP Tue Aug 22 21:09:27 UTC 2017
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.3.4
numpy : 1.19.5
pytz : 2021.3
dateutil : 2.8.2
pip : 21.3
setuptools : 58.2.0
Cython : 0.29.24
pytest : 6.1.2
hypothesis : None
sphinx : 4.2.0
blosc : None
feather : None
xlsxwriter : 3.0.1
lxml.etree : 4.6.3
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.2
IPython : 7.28.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : 2021.10.1
fastparquet : None
gcsfs : None
matplotlib : 3.4.3
numexpr : 2.7.3
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : 5.0.0
pyxlsb : None
s3fs : None
scipy : 1.7.1
sqlalchemy : 1.4.25
tables : 3.6.1
tabulate : 0.8.9
xarray : 0.19.0
xlrd : 2.0.1
xlwt : 1.3.0
numba : 0.54.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions