Home > Blockchain >  What are practical examples of using composite types in numpy?
What are practical examples of using composite types in numpy?

Time:04-26

The numpy documentation describes the option to create custom composite types.

How should one use them?

I can imagine a practical example being a composite time format that would could be used to unify python, hdf5 and numpy as for example:

custom_time = np.dtype( [ ('h', np.int8) , ('m', np.int8), ('s', np.float)] )

t = custom_time(23,59,59.99999)

(the line above doesn't work - I get TypeError: 'numpy.dtype[void]' object is not callable)

Perhaps you have used composite types and have an idea?

CodePudding user response:

Custom types are meant to be used in Numpy arrays and not as stand-alone types. Here is an example:

# Declare the type
custom_time = np.dtype( [ ('h', np.int8) , ('m', np.int8), ('s', np.float64)] )

# Create an array with 2 items of custom_time
arr = np.array([(4, 8, 15.16), (23, 42, 0.815)], dtype=custom_time)

# Access the fields
print(arr['h'])   # [4 23]
print(arr['s'])   # [15.16 0.815]
  • Related