Python 2.7: Error not caught by exception due to "raise" - python

Here's my code:
import pandas as pd
df2 = pd.DataFrame()
try:
df_task = df_task.append(df2, ignore_index = True)
except NameError:
df_task = df2
raise
It produces this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 540, in runfile
execfile(filename, namespace)
File "/home/cld_intern/.spyder2/.temp.py", line 12, in <module>
df_task = df_task.append(df2, ignore_index = True)
NameError: name 'df_task' is not defined
The except block is supposed to catch the NameError, but somehow the inclusion of "raise" without specifying the exception to raise produces a NameError. Why is it so?
This runs perfectly:
import pandas as pd
df2 = pd.DataFrame()
try:
df_task = df_task.append(df2, ignore_index = True)
except NameError:
df_task = df2
This runs as expected too:
import pandas as pd
df2 = pd.DataFrame()
try:
df_task = df_task.append(df2, ignore_index = True)
except NameError:
df_task = df2
raise Exception("error message")
With the following traceback:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 540, in runfile
execfile(filename, namespace)
File "/home/cld_intern/.spyder2/.temp.py", line 15, in <module>
raise Exception("error message")
Exception: error message

in python 3:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
NameError: name 'df_task' is not defined
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
NameError: name 'df2' is not defined
in python2.7
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
NameError: name 'df2' is not defined
Are you sure you are using python 2.7?
Like mentioned in the comment, df2 is not defined i guess.

Related

How to handle "not found" ticker in yfinance?

So im trying to fetch some stock data in a loop (not sure if i can pass an array), like this:
def getData(ticker):
print (ticker)
data = pdr.get_data_yahoo(ticker, start=start_date, end=today)
dataname= ticker+'_'+str(today)
files.append(dataname)
SaveData(data, dataname)
But for some reasons, some of the tickers i feed to pdr.get_data_yahoo() are not found, and python throws this error:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/pandas_datareader/yahoo/daily.py", line 157, in _read_one_data
data = j["context"]["dispatcher"]["stores"]["HistoricalPriceStore"]
KeyError: 'HistoricalPriceStore'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "borsdata_api.py", line 65, in <module>
getData(row['ticker'])
File "borsdata_api.py", line 47, in getData
data = pdr.get_data_yahoo(ticker, start=start_date, end=today)
File "/usr/local/lib/python3.7/site-packages/pandas_datareader/data.py", line 82, in get_data_yahoo
return YahooDailyReader(*args, **kwargs).read()
File "/usr/local/lib/python3.7/site-packages/pandas_datareader/base.py", line 251, in read
df = self._read_one_data(self.url, params=self._get_params(self.symbols))
File "/usr/local/lib/python3.7/site-packages/pandas_datareader/yahoo/daily.py", line 160, in _read_one_data
raise RemoteDataError(msg.format(symbol, self.__class__.__name__))
pandas_datareader._utils.RemoteDataError: No data fetched for symbol ADDV-TO-1.ST using YahooDailyReader
Is it possible to just skip this iteration and move on the next one in the list?
def getData(ticker):
print (ticker)
try:
data = pdr.get_data_yahoo(ticker, start=start_date, end=today)
dataname= ticker+'_'+str(today)
files.append(dataname)
SaveData(data, dataname)
except:
pass #or traceback.print_exc(), or traceback.format_exc()
#print_exc() will raise the error and print traceback.
#format_exc() will return the error as a string.

'MatlabWorkSpace' object is not callable

eng = matlab.engine.start_matlab()
for line in matcode:
eng.workspace(line, nargout=0)
res = eng.workspace['x']
eng.quit()
and matcode.txt object string is
'x = [1 1;1 2;1 3;1 4];'
Couldn't figure out why is this happening :(
Traceback (most recent call last):
File "lreg.py", line 14, in <module>
main()
File "lreg.py", line 10, in main
data = matlab_engine.start_engine(code_object)
File "/Users/hiteshkr/PycharmProjects/MLCO/matlab_engine.py", line 7, in start_engine
eng.workspace(line, nargout=0)
TypeError: 'MatlabWorkSpace' object is not callable

Is it possible to filter Spark DataFrames to return all rows where a column value is in a list using pyspark?

How can I return only the rows of a Spark DataFrame where the values for a column are within a specified list?
Here's my Python pandas way of doing this operation:
df_start = df[df['name'].isin(['App Opened', 'App Launched'])].copy()
I saw this SO scala implementation and tried several permutations, but couldn't get it to work.
Here's one failed attempt to do it using pyspark:
df_start = df_spark.filter(col("name") isin ['App Opened', 'App Launched'])
Output:
Traceback (most recent call last):
File "/tmp/zeppelin_pyspark-6660042787423349557.py", line 253, in <module>
code = compile('\n'.join(final_code), '<stdin>', 'exec', ast.PyCF_ONLY_AST, 1)
File "<stdin>", line 18
df_start = df_spark.filter(col("name") isin ['App Opened', 'App Launched'])
^
SyntaxError: invalid syntax
Another attempt:
df_start = df_spark.filter(col("name").isin(['App Opened', 'App Launched']))
Output:
Traceback (most recent call last):
File "/tmp/zeppelin_pyspark-6660042787423349557.py", line 267, in <module>
raise Exception(traceback.format_exc())
Exception: Traceback (most recent call last):
File "/tmp/zeppelin_pyspark-6660042787423349557.py", line 260, in <module>
exec(code)
File "<stdin>", line 18, in <module>
NameError: name 'col' is not defined
As dmdmdmdmdmd pointed out in the comments, the second method didn't work because col needed to be imported:
from pyspark.sql.functions import col
df_start = df_spark.filter(col("name").isin(['App Opened', 'App Launched']))
Here's another way of accomplishing the filter:
df_start = df_spark.filter(df_spark.name.isin(['App Opened', 'App Launched']))

how to use the post_mortem method of pdb?

I am trying to understand how to use the pdb.post_mortem() method.
for this given file
# expdb.py
import pdb
import trace
def hello():
a = 6 * 9
b = 7 ** 2
c = a * b
d = 4 / 0
print(c)
tracer = trace.Trace()
Command prompt
'''
# first Try
λ python -i expdb.py
>>> pdb.post_mortem()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Program Files\Anaconda3\lib\pdb.py", line 1590, in post_mortem
raise ValueError("A valid traceback must be passed if no "
ValueError: A valid traceback must be passed if no exception is being handled
'''
'''
# Second Try
λ python -i expdb.py
>>> pdb.post_mortem(traceback=tracer.run('hello()') )
--- modulename: trace, funcname: _unsettrace
trace.py(77): sys.settrace(None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Program Files\Anaconda3\lib\trace.py", line 500, in run
self.runctx(cmd, dict, dict)
File "C:\Program Files\Anaconda3\lib\trace.py", line 508, in runctx
exec(cmd, globals, locals)
File "<string>", line 1, in <module>
File "expdb.py", line 8, in hello
d = 4 / 0
ZeroDivisionError: division by zero
>>>
The post_mortem method wants a traceback object, not a Trace object. Traceback objects can be acquired from sys.exc_info()[2] inside of an except block, or you can simply call pdb.post_mortem() with no arguments directly (in the except block).
But either way, you must catch the exception before you can debug it.

How to print the first line from a traceback stack

Suppose I am given the following traceback:
Traceback (most recent call last):
File "<wingdb_compile>", line 3, in <module>
File "C:\Python34\lib\ftplib.py", line 419, in login
resp = self.sendcmd('PASS ' + passwd)
File "C:\Python34\lib\ftplib.py", line 272, in sendcmd
return self.getresp()
File "C:\Python34\lib\ftplib.py", line 245, in getresp
raise error_perm(resp)
ftplib.error_perm: 530 Login incorrect.
I have managed to extract the Error details but what has stumped me is how would I extract the line:
File "<wingdb_compile>", line 3, in <module>
I was looking at methods in the trace back package but wondered if any one had experience with that here
The function traceback.format_exc is built primarily for this
This is like print_exc(limit) but returns a string instead of printing to a file.
>>> import traceback
>>> try:
... x = 2/0
... except:
... error = traceback.format_exc()
...
>>> error
'Traceback (most recent call last):\n File "<stdin>", line 2, in <module>\nZeroDivisionError: division by zero\n'
>>> linesoferror = error.split('\n')
>>> linesoferror
['Traceback (most recent call last):', ' File "<stdin>", line 2, in <module>', 'ZeroDivisionError: division by zero', '']
So now you wanted the first line then you can simply use
>>> linesoferror[1]
' File "<stdin>", line 2, in <module>'
Voila! You have what you want
ALERT - Valid for Python 2.4.1 and above

Categories

Resources