5

I am a newbie trying to learn data visuallizaion using python. Actually, I was just trying to follow the example given by a cookbook, like:

import numpy
import os
os.chdir("Home/Desktop/Temporal_folder")
data = numpy.loadtxt ('ch02-data.csv', dtype= 'string', delimiter=',')
print (data)

but somehow it did not work out:

Traceback (most recent call last):
  File "Home/PycharmProjects/Learning/Datavisuallization.py", line 5, in <module>
    data = numpy.loadtxt ('ch02-data.csv', dtype= 'string', delimiter=',')
  File "Home/anaconda/lib/python3.6/site-packages/numpy/lib/npyio.py", line 930, in loadtxt
    dtype = np.dtype(dtype)
TypeError: data type "string" not understood

this is the data I used: "ch02-data.csv"

there were some similar issues posted, but I am not sure I understood what the answer tried to explain. Also, I checked the manual of numpy.loadtext(), still the answer does not seem to be obvious to me... any suggestion? https://docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html

Community
  • 1
  • 1
Jun
  • 309
  • 1
  • 5
  • 16
  • 1
    try `np.str` or just `str` : `data = numpy.loadtxt ('ch02-data.csv', dtype= numpy.str, delimiter=',')` – EdChum Apr 27 '17 at 08:14

2 Answers2

13

Try dtype='str' instead of dtype='string'.

mcb
  • 398
  • 2
  • 12
7

Actually, it works well in Python2, but it doesn't work in Python 3.x, you can try numpy.str

In Python 2, there's no problem:

>>> import numpy as np
>>> np.__version__
'1.12.0'
>>> np.dtype('string')
dtype('S')
>>> np.dtype('str')
dtype('S')

In Python 3, this throw an exception:

>>> import numpy as np
>>> np.__version__
'1.11.3'
>>> np.dtype('string')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: data type "string" not understood
>>> np.dtype('str')
dtype('<U')

you can see more details from this issue.

McGrady
  • 10,869
  • 13
  • 47
  • 69
  • Curious that there has been no action on this bug report (not even a comment by developers) in two years. – alexis Apr 27 '17 at 11:18
  • I'm here August/2018 with the same problem. Apparently there is no solution from python, yet. Using here Python3.6.5 Anaconda version. – Thiago Aug 31 '18 at 08:56