Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the available datatypes for 'dtype' with NumPy's loadtxt() an genfromtxt?

Tags:

python

numpy

What are the available numpy.loadtxt or numpy.genfromtxt for importing table data with varying datatypes, and what are the available abbreviations for the use (e.g. i32 for integer)?

This post demonstrates the use of conditions, which I was curious if somebody might elaborate on.

like image 983
ryanjdillon Avatar asked Dec 21 '12 20:12

ryanjdillon


People also ask

What does the Loadtxt () function do in NumPy?

loadtxt() function. The loadtxt() function is used to load data from a text file. Each row in the text file must have the same number of values.

What is the default datatype that NP Loadtxt () uses for numbers?

Data-type of the resulting array; default: float. If this is a structured data-type, the resulting array will be 1-dimensional, and each row will be interpreted as an element of the array. In this case, the number of columns used must match the number of fields in the data-type.

What is NumPy Genfromtxt?

genfromtxt() function. The genfromtxt() used to load data from a text file, with missing values handled as specified. Each line past the first skip_header lines is split at the delimiter character, and characters following the comments character are discarded.


2 Answers

In addition to np.sctypeDict, there are these variables:

In [141]: np.typecodes Out[141]:  {'All': '?bhilqpBHILQPefdgFDGSUVOMm',  'AllFloat': 'efdgFDG',  'AllInteger': 'bBhHiIlLqQpP',  'Character': 'c',  'Complex': 'FDG',  'Datetime': 'Mm',  'Float': 'efdg',  'Integer': 'bhilqp',  'UnsignedInteger': 'BHILQP'}  In [143]: np.sctypes Out[143]:  {'complex': [numpy.complex64, numpy.complex128, numpy.complex192],  'float': [numpy.float16, numpy.float32, numpy.float64, numpy.float96],  'int': [numpy.int8, numpy.int16, numpy.int32, numpy.int32, numpy.int64],  'others': [bool, object, str, unicode, numpy.void],  'uint': [numpy.uint8, numpy.uint16, numpy.uint32, numpy.uint32, numpy.uint64]} 
like image 99
hpaulj Avatar answered Oct 02 '22 10:10

hpaulj


Generic info about dtypes: http://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html

From http://docs.scipy.org/doc/numpy/reference/arrays.scalars.html#arrays-scalars-built-in

In NumPy, there are 24 new fundamental Python types to describe different types of scalars. These type descriptors are mostly based on the types available in the C language that CPython is written in, with several additional types compatible with Python’s types.

And what I didn't realise, is:

The C-like names are associated with character codes, which are shown in the table. Use of the character codes, however, is discouraged.

I doubt the numpy code/doc base is going anyway anytime soon, so that says it all I guess!

like image 37
Jon Clements Avatar answered Oct 02 '22 10:10

Jon Clements