Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ Timezones

Numeric
^^^^^^^
-
- Bug in :func:`to_numeric` where float precision was incorrect (:issue:`31364`)
-

Conversion
Expand Down
4 changes: 3 additions & 1 deletion pandas/_libs/src/parse_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ int to_double(char *item, double *p_value, char sci, char decimal,
char *p_end = NULL;
int error = 0;

*p_value = xstrtod(item, &p_end, decimal, sci, '\0', 1, &error, maybe_int);
/* Switch to precise xstrtod GH 31364 */
*p_value = precise_xstrtod(item, &p_end, decimal, sci, '\0', 1,
&error, maybe_int);

return (error == 0) && (!*p_end);
}
Expand Down
58 changes: 58 additions & 0 deletions pandas/tests/tools/test_to_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,3 +649,61 @@ def test_failure_to_convert_uint64_string_to_NaN():
ser = Series([32, 64, np.nan])
result = to_numeric(pd.Series(["32", "64", "uint64"]), errors="coerce")
tm.assert_series_equal(result, ser)


@pytest.mark.parametrize(
"strrep",
[
"243.164",
"245.968",
"249.585",
"259.745",
"265.742",
"272.567",
"279.196",
"280.366",
"275.034",
"271.351",
"272.889",
"270.627",
"280.828",
"290.383",
"308.153",
"319.945",
"336.0",
"344.09",
"351.385",
"356.178",
"359.82",
"361.03",
"367.701",
"380.812",
"387.98",
"391.749",
"391.171",
"385.97",
"385.345",
"386.121",
"390.996",
"399.734",
"413.073",
"421.532",
"430.221",
"437.092",
"439.746",
"446.01",
"451.191",
"460.463",
"469.779",
"472.025",
"479.49",
"474.864",
"467.54",
"471.978",
],
)
def test_precision_float_conversion(strrep):
# GH 31364
result = to_numeric(strrep)

assert result == float(strrep)