Home › Python

OverflowError: Python int too large to convert to C long — how to fix

Traceback (most recent call last):
  File "read.py", line 3, in <module>
    csv.field_size_limit(sys.maxsize)
OverflowError: Python int too large to convert to C long

Quick fix: For csv.field_size_limit(sys.maxsize) on Windows use csv.field_size_limit(2**31 - 1). For NumPy/pandas, use a wider dtype (dtype=np.int64, np.uint64), float, or dtype=object for arbitrarily large ints; read big ID columns as strings (dtype={"id": str}).

What it means

Python's int has no size limit, but C integers do. When a library hands your number to C code that expects a long, the value must fit:

So code that works on Linux can raise this on Windows with the same values.

Cause 1: csv.field_size_limit(sys.maxsize) on Windows

A popular snippet for "field larger than field limit" errors:

import csv, sys
csv.field_size_limit(sys.maxsize)   # OverflowError on Windows

sys.maxsize is 2**63 - 1 on 64-bit Windows, which doesn't fit a 32-bit long. Use the 32-bit maximum, which is plenty for any real field:

csv.field_size_limit(2**31 - 1)

Or, portable:

max_int = sys.maxsize
while True:
    try:
        csv.field_size_limit(max_int)
        break
    except OverflowError:
        max_int //= 10

Cause 2: NumPy array with a value outside its dtype

import numpy as np
np.array([2**63], dtype=np.int64)   # OverflowError

NumPy integer dtypes are fixed-size. Pick one that fits:

np.array([2**63], dtype=np.uint64)   # unsigned, up to 2**64 - 1
np.array([2**70], dtype=float)       # approximate
np.array([2**70], dtype=object)      # exact Python ints, slower

The exact message differs between NumPy versions (newer ones say Python integer ... out of bounds for int64), but the cause is the same.

On Windows with NumPy 1.x, the default integer type is 32-bit, so np.array([3_000_000_000]) style code or dtype=int can overflow far sooner. Specify dtype=np.int64 explicitly, or upgrade to NumPy 2, where the default integer is 64-bit on Windows too.

Cause 3: pandas columns with huge IDs

Very large numeric identifiers (some Twitter/Discord IDs, hashes, account numbers) can overflow when pandas converts them, for example in astype(int) or when building an Int64 column. IDs aren't for arithmetic, so read them as strings:

df = pd.read_csv("data.csv", dtype={"id": str})

Cause 4: ctypes, struct and C extensions

Passing a big Python int to a C function declared with c_long, or packing it with struct.pack("l", ...), has the same limits. Use a wider type (c_longlong, c_uint64, format "q"/"Q") that matches what the C side expects.

Cause 5: timestamps and sizes from bad data

Values like datetime.fromtimestamp(1e20) or range()/seek() with a corrupt huge number can also hit C limits (sometimes with a slightly different message). Validate the input: often the "number" is milliseconds treated as seconds, or a parse error. To check a timestamp quickly, use the Unix timestamp converter.

Last updated 2026-12-01.