I am running the exact same code on both windows and mac, with python 3.5 64 bit.
On windows, it looks like this:
>>> import numpy as np
>>> preds = np.zeros((1, 3), dtype=int)
>>> p = [6802256107, 5017549029, 3745804973]
>>> preds[0] = p
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
preds[0] = p
OverflowError: Python int too large to convert to C long
However, this code works fine on my mac. Could anyone help explain why or give a solution for the code on windows? Thanks so much!
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You’ll get that error once your numbers are greater than sys.maxsize:
>>> p = [sys.maxsize] >>> preds[0] = p >>> p = [sys.maxsize+1] >>> preds[0] = p Traceback (most recent call last): File "<stdin>", line 1, in <module> OverflowError: Python int too large to convert to C long
You can confirm this by checking:
>>> import sys >>> sys.maxsize 2147483647
To take numbers with larger precision, don’t pass an int type which uses a bounded C integer behind the scenes. Use the default float:
>>> preds = np.zeros((1, 3))
Method 2
You can use dtype=np.int64 instead of dtype=int
Method 3
Could anyone help explain why
Numpy arrays normally* have fixed size elements, including integers of various sizes, single or double precision floating point numbers, fixed length byte and Unicode strings and structures built up from the aforementioned types.
In Python 2 a python “int” was equivalent to a C long. In Python 3 an “int” is an arbitrary precision type but numpy still uses “int” it to represent the C type “long” when creating arrays.
The size of a C long is platform dependent. On windows it is always 32-bit. On unix-like systems it is normally 32 bit on 32 bit systems and 64 bit on 64 bit systems.
or give a solution for the code on windows? Thanks so much!
Choose a data type whose size is not platform dependent. You can find the list at https://docs.scipy.org/doc/numpy/reference/arrays.scalars.html#arrays-scalars-built-in the most sensible choice would probably be np.int64
* Numpy does allow arrays of python objects, but I don’t think they are widely used.
Method 4
Convert to float:
import pandas as pd
df = pd.DataFrame()
l_var_l = [8258255190131389999999000003296, 50661]
df['temp'] = l_var_l
df['temp'] = df['temp'].astype(int)
Above fails with error:
OverflowError: Python int too large to convert to C long.
Now try with float conversion:
df['temp'] = df['temp'].astype(float)
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0