Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting numpy dtypes to native python types

Tags:

python

numpy

If I have a numpy dtype, how do I automatically convert it to its closest python data type? For example,

numpy.float32 -> "python float" numpy.float64 -> "python float" numpy.uint32  -> "python int" numpy.int16   -> "python int" 

I could try to come up with a mapping of all of these cases, but does numpy provide some automatic way of converting its dtypes into the closest possible native python types? This mapping need not be exhaustive, but it should convert the common dtypes that have a close python analog. I think this already happens somewhere in numpy.

like image 488
conradlee Avatar asked Feb 26 '12 11:02

conradlee


People also ask

How do I change a numpy array to Dtype?

We have a method called astype(data_type) to change the data type of a numpy array. If we have a numpy array of type float64, then we can change it to int32 by giving the data type to the astype() method of numpy array.

Can you convert numpy array to list in Python?

We can use NumPy np. array tolist() function to convert an array to a list. If the array is multi-dimensional, a nested list is returned. For a one-dimensional array, a list with the array elements is returned.

What is the default Dtype of numpy?

The default data type: float_ . The 24 built-in array scalar type objects all convert to an associated data-type object.


2 Answers

Use val.item() to convert most NumPy values to a native Python type:

import numpy as np  # for example, numpy.float32 -> python float val = np.float32(0) pyval = val.item() print(type(pyval))         # <class 'float'>  # and similar... type(np.float64(0).item()) # <class 'float'> type(np.uint32(0).item())  # <class 'int'> type(np.int16(0).item())   # <class 'int'> type(np.cfloat(0).item())  # <class 'complex'> type(np.datetime64(0, 'D').item())  # <class 'datetime.date'> type(np.datetime64('2001-01-01 00:00:00').item())  # <class 'datetime.datetime'> type(np.timedelta64(0, 'D').item()) # <class 'datetime.timedelta'> ... 

(Another method is np.asscalar(val), however it is deprecated since NumPy 1.16).


For the curious, to build a table of conversions of NumPy array scalars for your system:

for name in dir(np):     obj = getattr(np, name)     if hasattr(obj, 'dtype'):         try:             if 'time' in name:                 npn = obj(0, 'D')             else:                 npn = obj(0)             nat = npn.item()             print('{0} ({1!r}) -> {2}'.format(name, npn.dtype.char, type(nat)))         except:             pass 

There are a few NumPy types that have no native Python equivalent on some systems, including: clongdouble, clongfloat, complex192, complex256, float128, longcomplex, longdouble and longfloat. These need to be converted to their nearest NumPy equivalent before using .item().

like image 126
Mike T Avatar answered Sep 23 '22 11:09

Mike T


found myself having mixed set of numpy types and standard python. as all numpy types derive from numpy.generic, here's how you can convert everything to python standard types:

if isinstance(obj, numpy.generic):     return numpy.asscalar(obj) 
like image 37
tm_lv Avatar answered Sep 22 '22 11:09

tm_lv