[Numpy-discussion] difference between dtypes
Robert Kern
robert.kern at gmail.com
Fri Jul 24 03:46:53 EDT 2015
On Wed, Jul 22, 2015 at 7:45 PM, <josef.pktd at gmail.com> wrote:
>
> Is there an explanation somewhere of what different basic dtypes mean,
across platforms and python versions?
>
> >>> np.bool8
> <type 'numpy.bool_'>
> >>> np.bool_
> <type 'numpy.bool_'>
> >>> bool
> <type 'bool'>
>
>
> Are there any rules and recommendations or is it all folks lore?
This may help a little:
http://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html#arrays-dtypes-constructing
Basically, we accept the builtin Python type objects as a dtype argument
and do something sensible with them. float -> np.float64 because Python
floats are C doubles. int -> np.int32 or np.int64 depending on whatever a C
long is (i.e. depending on the 64bitness of your CPU and how your OS
chooses to deal with that). We encode those precision choices as aliases to
the corresponding specific numpy scalar types (underscored as necessary to
avoid shadowing builtins of the same name): np.float_ is np.float64, for
example.
See here for why the aliases to Python builtin types, np.int, np.float,
etc. still exist:
https://github.com/numpy/numpy/pull/6103#issuecomment-123652497
If you just need to pass a dtype= argument and want the precision that
matches the "native" integer and float for your platform, then I prefer to
use the Python builtin types instead of the underscored aliases; they just
look cleaner. If you need a true numpy scalar type (e.g. to construct a
numpy scalar object), of course, you must use one of the numpy scalar
types, and the underscored aliases are convenient for that. Never use the
aliases to the Python builtin types.
--
Robert Kern
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20150724/bbd639c2/attachment.html>
More information about the NumPy-Discussion
mailing list