import numpy as np
#read in data and extract columns
cols = np.loadtxt('CST210.txt')
Day1Attendence = cols[:,0]
Day2Attendence = cols[:,1]
#calculate the total attendence over the two days
TotalAttendence = Day1Attendence + Day2Attendence
#create second array from Day1Attendence and Day2Attendence
outArray = np.column_stack((Day1Attendence,Day2Attendence))
#save to test file
np.savetxt('Totalattandence.txt',outArray, delimiter = 'A')
This is the error code I'm getting
runfile('/home/kasey/Documents/module1code.py',
wdir='/home/kasey/Documents') Traceback (most recent call last):
File "", line 1, in
runfile('/home/kasey/Documents/module1code.py', wdir='/home/kasey/Documents')
File
"/opt/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py",
line 668, in runfile
execfile(filename, namespace)
File
"/opt/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py",
line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/home/kasey/Documents/module1code.py", line 13, in
cols = np.loadtxt('CST210.txt')
File
"/opt/anaconda3/lib/python3.7/site-packages/numpy/lib/npyio.py", line
1101, in loadtxt
for x in read_data(_loadtxt_chunksize):
File
"/opt/anaconda3/lib/python3.7/site-packages/numpy/lib/npyio.py", line
1028, in read_data
items = [conv(val) for (conv, val) in zip(converters, vals)]
File
"/opt/anaconda3/lib/python3.7/site-packages/numpy/lib/npyio.py", line
1028, in
items = [conv(val) for (conv, val) in zip(converters, vals)]
File
"/opt/anaconda3/lib/python3.7/site-packages/numpy/lib/npyio.py", line
746, in floatconv
return float(x)
ValueError: could not convert string to float: '"""'
Related
Recently I have been working on a code and got stuck for days on this error. Basically the program plots a 3D colormap from csv file. I am using Python 3 with anaconda3.
https://drive.google.com/drive/folders/1hfL_TbfWwD6uZCgxiOa-xjWT1ChL2PUs?usp=sharing
This is the code:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
DataX_Y_1D = np.loadtxt("datacsv_1d_xy.csv", delimiter=",")
X, Y = np.meshgrid(DataX_Y_1D[:,0], DataX_Y_1D[:,1])
Z = np.loadtxt("datacsv_2d_Z.csv", delimiter=",")
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z)
plt.show()
The problem like this:
File "/Users/lenguyen/Desktop/test_3D/plot_3D_4.py", line 1, in <module>
DataAll1D = np.loadtxt("datacsv_1d.csv", delimiter=",")
NameError: name 'np' is not defined
(base) lenguyen#ntmle2 test_3D % python plot_3D_4.py
Traceback (most recent call last):
File "/Users/lenguyen/Desktop/test_3D/plot_3D_4.py", line 5, in <module>
DataAll1D = np.loadtxt("datacsv_1d.csv", delimiter=",")
File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 1148, in loadtxt
for x in read_data(_loadtxt_chunksize):
File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 999, in read_data
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 999, in <listcomp>
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 736, in floatconv
return float(x)
ValueError: could not convert string to float: '\ufeff9.9'
(base) lenguyen#ntmle2 test_3D % python plot_3D_4.py
Traceback (most recent call last):
File "/Users/lenguyen/Desktop/test_3D/plot_3D_4.py", line 5, in <module>
DataAll1D = np.loadtxt("datacsv_1d.csv", delimiter=",")
File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 1148, in loadtxt
for x in read_data(_loadtxt_chunksize):
File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 999, in read_data
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 999, in <listcomp>
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 736, in floatconv
return float(x)
ValueError: could not convert string to float: '\ufeff9.9'
(base) lenguyen#ntmle2 test_3D % python plot_3D_4.py
Traceback (most recent call last):
File "/Users/lenguyen/Desktop/test_3D/plot_3D_4.py", line 5, in <module>
DataAll1D = np.loadtxt("datacsv_1d.csv", delimiter=",")
File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 1148, in loadtxt
for x in read_data(_loadtxt_chunksize):
File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 999, in read_data
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 999, in <listcomp>
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 736, in floatconv
return float(x)
ValueError: could not convert string to float: '\ufeff9.9'
(base) lenguyen#ntmle2 test_3D % python plot_3D_4.py
Traceback (most recent call last):
File "/Users/lenguyen/Desktop/test_3D/plot_3D_4.py", line 5, in <module>
DataAll1D = np.loadtxt("datacsv_1d.csv", dtype= "float", delimiter=",")
File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 1148, in loadtxt
for x in read_data(_loadtxt_chunksize):
File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 999, in read_data
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 999, in <listcomp>
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/npyio.py", line 736, in floatconv
return float(x)
ValueError: could not convert string to float: '\ufeff10'
you need to define np
import numpy as np
I was able to import your file with:
x,y = np.genfromtxt('test.csv', delimiter=',', unpack=True, skip_header=0)
Loading z data:
z_all = np.genfromtxt('datacsv_2d_z.csv', delimiter=',', unpack=True, skip_header=0)
A quick plot:
plt.imshow(z_all)
Gives:
3D colormap graph:
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
fig, ax = plt.subplots(subplot_kw={"projection": "3d"}, figsize=(10, 10) )
X, Y = np.genfromtxt('datacsv_1d_xy.csv', delimiter=',', unpack=True, skip_header=0)
X, Y = np.meshgrid(X, Y)
Z = np.genfromtxt('datacsv_2d_z.csv', delimiter=',', unpack=True, skip_header=0)
# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
I am trying to create a some rolling timeseries panels (rolling 12 months forward each time) in pandas using the below code:
import pandas as pd
import numpy as np
def main():
dates = pd.date_range('1995-12-31', periods=480, freq='M', name='Date')
stoks = pd.Index(['s{:04d}'.format(i) for i in range(4000)])
df = pd.DataFrame(np.random.rand(480, 4000), dates, stoks)
print(df.head())
rdf = roll(df, 12)
def roll(df, w):
# stack df.values w-times shifted once at each stack
roll_array = np.dstack([df.values[i:i+w, :] for i in range(len(df.index) - w + 1)]).T
# roll_array is now a 3-D array and can be read into
# a pandas panel object
panel = pd.Panel(roll_array,
items=df.index[w-1:],
major_axis=df.columns,
minor_axis=pd.Index(range(w), name='roll'))
# convert to dataframe and pivot + groupby
# is now ready for any action normally performed
# on a groupby object
return panel.to_frame().unstack().T.groupby(level=0)
if __name__ == "__main__":
print ("Processing daily beta data...\n")
try:
main()
except KeyboardInterrupt:
print ("Ctrl+C pressed. Stopping...")
I am however getting the following error:
minor_axis=pd.Index(range(w), name='roll'))
TypeError: object() takes no parameters
Where have I gone wrong?
See below for the complete stack trace:
runfile('C:/Users/stacey/Documents/Strategies/Crypto/Data/Daily/beta_create_stack.py', wdir='C:/Users/stacey/Documents/Strategies/Crypto/Data/Daily')
Processing daily beta data...
s0000 s0001 s0002 ... s3997 s3998 s3999
Date ...
1995-12-31 0.572736 0.411435 0.914554 ... 0.389626 0.846049 0.736057
1996-01-31 0.012432 0.995075 0.254918 ... 0.497208 0.689789 0.140903
1996-02-29 0.901161 0.618174 0.153318 ... 0.126790 0.780571 0.494440
1996-03-31 0.932200 0.812955 0.814507 ... 0.511931 0.693407 0.571828
1996-04-30 0.348410 0.653946 0.591804 ... 0.196170 0.257667 0.880753
[5 rows x 4000 columns]
Traceback (most recent call last):
File "<ipython-input-181-981f6609137d>", line 1, in <module>
runfile('C:/Users/stacey/Data/Daily/beta_create_stack.py', wdir='C:/Users/stacey/Documents/Strategies/Crypto/Data/Daily')
File "C:\Anaconda\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "C:\Anaconda\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/stacey/Data/Daily/beta_create_stack.py", line 47, in <module>
main()
File "C:/Users/stacey/Data/Daily/beta_create_stack.py", line 17, in main
rdf = roll(df, 12)
File "C:/Users/stacey/Data/Daily/beta_create_stack.py", line 29, in roll
minor_axis=pd.Index(range(w), name='roll'))
TypeError: object() takes no parameters
Traceback (most recent call last):
File "<ipython-input-181-981f6609137d>", line 1, in <module>
runfile('C:/Users/stacey/Data/Daily/beta_create_stack.py', wdir='C:/Users/stacey/Data/Daily')
File "C:\Anaconda\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "C:\Anaconda\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/stacey/Data/Daily/beta_create_stack.py", line 47, in <module>
main()
File "C:/Users/stacey/Data/Daily/beta_create_stack.py", line 17, in main
rdf = roll(df, 12)
File "C:/Users/stacey/Data/Daily/beta_create_stack.py", line 29, in roll
minor_axis=pd.Index(range(w), name='roll'))
TypeError: object() takes no parameters
Hi everybody I have a problem uploading a excel file with Pandas
I have taken the file in archive, if I uploaded it directly it gaves me an error. If I cope and paste the excel file there is no problem.
The code is very easy:
data = pd.read_excel(r"C:\Users\obett\Desktop\Corporate Governance\pandas.xlsx")
and this is the error:
Traceback (most recent call last):
File "C:/Users/obett/PycharmProjects/pythonProject6/main.py", line 24, in <module>
data = pd.read_excel(r"C:\Users\obett\Desktop\Corporate Governance\Aida_Export_67.xlsx")
File "C:\Users\obett\PycharmProjects\pythonProject6\venv\lib\site-packages\pandas\util\_decorators.py", line 299, in wrapper
return func(*args, **kwargs)
File "C:\Users\obett\PycharmProjects\pythonProject6\venv\lib\site-packages\pandas\io\excel\_base.py", line 344, in read_excel
data = io.parse(
File "C:\Users\obett\PycharmProjects\pythonProject6\venv\lib\site-packages\pandas\io\excel\_base.py", line 1170, in parse
return self._reader.parse(
File "C:\Users\obett\PycharmProjects\pythonProject6\venv\lib\site-packages\pandas\io\excel\_base.py", line 492, in parse
data = self.get_sheet_data(sheet, convert_float)
File "C:\Users\obett\PycharmProjects\pythonProject6\venv\lib\site-packages\pandas\io\excel\_openpyxl.py", line 549, in get_sheet_data
converted_row = [self._convert_cell(cell, convert_float) for cell in row]
File "C:\Users\obett\PycharmProjects\pythonProject6\venv\lib\site-packages\pandas\io\excel\_openpyxl.py", line 549, in <listcomp>
converted_row = [self._convert_cell(cell, convert_float) for cell in row]
File "C:\Users\obett\PycharmProjects\pythonProject6\venv\lib\site-packages\pandas\io\excel\_openpyxl.py", line 514, in _convert_cell
elif cell.is_date:
File "C:\Users\obett\PycharmProjects\pythonProject6\venv\lib\site-packages\openpyxl\cell\read_only.py", line 101, in is_date
return Cell.is_date.__get__(self)
File "C:\Users\obett\PycharmProjects\pythonProject6\venv\lib\site-packages\openpyxl\cell\cell.py", line 256, in is_date
self.data_type == 'n' and is_date_format(self.number_format)
File "C:\Users\obett\PycharmProjects\pythonProject6\venv\lib\site-packages\openpyxl\cell\read_only.py", line 66, in number_format
_id = self.style_array.numFmtId
File "C:\Users\obett\PycharmProjects\pythonProject6\venv\lib\site-packages\openpyxl\cell\read_only.py", line 56, in style_array
return self.parent.parent._cell_styles[self._style_id]
IndexError: list index out of range
Thank you very much
I have a bunch of urls that I want to download the text and do some further analysis. I am a python newbie. I have two problems: (1) I have a really weird type error; and (2) the results are not being written to the data frame. My code is as follows:
smallURL= ['http://www.walesonline.co.uk/business/business-news/more-70-jobs-created-bio-12836127','http://economictimes.indiatimes.com/articleshow/61006825.cms?utm_source=contentofinterest&utm_medium=text&utm_campaign=cppst','http://100seguro.com.ar/telefonica-pone-en-venta-su-aseguradora-antares-vida/','http://13wham.com/news/local/urmc-opens-newest-urgent-care-facility']
import pandas
import datetime
f = open('myfile', 'w')
#lista= ['http://www.walesonline.co.uk/business/business-news/more-70-jobs-created-bio-12836127','http://economictimes.indiatimes.com/articleshow/61006825.cms?utm_source=contentofinterest&utm_medium=text&utm_campaign=cppst','http://100seguro.com.ar/telefonica-pone-en-venta-su-aseguradora-antares-vida/','http://13wham.com/news/local/urmc-opens-newest-urgent-care-facility']
df = pandas.DataFrame(columns=('d', 'datetime', 'title', 'text','keywords', 'url'))
from newspaper import Article
for index in range(len(smallURL)):
#url = "https://www.bloomberg.com/news/articles/2017-11-10/microsoft-and-google-turn-to-ai-to-catch-amazon-in-the-cloud"
article = Article(smallURL[index])
#1 . Download the article
#try:
article.download()
#f.write('article.title+\n')
#except:
#pass
#2. Parse the article
try:
article.parse()
f.write('article.title+\n')
except:
pass
#Print article title
#print(article.title)
article.title
#3. Fetch Author Name(s)
print(article.authors)
#4. Fetch Publication Date
if article.publish_date is None:
d = datetime.datetime.now().date()
else:
d = article.publish_date
#5. Print article text
print(article.text)
#6. Natural Language Processing on Article to fetch Keywords
#article.nlp()
#Print Keywords
print(article.keywords)
#7. Generate Summary of the article
#print(article.url)
print(article.url)
df.loc[index] = [d, datetime.datetime.now().date(), article.title, article.text,article.keywords,article.url]
My output includes:
[]
http://100seguro.com.ar/telefonica-pone-en-venta-su-aseguradora-antares-vida/
Traceback (most recent call last):
File "", line 1, in
runfile('C:/Users/theiman/Desktop/untitled7.py', wdir='C:/Users/theiman/Desktop')
File "C:\Users\theiman\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 710, in runfile
execfile(filename, namespace)
File "C:\Users\theiman\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 101, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/theiman/Desktop/untitled7.py", line 57, in
df.loc[index] = [d, datetime.datetime.now().date(), article.title, article.text,article.keywords,article.url]
File "C:\Users\theiman\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexing.py", line 179, in setitem
self._setitem_with_indexer(indexer, value)
File "C:\Users\theiman\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexing.py", line 425, in _setitem_with_indexer
self.obj._data = self.obj.append(value)._data
File "C:\Users\theiman\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py", line 4533, in append
other = other._convert(datetime=True, timedelta=True)
File "C:\Users\theiman\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\generic.py", line 3472, in _convert
copy=copy)).finalize(self)
File "C:\Users\theiman\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\internals.py", line 3227, in convert
return self.apply('convert', **kwargs)
File "C:\Users\theiman\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\internals.py", line 3091, in apply
applied = getattr(b, f)(**kwargs)
File "C:\Users\theiman\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\internals.py", line 1892, in convert
values = fn(values.ravel(), **fn_kwargs)
File "C:\Users\theiman\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\dtypes\cast.py", line 740, in soft_convert_objects
values = lib.maybe_convert_objects(values, convert_datetime=datetime)
File "pandas/_libs/src\inference.pyx", line 1204, in pandas._libs.lib.maybe_convert_objects
TypeError: unhashable type: 'tzutc'
Any idea on what is going wrong and how I can fix it? Thank you!!
from textblob import Blobber
from textblob_aptagger import PerceptronTagger
blob1 = TextBlob("This is a blob.",pos_tagger=PerceptronTagger())
blob2 = TextBlob("This is another blob.")
print(blob1.tags)
Connected to pydev debugger (build 145.844) Traceback (most recent call last): File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.1.2\helpers\pydev\pydevd.py", line 1531, in <module>
globals = debugger.run(setup['file'], None, None, is_module) File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition
2016.1.2\helpers\pydev\pydevd.py", line 938, in run
pydev_imports.execfile(file, globals, locals) # execute the script File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.1.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc) File "D:/Tagger/Test.py", line 31, in <module>
print(blob1.tags) File "C:\Users\karthika\AppData\Local\Programs\Python\Python35\lib\site-packages\textblob\decorators.py", line 24, in __get__
value = obj.__dict__[self.func.__name__] = self.func(obj) File "C:\Users\karthika\AppData\Local\Programs\Python\Python35\lib\site-packages\textblob\blob.py", line 475, in pos_tags
return [val for sublist in [s.pos_tags for s in self.sentences] for val in sublist] File "C:\Users\karthika\AppData\Local\Programs\Python\Python35\lib\site-packages\textblob\blob.py", line 475, in <listcomp>
return [val for sublist in [s.pos_tags for s in self.sentences] for val in sublist] File "C:\Users\karthika\AppData\Local\Programs\Python\Python35\lib\site-packages\textblob\decorators.py", line 24, in __get__
value = obj.__dict__[self.func.__name__] = self.func(obj) File "C:\Users\karthika\AppData\Local\Programs\Python\Python35\lib\site-packages\textblob\blob.py", line 478, in pos_tags
for word, t in self.pos_tagger.tag(self) File "C:\Users\karthika\AppData\Local\Programs\Python\Python35\lib\site-packages\textblob_aptagger\taggers.py", line 49, in tag
for words in split_sents(corpus): File "C:\Users\karthika\AppData\Local\Programs\Python\Python35\lib\site-packages\textblob_aptagger\taggers.py", line 44, in split_sents
for s in s_split(corpus): File "C:\Users\karthika\AppData\Local\Programs\Python\Python35\lib\site-packages\textblob\decorators.py", line 35, in decorated
return func(*args, **kwargs) File "C:\Users\karthika\AppData\Local\Programs\Python\Python35\lib\site-packages\textblob\tokenizers.py", line 57, in tokenize
return nltk.tokenize.sent_tokenize(text) File "C:\Users\karthika\AppData\Local\Programs\Python\Python35\lib\site-packages\nltk\tokenize\__init__.py", line 95, in sent_tokenize
return tokenizer.tokenize(text) File "C:\Users\karthika\AppData\Local\Programs\Python\Python35\lib\site-packages\nltk\tokenize\punkt.py", line 1237, in tokenize
return list(self.sentences_from_text(text, realign_boundaries)) File "C:\Users\karthika\AppData\Local\Programs\Python\Python35\lib\site-packages\nltk\tokenize\punkt.py", line 1285, in sentences_from_text
return [text[s:e] for s, e in self.span_tokenize(text, realign_boundaries)] File "C:\Users\karthika\AppData\Local\Programs\Python\Python35\lib\site-packages\nltk\tokenize\punkt.py", line 1276, in span_tokenize
return [(sl.start, sl.stop) for sl in slices] File "C:\Users\karthika\AppData\Local\Programs\Python\Python35\lib\site-packages\nltk\tokenize\punkt.py", line 1276, in <listcomp>
return [(sl.start, sl.stop) for sl in slices] File "C:\Users\karthika\AppData\Local\Programs\Python\Python35\lib\site-packages\nltk\tokenize\punkt.py", line 1316, in _realign_boundaries
for sl1, sl2 in _pair_iter(slices): File "C:\Users\karthika\AppData\Local\Programs\Python\Python35\lib\site-packages\nltk\tokenize\punkt.py", line 312, in _pair_iter
prev = next(it) File "C:\Users\karthika\AppData\Local\Programs\Python\Python35\lib\site-packages\nltk\tokenize\punkt.py", line 1289, in _slices_from_text
for match in self._lang_vars.period_context_re().finditer(text): TypeError: expected string or bytes-like object