Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing issue w/ '~class' in facet_wrap? #28

Closed
g2boojum opened this issue Jun 30, 2017 · 3 comments · Fixed by #29
Closed

Parsing issue w/ '~class' in facet_wrap? #28

g2boojum opened this issue Jun 30, 2017 · 3 comments · Fixed by #29
Labels

Comments

@g2boojum
Copy link

I found this issue when working through Chapter 3 of the "R for Data Science" book (using plotnine). I was working within a Jupyter notebook, using Python 3.6.

%matplotlib inline
from plotnine import *
from plotnine.data import *

ggplot(data=mpg) + geom_point(mapping=aes(x='displ', y='hwy')) + facet_wrap('~class')

I get the interesting error:

File "", line 1
class
^
SyntaxError: unexpected EOF while parsing

Replacing '~class' with '~cyl' gives me a lovely plot, and no error.

To see if Jupyter was contributing to the problem, I created a simple script:

# foo.py
from plotnine import *
from plotnine.data import *

(ggplot(data=mpg) + geom_point(mapping=aes(x='displ', y='hwy')) 
 + facet_wrap('~class')).save('foo.png')

I get a somewhat more involved traceback, but essentially the same error:

$ python foo.py
/home/grant/Envs/py36/lib64/python3.6/site-packages/statsmodels/compat/pandas.py:56: FutureWarning: The pandas.core.datetools module is deprecated and will be removed in a future version. Please use the pandas.tseries module instead.
from pandas.core import datetools
Traceback (most recent call last):
File "foo.py", line 5, in
+ facet_wrap('~class')).save('foo.png')
File "/home/grant/Envs/py36/lib64/python3.6/site-packages/plotnine/ggplot.py", line 571, in save
raise err
File "/home/grant/Envs/py36/lib64/python3.6/site-packages/plotnine/ggplot.py", line 568, in save
_save()
File "/home/grant/Envs/py36/lib64/python3.6/site-packages/plotnine/ggplot.py", line 533, in _save
fig = figure[0] = self.draw()
File "/home/grant/Envs/py36/lib64/python3.6/site-packages/plotnine/ggplot.py", line 141, in draw
self._build()
File "/home/grant/Envs/py36/lib64/python3.6/site-packages/plotnine/ggplot.py", line 222, in _build
layout.setup(layers, self)
File "/home/grant/Envs/py36/lib64/python3.6/site-packages/plotnine/facets/layout.py", line 59, in setup
self.layout = self.facet.compute_layout(data)
File "/home/grant/Envs/py36/lib64/python3.6/site-packages/plotnine/facets/facet_wrap.py", line 73, in compute_layout
self.vars, drop=self.drop)
File "/home/grant/Envs/py36/lib64/python3.6/site-packages/plotnine/facets/facet.py", line 519, in combine_vars
for df in data if df is not None]
File "/home/grant/Envs/py36/lib64/python3.6/site-packages/plotnine/facets/facet.py", line 519, in
for df in data if df is not None]
File "/home/grant/Envs/py36/lib64/python3.6/site-packages/plotnine/facets/facet.py", line 630, in eval_facet_vars
res = env.eval(name, inner_namespace=data)
File "/home/grant/Envs/py36/lib64/python3.6/site-packages/patsy/eval.py", line 164, in eval
code = compile(expr, source_name, "eval", self.flags, False)
File "", line 1
class
^
SyntaxError: unexpected EOF while parsing

Again, if I replace facet_wrap('~class') with facet_wrap('~cyl'), I get a lovely plot and no error.

I'm really looking forward to using plotnine (and finally learning ggplot). Thanks for all of your hard work!

@has2k1
Copy link
Owner

has2k1 commented Jun 30, 2017

The problem is class is a reserved python keyword. That messes with the formula evaluation.

@has2k1 has2k1 added the bug label Jun 30, 2017
@g2boojum
Copy link
Author

Ah, that makes sense. Thanks!

has2k1 added a commit that referenced this issue Jul 1, 2017
This is an incomplete fix but it should cover the majority
(the simple & obvious) of cases.

fixes #28
@has2k1 has2k1 closed this as completed in #29 Jul 1, 2017
@g2boojum
Copy link
Author

g2boojum commented Jul 3, 2017

You rock! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants