Column y below should be [‘Reg’, ‘Reg’, ‘Swp’, ‘Swp’]
In [1]: pd.read_csv('/tmp/test3.csv')
Out[1]:
x,y
^@^@^@,Reg
^@^@^@,Reg
I,Swp
I,Swp
In [2]: ! cat /tmp/test3.csv
x y
0
1 NaN NaN
2 I Swp
3 I Swp
In [3]: f = open('/tmp/test3.csv', 'rb'); print(repr(f.read()))
'x,y\n \x00\x00\x00,Reg\n \x00\x00\x00,Reg\nI,Swp\nI,Swp\n'
Yes, I could reproduce the problem, but don’t know how to fix it with
pd.read_csv. Here is a workaround:Note that with
names = Truethe first valid line of the csv is interpreted as column names (and therefore does not affect the dtype of the values on the subsequent lines.) Thus, if the csv file contains numerical data such asThen genfromtxt will assign a numerical dtype to the third column (
<i4in this case).However, if the numerical data is intermingled with bytes such as
'\x00'then genfromtxt will be unable to recognize this column as numerical and will therefore resort to assigning a string dtype. Nevertheless, you can force the dtype of the columns by manually assigning thedtypeparameter. For example,sets the first column
xto have dtype|i4(4-byte integers) and the second columnyto have dtype|S3(3-byte string). See this doc page for more information on available dtypes.