Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
696 views
in Technique[技术] by (71.8m points)

python - Numpy.genfromtxt deleting square brackets in dtype.names

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)!
1,2,3
4,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
1', '<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
1', '<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)!
1', '<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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...