Strange enough! but, applying np.genfromtxt() function on the file(goog.csv), wherein the data has been downloaded and stored from a source, produces no error.Following is the code->
import numpy as np
from matplotlib.dates import bytespdate2num
names = ["A", "B", "C", "D", "E", "F", "G"]
my_array1 = np.genfromtxt("goog.csv",
delimiter=',',
skip_header=1,
names=names,
dtype=None,
converters={0: bytespdate2num('%Y-%m-%d')})
print(my_array1["A"])
Output->
[ 736536. 736535. 736534. ..., 730124. 730123. 730122.]
However, applying the same function on a list whose data has been fetched from the same source, being in the same format(.csv), produces the Typerror.Following is the code->
import numpy as np
import request
from matplotlib.dates import bytespdate2num
/*fetching the internet data and store it in a list called stock_data*/
source_code = str(requests.get(goog_url, verify=True, auth=('user', 'pass')).content)
stock_data = []
split_source = source_code.split('\\n')
for line in split_source:
stock_data.append(line)
names = ["A", "B", "C", "D", "E", "F", "G"]
my_array2 = np.genfromtxt(stock_data,
delimiter=',',
skip_header=1,
names=names,
dtype=None,
converters={0: bytespdate2num('%Y-%m-%d')})
print(my_array2["A"])
Output->
TypeError: must be str or None, not bytes
Data in the link goog_url as well as the file (goog.csv) is of the following format->
2017-07-26,153.3500,153.9300,153.0600,153.5000,153.5000,12778195.00
could find no reason for the difference and error in the second case.
Using
decode
like this assumesx
is bytestring:Without the decode it handles the default PY3 string type:
Previously
genfromtxt
opened the file in bytestring mode, and thus required this kind of conversion. But in the current version, it can open the file in unicode, and shouldn't need thedecode
. If your version ofgenfromtxt
accepts anencoding
parameter (it may be even raise a warning about it), it's new.