向结构化的numpy数组添加字段(4) [英] Adding a field to a structured numpy array (4)
问题描述
此问题已在之前解决(在此处,此处和中添加一个新字段,编号为numpy-genfromtxt-输出.
This has been addressed before (here, here and here). I want to add a new field to a structure array returned by numpy genfromtxt
(also asked here).
我的新问题是我正在读取的csv文件只有一个标题行和一个数据行:
My new problem is that the csv file I'm reading has only a header line and a single data row:
output-Summary.csv
:
Wedge, DWD, Yield (wedge), Efficiency
1, 16.097825, 44283299.473156, 2750887.118836
我正在通过 genfromtxt
读取它,并计算一个新值'tl'
:
I'm reading it via genfromtxt
and calculate a new value 'tl'
:
test_out = np.genfromtxt('output-Summary.csv', delimiter=',', names=True)
tl = 300 / test_out['DWD']
test_out
看起来像这样:
array((1., 16.097825, 44283299.473156, 2750887.118836),
dtype=[('Wedge', '<f8'), ('DWD', '<f8'), ('Yield_wedge', '<f8'), ('Efficiency', '<f8')])
使用 recfunctions.append_fields
(如上面的示例1-3中所述)无法对大小为1的数组使用 len()
:
Using recfunctions.append_fields
(as suggested in the examples 1-3 above) fails over the use of len()
for the size 1 array:
from numpy.lib import recfunctions as rfn
rfn.append_fields(test_out,'tl',tl)
TypeError: len() of unsized object
搜索替代方法(答案之一此处)我发现 mlab.rec_append_fields
效果很好(但已弃用):
Searching for alternatives (one of the answers here) I find that mlab.rec_append_fields
works well (but is deprecated):
mlab.rec_append_fields(test_out,'tl',tl)
C:\ProgramData\Anaconda3\lib\site-packages\ipykernel_launcher.py:1: MatplotlibDeprecationWarning: The rec_append_fields function was deprecated in version 2.2.
"""Entry point for launching an IPython kernel.
rec.array((1., 16.097825, 44283299.473156, 2750887.118836, 18.63605798),
dtype=[('Wedge', '<f8'), ('DWD', '<f8'), ('Yield_wedge', '<f8'), ('Efficiency', '<f8'), ('tl', '<f8')])
I can also copy the array over to a new structured array "by hand" as suggested here. This works:
test_out_new = np.zeros(test_out.shape, dtype=new_dt)
for name in test_out.dtype.names:
test_out_new[name]=test_out[name]
test_out_new['tl']=tl
因此,总而言之,有没有一种方法可以使 recfunctions.append_fields
与单行csv文件的 genfromtxt
输出配合使用?我真的更愿意使用一种标准的方式来处理此问题,而不是使用家庭酿造方法.
So in summary - is there a way to get recfunctions.append_fields
to work with the genfromtxt
output from my single row csv file?
I would really rather use a standard way to handle this rather than a home brew..
推荐答案
将数组(和新字段)重塑为大小(1,).仅用一行, genfromtxt
将数据加载为0d数组,形状为(). rfn
代码并未得到广泛使用,并且也不如应有的健壮性.换句话说,标准方式"还是有点小问题.
Reshape the array (and new field) to size (1,). With just one line, the genfromtxt
is loading the data as a 0d array, shape (). The rfn
code isn't heavily used, and isn't a robust as it should be. In other words, the 'standard way' is still bit buggy.
例如:
In [201]: arr=np.array((1,2,3), dtype='i,i,i')
In [202]: arr.reshape(1)
Out[202]: array([(1, 2, 3)], dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<i4')])
In [203]: rfn.append_fields(arr.reshape(1), 't1',[1], usemask=False)
Out[203]:
array([(1, 2, 3, 1)],
dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<i4'), ('t1', '<i8')])
home_brew没问题.大多数 rfn
函数都使用该机制-定义一个新的dtype,使用该dtype创建一个收件人数组,然后按名称逐个复制字段.
Nothing wrong with the home_brew. Most of the rfn
functions use that mechanism - define a new dtype, create a recipient array with that dtype, and copy the fields over, name by name.
这篇关于向结构化的numpy数组添加字段(4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!