Search code examples
pythontry-exceptpython-ggplot

try except not working with python ggplot


I am debugging a problem in some python ggplot code. So that I could see if my efforts (which I expect are coming from the underlying data frame) are working, I tried to wrap the problem code in a try/except statement. However, when I run the Python 2.7 Jupyter cell the code is in, if I take off the try/except, it throws an error. Put try/except back on, and the cell simply executes with no output of any kind.

Does ggplot not work with try/except? Or is there something I need to do differently to this code so the interaction will react as expected?

Problem Code:

try:
    ggplot(aes(x='Date', y='Price', color='Daily'), data=googleHiLoPlotData) \
        + geom_density() 
except Exception as ee:
    print(ee)
    print(type(ee))

Error it only throws if I remove try/except and take out the indenting:

TypeError: unsupported operand type(s) for /: 'str' and 'long'

I have copied the 'try/except' block to another cell and verified that if I replace the entire ggplot line to something else that will throw an error, it works and then prints the error and error type as expected. So the problem seems to be something about this specific code within the block. Any ideas what is going on here, and how to make it throw the error correctly) with the ggplot line?

Version Info: Python 2.7, ggplot 0.11.5, Windows 7 64bit


Solution

  • Looks like in creating this question (and doing a little investigation of the issue myself), I may have uncovered a bug in either ggplot or python for this specific interaction.

    Simply changing the x value in the ggplot line to 'Something Stupid', the try-except error then gets reported and printed as per the except portion of the block.

    However - Anaconda was installed using version 4.2. Until I rule out the possibility that something needs an upgrade, this bug should not be reported yet. It is worth noting in case anyone else encounters the same problem.