Skip to content

Commit ab2fab8

Browse files
committed
fix: don't throw error when casting is expected
1 parent fc10a5f commit ab2fab8

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

pandas/_libs/parsers.pyx

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,7 @@ cdef class TextReader:
10591059
if col_dtype is not None:
10601060
col_res, na_count = self._convert_with_dtype(
10611061
col_dtype, i, start, end, na_filter,
1062-
1, na_hashset, na_fset)
1062+
1, na_hashset, na_fset, False)
10631063

10641064
# Fallback on the parse (e.g. we requested int dtype,
10651065
# but its actually a float).
@@ -1077,7 +1077,7 @@ cdef class TextReader:
10771077

10781078
try:
10791079
col_res, na_count = self._convert_with_dtype(
1080-
dt, i, start, end, na_filter, 0, na_hashset, na_fset)
1080+
dt, i, start, end, na_filter, 0, na_hashset, na_fset, True)
10811081
except ValueError as e:
10821082
if str(e) == "Number is float":
10831083
maybe_int = False
@@ -1089,15 +1089,15 @@ cdef class TextReader:
10891089
# column AS IS with object dtype.
10901090
col_res, na_count = self._convert_with_dtype(
10911091
np.dtype("object"), i, start, end, 0,
1092-
0, na_hashset, na_fset)
1092+
0, na_hashset, na_fset, False)
10931093
except OverflowError:
10941094
try:
10951095
col_res, na_count = _try_pylong(self.parser, i, start,
10961096
end, na_filter, na_hashset)
10971097
except ValueError:
10981098
col_res, na_count = self._convert_with_dtype(
10991099
np.dtype("object"), i, start, end, 0,
1100-
0, na_hashset, na_fset)
1100+
0, na_hashset, na_fset, False)
11011101

11021102
if col_res is not None:
11031103
break
@@ -1145,7 +1145,7 @@ cdef class TextReader:
11451145
bint na_filter,
11461146
bint user_dtype,
11471147
kh_str_starts_t *na_hashset,
1148-
set na_fset):
1148+
set na_fset, bint raise_on_float):
11491149
if isinstance(dtype, CategoricalDtype):
11501150
# TODO: I suspect that _categorical_convert could be
11511151
# optimized when dtype is an instance of CategoricalDtype
@@ -1186,14 +1186,14 @@ cdef class TextReader:
11861186

11871187
elif dtype.kind in "iu":
11881188
try:
1189-
result, na_count = _try_int64(self.parser, i, start,
1190-
end, na_filter, na_hashset)
1189+
result, na_count = _try_int64(self.parser, i, start, end,
1190+
na_filter, na_hashset, raise_on_float)
11911191
if user_dtype and na_count is not None:
11921192
if na_count > 0:
11931193
raise ValueError(f"Integer column has NA values in column {i}")
11941194
except OverflowError:
11951195
result = _try_uint64(self.parser, i, start, end,
1196-
na_filter, na_hashset)
1196+
na_filter, na_hashset, raise_on_float)
11971197
na_count = 0
11981198

11991199
if result is not None and dtype != "int64":
@@ -1752,7 +1752,8 @@ cdef int _try_double_nogil(parser_t *parser,
17521752

17531753
cdef _try_uint64(parser_t *parser, int64_t col,
17541754
int64_t line_start, int64_t line_end,
1755-
bint na_filter, kh_str_starts_t *na_hashset):
1755+
bint na_filter, kh_str_starts_t *na_hashset,
1756+
bint raise_on_float):
17561757
cdef:
17571758
int error
17581759
Py_ssize_t lines
@@ -1774,9 +1775,10 @@ cdef _try_uint64(parser_t *parser, int64_t col,
17741775
if error == ERROR_OVERFLOW:
17751776
# Can't get the word variable
17761777
raise OverflowError("Overflow")
1777-
elif error == ERROR_IS_FLOAT:
1778+
elif raise_on_float and error == ERROR_IS_FLOAT:
17781779
raise ValueError("Number is float")
1779-
return None
1780+
elif not raise_on_float or error != ERROR_IS_FLOAT:
1781+
return None, None
17801782

17811783
if uint64_conflict(&state):
17821784
raise ValueError("Cannot convert to numerical dtype")
@@ -1826,7 +1828,7 @@ cdef int _try_uint64_nogil(parser_t *parser, int64_t col,
18261828

18271829
cdef _try_int64(parser_t *parser, int64_t col,
18281830
int64_t line_start, int64_t line_end,
1829-
bint na_filter, kh_str_starts_t *na_hashset):
1831+
bint na_filter, kh_str_starts_t *na_hashset, bint raise_on_float):
18301832
cdef:
18311833
int error, na_count = 0
18321834
Py_ssize_t lines
@@ -1846,9 +1848,10 @@ cdef _try_int64(parser_t *parser, int64_t col,
18461848
if error == ERROR_OVERFLOW:
18471849
# Can't get the word variable
18481850
raise OverflowError("Overflow")
1849-
elif error == ERROR_IS_FLOAT:
1851+
elif raise_on_float and error == ERROR_IS_FLOAT:
18501852
raise ValueError("Number is float")
1851-
return None, None
1853+
elif not raise_on_float or error != ERROR_IS_FLOAT:
1854+
return None, None
18521855

18531856
return result, na_count
18541857

0 commit comments

Comments
 (0)