Python 'astype' not working
19,145
astype
returns a new array. You need to assign the result to x
:
In [298]: x = x.astype(int)
In [299]: x
Out[299]: array([1, 2, 2])
Author by
user2590144
Updated on June 12, 2022Comments
-
user2590144 almost 2 years
I am currently using Spyder from Anaconda and I am trying to convert an array containing type float to type int:
x = np.array([1, 2, 2.5]) x.astype(int) print x
The result still comes out unchanged:
[1. 2. 2.5]
Thoughts?
-
user2590144 about 10 yearsWhen I print it in the program now, it works. But when I try to write it out to a file, it gets written out in float/scientific notation. How do I get it to write to a file as int? (I am using: np.savetxt(outfile_name, array, header = str(dimension), comments = '') )
-
unutbu about 10 yearsUse
np.savetxt(outfile_name, array, fmt='%d', ...)
-
Debadatta about 5 yearsThanks, totally forgot that astype returns a value