Skip to content

shift(-1) doesn't work in df.eval() #16833

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

Closed
kenahoo opened this issue Jul 5, 2017 · 2 comments
Closed

shift(-1) doesn't work in df.eval() #16833

kenahoo opened this issue Jul 5, 2017 · 2 comments
Labels
Duplicate Report Duplicate issue or pull request

Comments

@kenahoo
Copy link

kenahoo commented Jul 5, 2017

Code Sample, a copy-pastable example if possible

>>> df = pd.DataFrame({'x':[1,2,3,4,5]})

>>> df['x'].shift(-1)
0    2.0
1    3.0
2    4.0
3    5.0
4    NaN
Name: x, dtype: float64

>>> df.eval('x.shift(-1)')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/frame.py", line 2284, in eval
    return _eval(expr, inplace=inplace, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/computation/eval.py", line 262, in eval
    truediv=truediv)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/computation/expr.py", line 727, in __init__
    self.terms = self.parse()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/computation/expr.py", line 744, in parse
    return self._visitor.visit(self.expr)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/computation/expr.py", line 313, in visit
    return visitor(node, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/computation/expr.py", line 319, in visit_Module
    return self.visit(expr, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/computation/expr.py", line 313, in visit
    return visitor(node, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/computation/expr.py", line 322, in visit_Expr
    return self.visit(node.value, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/computation/expr.py", line 313, in visit
    return visitor(node, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/computation/expr.py", line 568, in visit_Call_35
    new_args = [self.visit(arg).value for arg in node.args]
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/computation/expr.py", line 568, in <listcomp>
    new_args = [self.visit(arg).value for arg in node.args]
AttributeError: 'UnaryOp' object has no attribute 'value'

Problem description

The error's pretty opaque and several layers deep, so I don't really understand much about what's going wrong internally. It does work correctly for shift(1), so I assume it's something about negative indices.

Expected Output

df['x'].shift(-1) and df.eval('x.shift(-1)') should have identical output.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.5.3.final.0
python-bits: 64
OS: Darwin
OS-release: 16.6.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: None
LOCALE: en_US.UTF-8

pandas: 0.20.1
pytest: 3.1.1
pip: 9.0.1
setuptools: 28.8.0
Cython: None
numpy: 1.12.1
scipy: None
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
pandas_gbq: None
pandas_datareader: None

@TomAugspurger
Copy link
Contributor

I suspect this will be fixed by fixing #16363. I added your example there to ensure that it's tested (we'll reopen this one if the fix turns out to be different).

I think we could use a contributor for this, so feel free to dive in.

@TomAugspurger TomAugspurger added this to the No action milestone Jul 5, 2017
@TomAugspurger TomAugspurger added the Duplicate Report Duplicate issue or pull request label Jul 5, 2017
@kenahoo
Copy link
Author

kenahoo commented Jul 5, 2017

Thanks. I took a look at the suggested pathways to fix #16363, and they're probably out of my capability league.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate Report Duplicate issue or pull request
Projects
None yet
Development

No branches or pull requests

2 participants