Search code examples
pythonarraysuser-defined-types

Python: Why are my integers being converted to strange negative numbers at np.genfromtxt read in


Why is python inserting strange negative numbers into my array?

I am reading some numerical data from a plain text file like below:

fp=np.genfromtxt("mytextfile.txt", dtype=('int32'), delimiter='\r\n')

The information contained in the file are all positive numbers, file is formatted like below and there are 300000 of these numbers:

12345
45678
1056789
232323
6789010001
1023242556

When I print out the fp read in array, the first half of the array is correctly read in but the last half is strange negative numbers that aren't in my file at all.

How can I get it to read correctly what is in the file?


Solution

  • You have data larger then int32 can save, this causes overflow and makes it negative.

    Numpy's Integers doesn't act like python's integer it's like a C integer

    Try change dtype from int32 to int64 or object may help