w3resource

NumPy Data type: format_parser() function

numpy.format_parser()function

The format_parser() function is used to convert formats, names, titles description to a dtype.

After constructing the format_parser object, the dtype attribute is the converted data-type: dtype = format_parser(formats, names, titles).dtype

Version: 1.15.0

Syntax:

class numpy.format_parser(formats, names, titles, aligned=False, byteorder=None)

Parameter:

Name Description Required /
Optional
formats : str or list of str The format description, either specified as a string with comma-separated format descriptions in the form 'f8, i4, a5', or a list of format description strings in the form ['f8', 'i4', 'a5']. Required
names : str or list/tuple of str The field names, either specified as a comma-separated string in the form 'col1, col2, col3', or as a list or tuple of strings in the form ['col1', 'col2', 'col3']. An empty list can be used, in that case default field names (‘f0’, ‘f1’, …) are used. Optional
titles : sequence Sequence of title strings. An empty list can be used to leave titles out. Optional
aligned : bool, If True, align the fields by padding as the C-compiler would. Default is False. Optional
byteorder : str, If specified, all the fields will be changed to the provided byte-order. Otherwise, the default byte-order is used. Optional

Return value:

dtype : dtype or Python type - The data type of rep.

Example: numpy.format_parser() function

>>> import numpy as np
>>> np.format_parser(['f8', 'i4', 'a5'], ['col1', 'col2', 'col3'], ['T1', 'T2', 'T3']).dtype
dtype([(('T1', 'col1'), '<f8'), (('T2', 'col2'), '<i4'), (('T3', 'col3'), 'S5')])

Example: numpy.format_parser() function

>>> import numpy as np
>>> np.format_parser(['f8', 'i4', 'a5'], ['col1', 'col2', 'col3'], []).dtype
dtype([('col1', '<f8'), ('col2', '<i4'), ('col3', 'S5')])
>>> np.format_parser(['f8', 'i4', 'a5'], [], []).dtype
dtype([('f0', '<f8'), ('f1', '<i4'), ('f2', 'S5')])

Python - NumPy Code Editor:

Previous: Crreating data types dtype()
Next: Data type information iinfo()



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/numpy/data-type-routines/format_parser.php