Numpy.genfromtxt删除中的方括号数据类型名称

2024-04-19 23:29:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着用numpy.genfromtxt. 我将names参数设置为逗号分隔的字符串列表,例如

names = ['a', '[b]', 'c']

但是,当返回数组时数据类型名称值返回('a', 'b', 'c')

deletechars参数未设置或强制为None。我已经检查过了努比·恩达雷对于具有带方括号的命名列的dtype,保留了方括号,因此genfromtxt一定是在删除方括号。有没有办法关闭这个意想不到的功能?在

注意,如果names参数设置为True,也会发生这种行为。我已经在numpy版本1.6.1和1.9.9中进行了测试


Tags: 字符串numpy名称none列表参数names数组
2条回答

我以前曾抱怨过numpy issue tracker和邮件列表上的字段名损坏行为。它也出现在severalpreviousquestions等等。在

实际上,默认情况下,np.genfromtxt将损坏字段名,即使您通过传递字符串列表作为names=参数直接指定它们:

import numpy as np
from io import BytesIO

s = '[5],name with spaces,(x-1)!\n1,2,3\n4,5,6'

x = np.genfromtxt(BytesIO(s), delimiter=',', names=True)
print(repr(x))
# array([(1.0, 2.0, 3.0), (4.0, 5.0, 6.0)], 
#       dtype=[('5', '<f4'), ('name_with_spaces', '<f4'), ('x1\n1', '<f4')])

names = s.split(',')[:3]
x = np.genfromtxt(BytesIO(s), delimiter=',', skip_header=1, names=names)
print(repr(x))
# array([(1.0, 2.0, 3.0), (4.0, 5.0, 6.0)], 
#       dtype=[('5', '<f4'), ('name_with_spaces', '<f4'), ('x1\n1', '<f4')])

尽管包含非字母数字字符的字段名是完全合法的:

^{pr2}$

我不明白这种行为的逻辑。在


如您所见,传递None作为deletechars=参数不足以防止这种情况发生,因为此参数在内部初始化为^{}内的一组默认字符。在

但是,您可以传递一个空序列:

x = np.genfromtxt(BytesIO(s), delimiter=',', names=True, deletechars='')
print(repr(x))
# array([(1.0, 2.0, 3.0), (4.0, 5.0, 6.0)], 
#       dtype=[('[5]', '<f8'), ('name_with_spaces', '<f8'), ('(x-1)!', '<f8')])

这可以是空字符串、列表、元组等,只要长度为零就无所谓。在

String formatting issue (parantheses vs underline) 我发现除了deletechars参数之外,还需要dtype=None

https://stackoverflow.com/a/32540939/901925

In [168]: np.genfromtxt([b'1,2,3'],names=['a','[b]','xcx'],delimiter=',',deletechars='',dtype=None)
Out[168]: 
array((1, 2, 3), 
      dtype=[('a', '<i4'), ('[b]', '<i4'), ('xcx', '<i4')])

在默认的dtype(float)中,使用deletechars,但是名称要经过第二个验证器easy_dtype,该验证器不获取此参数。在

^{pr2}$

https://github.com/numpy/numpy/pull/4649


加载后可以更改字段名称:

In [205]: data=np.genfromtxt([b'1 2 3 txt'],names=['a','b','c','d'],dtype=[int,float,int,'S4'])

In [206]: data.dtype.names
Out[206]: ('a', 'b', 'c', 'd')

In [207]: data.dtype.names=['a','[b]','*c*','d']

In [208]: data
Out[208]: 
array((1, 2.0, 3, 'txt'), 
      dtype=[('a', '<i4'), ('[b]', '<f8'), ('*c*', '<i4'), ('d', 'S4')])

这适用于从文件本身提取的名称:

In [212]: data=np.genfromtxt([b'a [b] *c* d','1 2 3 txt'],dtype=[int,float,int,'S4'],names=True)

相关问题 更多 >