John Crow John Crow - 17 days ago 7
Python Question

Numpy data types (dtype.char)

I have an array where

arr.dtype.char = d
. Numpy docs indicate that
d
represents a double-precision floating-point number. If I want this array simply as an integer I can change the datatype using something like
arr.astype('int64')
. However this doesn't change the value given by
arr.dtype.char
.

I don't understand what is going on here. A module I am using doesn't support data type
d
. How do I change it? The module supports data types
B
,
H
,
h
,
f
, what are each of these used for?

Answer

Well actually to change the arr datatype, you have to write:

arr_int = arr.astype('int64')

As for the data types, read the docs.

Edit: As noted in the comments by Sven Marnach, arr.astype('int64') returns a copy of the array. It doesn't change arr itself at all.

Comments