Skip to content

Commit 8c7efd1

Browse files
authored
Make to_numeric default to correct precision (#36149)
1 parent f4458f2 commit 8c7efd1

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

doc/source/whatsnew/v1.2.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ Timezones
245245

246246
Numeric
247247
^^^^^^^
248-
-
248+
- Bug in :func:`to_numeric` where float precision was incorrect (:issue:`31364`)
249249
-
250250

251251
Conversion

pandas/_libs/src/parse_helper.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ int to_double(char *item, double *p_value, char sci, char decimal,
1818
char *p_end = NULL;
1919
int error = 0;
2020

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

2325
return (error == 0) && (!*p_end);
2426
}

pandas/tests/tools/test_to_numeric.py

+58
Original file line numberDiff line numberDiff line change
@@ -649,3 +649,61 @@ def test_failure_to_convert_uint64_string_to_NaN():
649649
ser = Series([32, 64, np.nan])
650650
result = to_numeric(pd.Series(["32", "64", "uint64"]), errors="coerce")
651651
tm.assert_series_equal(result, ser)
652+
653+
654+
@pytest.mark.parametrize(
655+
"strrep",
656+
[
657+
"243.164",
658+
"245.968",
659+
"249.585",
660+
"259.745",
661+
"265.742",
662+
"272.567",
663+
"279.196",
664+
"280.366",
665+
"275.034",
666+
"271.351",
667+
"272.889",
668+
"270.627",
669+
"280.828",
670+
"290.383",
671+
"308.153",
672+
"319.945",
673+
"336.0",
674+
"344.09",
675+
"351.385",
676+
"356.178",
677+
"359.82",
678+
"361.03",
679+
"367.701",
680+
"380.812",
681+
"387.98",
682+
"391.749",
683+
"391.171",
684+
"385.97",
685+
"385.345",
686+
"386.121",
687+
"390.996",
688+
"399.734",
689+
"413.073",
690+
"421.532",
691+
"430.221",
692+
"437.092",
693+
"439.746",
694+
"446.01",
695+
"451.191",
696+
"460.463",
697+
"469.779",
698+
"472.025",
699+
"479.49",
700+
"474.864",
701+
"467.54",
702+
"471.978",
703+
],
704+
)
705+
def test_precision_float_conversion(strrep):
706+
# GH 31364
707+
result = to_numeric(strrep)
708+
709+
assert result == float(strrep)

0 commit comments

Comments
 (0)