cf.Index.dtype

Index.dtype

The numpy data type of the data array.

By default this is the data type with the smallest size and smallest scalar kind to which all sub-arrays of the master data array may be safely cast without loss of information. For example, if the sub-arrays have data types ‘int64’ and ‘float32’ then the master data array’s data type will be ‘float64’; or if the sub-arrays have data types ‘int64’ and ‘int32’ then the master data array’s data type will be ‘int64’.

Setting the data type to a numpy.dtype object, or any object convertible to a numpy.dtype object, will cause the master data array elements to be recast to the specified type at the time that they are next accessed, and not before. This does not immediately change the master data array elements, so, for example, reinstating the original data type prior to data access results in no loss of information.

Deleting the data type forces the default behaviour. Note that if the data type of any sub-arrays has changed after dtype has been set (which could occur if the data array is accessed) then the reinstated default data type may be different to the data type prior to dtype being set.

Examples

>>> f.dtype
dtype('float64')
>>> type(f.dtype)
<type 'numpy.dtype'>
>>> print(f.array)
[0.5 1.5 2.5]
>>> import numpy
>>> f.dtype = numpy.dtype(int)
>>> print(f.array)
[0 1 2]
>>> f.dtype = bool
>>> print(f.array)
[False  True  True]
>>> f.dtype = 'float64'
>>> print(f.array)
[ 0.  1.  1.]
>>> print(f.array)
[0.5 1.5 2.5]
>>> f.dtype = int
>>> f.dtype = bool
>>> f.dtype = float
>>> print(f.array)
[ 0.5  1.5  2.5]