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
Comment From: TomAugspurger
I suspect this will be fixed by fixing https://github.com/pandas-dev/pandas/issues/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.
Comment From: kenahoo
Thanks. I took a look at the suggested pathways to fix #16363, and they're probably out of my capability league.