Skip to content

Commit 27629f0

Browse files
BUG: raise on integer overflow when parsing with insufficient user dtype
1 parent 8d0efca commit 27629f0

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

pandas/_libs/parsers.pyx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,13 +1182,21 @@ cdef class TextReader:
11821182
if user_dtype and na_count is not None:
11831183
if na_count > 0:
11841184
raise ValueError(f"Integer column has NA values in column {i}")
1185-
except OverflowError:
1185+
except OverflowError as err:
1186+
if user_dtype and dtype == 'int64':
1187+
raise err
11861188
result = _try_uint64(self.parser, i, start, end,
11871189
na_filter, na_hashset)
11881190
na_count = 0
11891191

11901192
if result is not None and dtype != 'int64':
1191-
result = result.astype(dtype)
1193+
casted = result.astype(dtype)
1194+
if (casted == result).all():
1195+
result = casted
1196+
else:
1197+
raise TypeError(
1198+
f"cannot safely cast non-equivalent {result.dtype} to {dtype}"
1199+
)
11921200

11931201
return result, na_count
11941202

0 commit comments

Comments
 (0)