I am reading in a file using numpy.genfromtxt which brings in columns of both strings and numeric values. One thing I need to do is detect the length of the input. This is all fine provided there are more than one value read into each array.
But...if there is only one element in the resulting array, the logic fails. I can recreate an example here:
import numpy as np
a = np.array(2.3)
len(a) returns an error saying:
TypeError: len() of unsized object
however, If a has 2 or more elements, len() behaves as one would expect.
import numpy as np
a = np.array([2.3,3.6])
len(a) returns 2
My concern here is, if I use some strange exception handling, I can't distinguish between a being empty and a having length = 1.
EDIT: @noskio suggested setting a = np.array([2.3]). The problem is, the actual genesis of a is by using numpy.genfromtxt. The code looks like this:
import numpy as np
indata = np.genfromtxt(some_filename, names=True,dtype=None)
a = indata['one_col_headername']
As a result, if indata is only one row in the file, a is a 0-d array.
array([2])
is an array with one element and 1 dimension.array(2)
is an array with zero rank or zero dimensions. – endolith Dec 16 '12 at 5:12