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

Column name 'class' causes access and query issues #9912

Closed
simjega opened this issue Apr 16, 2015 · 3 comments
Closed

Column name 'class' causes access and query issues #9912

simjega opened this issue Apr 16, 2015 · 3 comments
Labels
Indexing Related to indexing on series/frames, not to indexes themselves Usage Question

Comments

@simjega
Copy link

simjega commented Apr 16, 2015

For some reason pandas isn't liking when I create a column named class and attempt to access with .{columnname} method

I also cannot run a query on the dataframe.

Code:

>>> a = pandas.DataFrame({'class':[0], 'clas1': [1]})
>>> a.clas1
0    1
Name: clas1, dtype: int64
>>> a.class
  File "<stdin>", line 1
    a.class
          ^
SyntaxError: invalid syntax
>>> a.query("clas1==1")
   clas1  class
0      1      0
>>> a.query("class==0")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.4/site-packages/pandas/core/frame.py", line 1918, in query
    res = self.eval(expr, **kwargs)
  File "/usr/lib/python3.4/site-packages/pandas/core/frame.py", line 1970, in eval
    return _eval(expr, **kwargs)
  File "/usr/lib/python3.4/site-packages/pandas/computation/eval.py", line 230, in eval
    truediv=truediv)
  File "/usr/lib/python3.4/site-packages/pandas/computation/expr.py", line 635, in __init__
    self.terms = self.parse()
  File "/usr/lib/python3.4/site-packages/pandas/computation/expr.py", line 652, in parse
    return self._visitor.visit(self.expr)
  File "/usr/lib/python3.4/site-packages/pandas/computation/expr.py", line 310, in visit
    node = ast.fix_missing_locations(ast.parse(clean))
  File "/usr/lib/python3.4/ast.py", line 35, in parse
    return compile(source, filename, mode, PyCF_ONLY_AST)
  File "<unknown>", line 1
    class ==0 
           ^
SyntaxError: invalid syntax

Dependencies:

INSTALLED VERSIONS

commit: None
python: 3.4.3.final.0
python-bits: 64
OS: Linux
OS-release: 3.18.6-1-ARCH
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: en_US.utf8

pandas: 0.15.2
nose: 1.3.4
Cython: None
numpy: 1.9.2
scipy: 0.15.1
statsmodels: None
IPython: 3.0.0
sphinx: None
patsy: None
dateutil: 2.4.1
pytz: 2014.10
bottleneck: 1.0.0
tables: 3.1.1
numexpr: 2.4
matplotlib: 1.4.3
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: None
apiclient: None
rpy2: None
sqlalchemy: None
pymysql: None
psycopg2: None

@jreback
Copy link
Contributor

jreback commented Apr 16, 2015

http://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access

class is not a valid Python identifier as its reserved word

simply use df['class'] which is the canonical method of access

@jreback jreback added Usage Question Indexing Related to indexing on series/frames, not to indexes themselves labels Apr 16, 2015
@simjega
Copy link
Author

simjega commented Apr 16, 2015

Thanks,

Is this a recent change? I've been using the column name 'class' for months now without any problems

@jreback
Copy link
Contributor

jreback commented Apr 16, 2015

nope been there forever

@jreback jreback closed this as completed Apr 16, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Indexing Related to indexing on series/frames, not to indexes themselves Usage Question
Projects
None yet
Development

No branches or pull requests

2 participants