python - Numpy.genfromtxt deleting square brackets in dtype.names -
i'm trying read in data files using numpy.genfromtxt. set names parameter comma-separated list of strings, such
names = ['a', '[b]', 'c']
however, when array returned, dtype.names value returns ('a', 'b', 'c')
the deletechars
parameter either not set or forced none
. i've checked creating numpy.ndarray dtype has named column square brackets preserves square brackets, must genfromtxt deleting square brackets. there way turn off unexpected feature?
note, behavior occurs if names
parameter set true
. i've tested in numpy versions 1.6.1 , 1.9.9
i've complained field name mangling behavior before on numpy issue tracker , mailing list. has cropped in several previous questions on so.
in fact, default np.genfromtxt
mangle field names if specify them directly passing list of strings names=
parameter:
import numpy np io import bytesio s = '[5],name 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 fact field names containing non-alphanumeric characters 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 spaces', '<f4'), ('(x-1)!\n1', '<f4')])
the logic of behavior escapes me.
as you've seen, passing none
deletechars=
argument not enough prevent happening, since argument gets initialized internally set of default characters within numpy._iotools.namevalidator
.
however, can pass 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 empty string, list, tuple etc. doesn't matter long length zero.
Comments
Post a Comment