[Numpy-discussion] A question about dtype syntax

Jean-Baptiste Marquette marquett@iap...
Wed Aug 31 08:40:55 CDT 2011

Hi Pierre,

Bingo ! That works. I finally coded like:

            Stats = [(CatBase, round(stats.mean(Data.Ra), 5), round(stats.mean(Data.Dec), 5), len(Sep), round(stats.mean(Sep),4), round(stats.stdev(Sep),4),)]
            StatArray = np.array(Stats, dtype=([('Catalog', 'a15'), ('RaMean', 'f8'), ('DecMean', 'f8'), ('NStars', 'i4'), ('RMS', 'f8'), ('StdDev', 'f8')]))
            print StatArray
            if First:
                StatsAll = StatArray
                First = False
                StatsAll = np.vstack((StatsAll, StatArray))

My next problem deals with the writing of data to a file. I use the command:

np.savetxt(Table, StatsAll, delimiter=' ', fmt=['%15s %.5f %.5f %5d %.4f %.4f'])

which yields:

Traceback (most recent call last):
  File "/Users/marquett/workspace/Distort/src/StatsSep.py", line 44, in <module>
    np.savetxt(Table, StatsAll, delimiter=' ', fmt=['%15s %.5f %.5f %5d %.4f %.4f'])
  File "/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/site-packages/numpy/lib/npyio.py", line 979, in savetxt
    fh.write(asbytes(format % tuple(row) + newline))
TypeError: not enough arguments for format string

I struggled with various unsuccessful fmt syntaxes, and the numpy doc is very discrete about that topic:

fmt : string or sequence of strings
    A single format (%10.5f), a sequence of formats

But I don't find this valid sequence nor an example...


> On Aug 31, 2011, at 12:20 PM, Jean-Baptiste Marquette wrote:
>> Hi Pierre,
>> Thanks for the guess. Unfortunately, I got the same error:
>> [('bs3000k.cat', 280.60341, -7.09118, 9480, 0.2057, 0.14)]
>> Traceback (most recent call last):
>>  File "/Users/marquett/workspace/Distort/src/StatsSep.py", line 40, in <module>
>>    StatsAll = np.array(np.asarray(Stats), dtype=('a15, f8, f8, i4, f8, f8'))
>> ValueError: could not convert string to float: bs3000k.cat
> Of course, silly me
> Your line 40 is actually
>>>>   StatsAll = np.array(np.asarray(Stats), dtype=('a15, f8, f8, i4, f8, f8'))
> With np.asarray(Stats), you're trying to load Stats as an array using a dtype of float by default. Of course, np.asarray is choking on the first element.
> So, try to use instead
>>>>   StatsAll = np.array(Stats, dtype=('a15, f8, f8, i4, f8, f8'))
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20110831/6fdcb99b/attachment-0001.html 

More information about the NumPy-Discussion mailing list