1

I use numpy's np.genfromtxt(FileName,delimiter=",",names=True) to import a csv file into a numpy array. Everything seems to work fine, except that my header has names with a colon (e.g. Points:1), but numpy sets the key for this data column without the colon (e.g. Points1).

Why is this happening and is there any way to prevent it?

MWE

min.csv:

"Time", "Points:0", "Points:1"
0.0, 1.0, 2.0
3.0,4.0,5.0

script:

import numpy as np
data = np.genfromtxt("min.csv",delimiter=",",names=True)
print data
print data.dtype

output:

[( 0.,  1.,  2.) ( 3.,  4.,  5.)]
[('Time', '<f8'), ('Points0', '<f8'), ('Points1', '<f8')]
Miguel
  • 1,293
  • 1
  • 13
  • 30

2 Answers2

4

From the numpy documentation under 'validating names':

deletechars Gives a string combining all the characters that must be deleted from the name. By default, invalid characters are ~!@#$%^&*()-=+~\|]}[{';: /?.>,< .

So all you need to do is to add your own deletechars value. In the example below I just copied the string from the documentation and removed the colon:

import numpy as np
data = np.genfromtxt(
    "min.csv",delimiter=",",names=True,
    deletechars="~!@#$%^&*()-=+~\|]}[{'; /?.>,<",
)
print data
print data.dtype

And the result looks like this:

[( 0.,  1.,  2.) ( 3.,  4.,  5.)]
[('Time', '<f8'), ('Points:0', '<f8'), ('Points:1', '<f8')]
Thomas Kühn
  • 9,412
  • 3
  • 47
  • 63
2

You need to use the deletechars parameter to control what's removed from the names. Normally the names are 'sanitized' so they would work as recarray names (i.e. as valid Python variable/attribute names). In the past I found the dtype=None was also required to get this work (that may be corrected now).

Numpy.genfromtxt deleting square brackets in dtype.names

In [1137]: txt = b""""Time", "Points:0", "Points:1"
      ...: 0.0, 1.0, 2.0
      ...: 3.0,4.0,5.0"""
In [1141]: np.genfromtxt(txt.splitlines(),delimiter=',',names=True,deletechars='',dtype=None)
Out[1141]: 
array([( 0.,  1.,  2.), ( 3.,  4.,  5.)],
      dtype=[('Time', '<f8'), ('Points:0', '<f8'), ('Points:1', '<f8')])
hpaulj
  • 221,503
  • 14
  • 230
  • 353