问题
I'm trying to read in data from files using numpy.genfromtxt. I set the names parameter to a comma-separated list of strings, such as
names = ['a', '[b]', 'c']
However, when the array is returned, the dtype.names value returns ('a', 'b', 'c')
The deletechars
parameter is either not set or forced to be None
. I've checked that creating a numpy.ndarray with a dtype that has a named column with square brackets preserves the square brackets, so it must be that genfromtxt is deleting the square brackets. Is there a way to turn off this unexpected feature?
Note, this behavior also occurs if the names
parameter is set to True
. I've tested this in numpy versions 1.6.1 and 1.9.9
回答1:
I've complained about this field name mangling behavior before on the numpy issue tracker and mailing list. It has also cropped up in several previous questions on SO.
In fact, by default np.genfromtxt
will mangle field names even if you specify them directly by passing a list of strings as the names=
parameter:
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')])
This happens despite the fact that field names containing non-alphanumeric characters are perfectly legal:
x2 = np.empty(2, dtype=dtype)
x2[:] = [(1.0, 2.0, 3.0), (4.0, 5.0, 6.0)]
print(repr(x2))
# array([(1.0, 2.0, 3.0), (4.0, 5.0, 6.0)],
# dtype=[('[5]', '<f4'), ('name with spaces', '<f4'), ('(x-1)!\n1', '<f4')])
The logic of this behavior escapes me.
As you've seen, passing None
as the deletechars=
argument is not enough to prevent this from happening, since this argument gets initialized internally to a set of default characters within numpy._iotools.NameValidator.
However, you can pass an empty sequence instead:
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')])
This could be an empty string, list, tuple etc. It doesn't matter as long as its length is zero.
回答2:
In String formatting issue (parantheses vs underline)
I found that dtype=None
is required in addition to the deletechars
parameter:
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')])
With the default dtype
(float), deletechars
is used, but the names pass through a second validator, easy_dtype
which does not get this parameter.
In [170]: np.genfromtxt([b'1,2,3'],names=['a','[b]','xcx'],delimiter=',',deletechars='x')
Out[170]:
array((1.0, 2.0, 3.0),
dtype=[('a', '<f8'), ('b', '<f8'), ('c', '<f8')])
https://github.com/numpy/numpy/pull/4649
Field names can be changed after loading:
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')])
This works for names taken from the file itself:
In [212]: data=np.genfromtxt([b'a [b] *c* d','1 2 3 txt'],dtype=[int,float,int,'S4'],names=True)
来源:https://stackoverflow.com/questions/35234643/numpy-genfromtxt-deleting-square-brackets-in-dtype-names