<h4 dir="auto">Code Sample, a copy-pastable example if possible</h4>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import pandas as pd

df = pd.DataFrame({
    'Foo64': [1.2, 2.4, 7.24],
})

df['Foo32'] = df['Foo64'].astype('float32')

df.eval('-1 + Foo64')   # Works
df.eval('-1 + Foo32')   # Throws Exception"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">pandas</span> <span class="pl-k">as</span> <span class="pl-s1">pd</span>

<span class="pl-s1">df</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>({
    <span class="pl-s">'Foo64'</span>: [<span class="pl-c1">1.2</span>, <span class="pl-c1">2.4</span>, <span class="pl-c1">7.24</span>],
})

<span class="pl-s1">df</span>[<span class="pl-s">'Foo32'</span>] <span class="pl-c1">=</span> <span class="pl-s1">df</span>[<span class="pl-s">'Foo64'</span>].<span class="pl-en">astype</span>(<span class="pl-s">'float32'</span>)

<span class="pl-s1">df</span>.<span class="pl-en">eval</span>(<span class="pl-s">'-1 + Foo64'</span>)   <span class="pl-c"># Works</span>
<span class="pl-s1">df</span>.<span class="pl-en">eval</span>(<span class="pl-s">'-1 + Foo32'</span>)   <span class="pl-c"># Throws Exception</span></pre></div>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="Traceback (most recent call last):
  File &quot;C:\Users\vmuriart\Desktop\bug_eval.py&quot;, line 10, in &lt;module&gt;
    df.eval('-1 + Foo32')  # Throws Exception
  File &quot;C:\Anaconda\lib\site-packages\pandas\core\frame.py&quot;, line 2186, in eval
    return _eval(expr, inplace=inplace, **kwargs)
  File &quot;C:\Anaconda\lib\site-packages\pandas\core\computation\eval.py&quot;, line 262, in eval
    truediv=truediv)
  File &quot;C:\Anaconda\lib\site-packages\pandas\core\computation\expr.py&quot;, line 727, in __init__
    self.terms = self.parse()
  File &quot;C:\Anaconda\lib\site-packages\pandas\core\computation\expr.py&quot;, line 744, in parse
    return self._visitor.visit(self.expr)
  File &quot;C:\Anaconda\lib\site-packages\pandas\core\computation\expr.py&quot;, line 313, in visit
    return visitor(node, **kwargs)
  File &quot;C:\Anaconda\lib\site-packages\pandas\core\computation\expr.py&quot;, line 319, in visit_Module
    return self.visit(expr, **kwargs)
  File &quot;C:\Anaconda\lib\site-packages\pandas\core\computation\expr.py&quot;, line 313, in visit
    return visitor(node, **kwargs)
  File &quot;C:\Anaconda\lib\site-packages\pandas\core\computation\expr.py&quot;, line 322, in visit_Expr
    return self.visit(node.value, **kwargs)
  File &quot;C:\Anaconda\lib\site-packages\pandas\core\computation\expr.py&quot;, line 313, in visit
    return visitor(node, **kwargs)
  File &quot;C:\Anaconda\lib\site-packages\pandas\core\computation\expr.py&quot;, line 417, in visit_BinOp
    left, right = self._maybe_downcast_constants(left, right)
  File &quot;C:\Anaconda\lib\site-packages\pandas\core\computation\expr.py&quot;, line 365, in _maybe_downcast_constants
    name = self.env.add_tmp(np.float32(left.value))
AttributeError: 'UnaryOp' object has no attribute 'value'
"><pre class="notranslate"><span class="pl-v">Traceback</span> (<span class="pl-s1">most</span> <span class="pl-s1">recent</span> <span class="pl-s1">call</span> <span class="pl-s1">last</span>):
  <span class="pl-v">File</span> <span class="pl-s">"C:\Users<span class="pl-cce">\v</span>muriart\Desktop<span class="pl-cce">\b</span>ug_eval.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">10</span>, <span class="pl-s1">in</span> <span class="pl-c1">&lt;</span><span class="pl-s1">module</span><span class="pl-c1">&gt;</span>
    <span class="pl-s1">df</span>.<span class="pl-en">eval</span>(<span class="pl-s">'-1 + Foo32'</span>)  <span class="pl-c"># Throws Exception</span>
  <span class="pl-v">File</span> <span class="pl-s">"C:\Anaconda\lib\site-packages\pandas\core<span class="pl-cce">\f</span>rame.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">2186</span>, <span class="pl-s1">in</span> <span class="pl-s1">eval</span>
    <span class="pl-k">return</span> <span class="pl-en">_eval</span>(<span class="pl-s1">expr</span>, <span class="pl-s1">inplace</span><span class="pl-c1">=</span><span class="pl-s1">inplace</span>, <span class="pl-c1">**</span><span class="pl-s1">kwargs</span>)
  <span class="pl-v">File</span> <span class="pl-s">"C:\Anaconda\lib\site-packages\pandas\core\computation\eval.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">262</span>, <span class="pl-s1">in</span> <span class="pl-s1">eval</span>
    <span class="pl-s1">truediv</span><span class="pl-c1">=</span><span class="pl-s1">truediv</span>)
  <span class="pl-v">File</span> <span class="pl-s">"C:\Anaconda\lib\site-packages\pandas\core\computation\expr.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">727</span>, <span class="pl-s1">in</span> <span class="pl-s1">__init__</span>
    <span class="pl-s1">self</span>.<span class="pl-s1">terms</span> <span class="pl-c1">=</span> <span class="pl-s1">self</span>.<span class="pl-en">parse</span>()
  <span class="pl-v">File</span> <span class="pl-s">"C:\Anaconda\lib\site-packages\pandas\core\computation\expr.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">744</span>, <span class="pl-s1">in</span> <span class="pl-s1">parse</span>
    <span class="pl-k">return</span> <span class="pl-s1">self</span>.<span class="pl-s1">_visitor</span>.<span class="pl-en">visit</span>(<span class="pl-s1">self</span>.<span class="pl-s1">expr</span>)
  <span class="pl-v">File</span> <span class="pl-s">"C:\Anaconda\lib\site-packages\pandas\core\computation\expr.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">313</span>, <span class="pl-s1">in</span> <span class="pl-s1">visit</span>
    <span class="pl-k">return</span> <span class="pl-en">visitor</span>(<span class="pl-s1">node</span>, <span class="pl-c1">**</span><span class="pl-s1">kwargs</span>)
  <span class="pl-v">File</span> <span class="pl-s">"C:\Anaconda\lib\site-packages\pandas\core\computation\expr.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">319</span>, <span class="pl-s1">in</span> <span class="pl-s1">visit_Module</span>
    <span class="pl-k">return</span> <span class="pl-s1">self</span>.<span class="pl-en">visit</span>(<span class="pl-s1">expr</span>, <span class="pl-c1">**</span><span class="pl-s1">kwargs</span>)
  <span class="pl-v">File</span> <span class="pl-s">"C:\Anaconda\lib\site-packages\pandas\core\computation\expr.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">313</span>, <span class="pl-s1">in</span> <span class="pl-s1">visit</span>
    <span class="pl-k">return</span> <span class="pl-en">visitor</span>(<span class="pl-s1">node</span>, <span class="pl-c1">**</span><span class="pl-s1">kwargs</span>)
  <span class="pl-v">File</span> <span class="pl-s">"C:\Anaconda\lib\site-packages\pandas\core\computation\expr.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">322</span>, <span class="pl-s1">in</span> <span class="pl-s1">visit_Expr</span>
    <span class="pl-k">return</span> <span class="pl-s1">self</span>.<span class="pl-en">visit</span>(<span class="pl-s1">node</span>.<span class="pl-s1">value</span>, <span class="pl-c1">**</span><span class="pl-s1">kwargs</span>)
  <span class="pl-v">File</span> <span class="pl-s">"C:\Anaconda\lib\site-packages\pandas\core\computation\expr.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">313</span>, <span class="pl-s1">in</span> <span class="pl-s1">visit</span>
    <span class="pl-k">return</span> <span class="pl-en">visitor</span>(<span class="pl-s1">node</span>, <span class="pl-c1">**</span><span class="pl-s1">kwargs</span>)
  <span class="pl-v">File</span> <span class="pl-s">"C:\Anaconda\lib\site-packages\pandas\core\computation\expr.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">417</span>, <span class="pl-s1">in</span> <span class="pl-s1">visit_BinOp</span>
    <span class="pl-s1">left</span>, <span class="pl-s1">right</span> <span class="pl-c1">=</span> <span class="pl-s1">self</span>.<span class="pl-en">_maybe_downcast_constants</span>(<span class="pl-s1">left</span>, <span class="pl-s1">right</span>)
  <span class="pl-v">File</span> <span class="pl-s">"C:\Anaconda\lib\site-packages\pandas\core\computation\expr.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">365</span>, <span class="pl-s1">in</span> <span class="pl-s1">_maybe_downcast_constants</span>
    <span class="pl-s1">name</span> <span class="pl-c1">=</span> <span class="pl-s1">self</span>.<span class="pl-s1">env</span>.<span class="pl-en">add_tmp</span>(<span class="pl-s1">np</span>.<span class="pl-en">float32</span>(<span class="pl-s1">left</span>.<span class="pl-s1">value</span>))
<span class="pl-v">AttributeError</span>: <span class="pl-s">'UnaryOp'</span> <span class="pl-s1">object</span> <span class="pl-s1">has</span> <span class="pl-s1">no</span> <span class="pl-s1">attribute</span> <span class="pl-s">'value'</span></pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto"><code class="notranslate">df.eval(...)</code> operations fail if one of the columns involved is of type <code class="notranslate">float32</code> and some <code class="notranslate">unary</code> operations are involved. In the example above <code class="notranslate">df.eval('-1 + Foo32')</code> failed, but <code class="notranslate">df.eval('-Foo32')</code> will succeed, and <code class="notranslate">df.eval('-1*Foo32')</code> will also fail.</p>
<p dir="auto">Originally I tested fixing this by just adding <code class="notranslate">self.value = operand.value</code> to the <code class="notranslate">UnaryOp</code> <a href="https://github.com/pandas-dev/pandas/blob/master/pandas/core/computation/ops.py#L497">object</a>, before I realized that this issue only affects <code class="notranslate">float32</code> objects. I haven't looked to see where the code branches off to cause this misbehavior.</p>
<p dir="auto">This was tested and reproduced on both <code class="notranslate">0.20.3</code> and <code class="notranslate">0.22.0</code></p>
<h4 dir="auto">Expected Output</h4>
<p dir="auto">No exception thrown.</p>
<h4 dir="auto">Output of <code class="notranslate">pd.show_versions()</code></h4>
<details>
<h2 dir="auto">INSTALLED VERSIONS</h2>
<p dir="auto">commit: None<br>
python: 3.6.3.final.0<br>
python-bits: 64<br>
OS: Windows<br>
OS-release: 10<br>
machine: AMD64<br>
processor: Intel64 Family 6 Model 79 Stepping 1, GenuineIntel<br>
byteorder: little<br>
LC_ALL: None<br>
LANG: None<br>
LOCALE: None.None</p>
<p dir="auto">pandas: 0.22.0<br>
pytest: 3.2.1<br>
pip: 9.0.1<br>
setuptools: 36.5.0.post20170921<br>
Cython: 0.26.1<br>
numpy: 1.12.1<br>
scipy: 0.19.1<br>
pyarrow: 0.7.1<br>
xarray: None<br>
IPython: 6.1.0<br>
sphinx: 1.6.3<br>
patsy: 0.4.1<br>
dateutil: 2.6.1<br>
pytz: 2017.2<br>
blosc: None<br>
bottleneck: 1.2.1<br>
tables: 3.4.2<br>
numexpr: 2.6.2<br>
feather: None<br>
matplotlib: 2.1.0<br>
openpyxl: 2.4.8<br>
xlrd: 1.1.0<br>
xlwt: 1.3.0<br>
xlsxwriter: 1.0.2<br>
lxml: 4.1.0<br>
bs4: 4.6.0<br>
html5lib: 0.999999999<br>
sqlalchemy: 1.1.13<br>
pymysql: None<br>
psycopg2: None<br>
jinja2: 2.9.6<br>
s3fs: None<br>
fastparquet: None<br>
pandas_gbq: None<br>
pandas_datareader: None</p>
</details>
<h4 dir="auto">Code Sample, a copy-pastable example if possible</h4>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="def test_unary():
    df = pd.DataFrame({'x': np.array([0.11, 0], dtype=np.float32)})
    res = df.eval('(x &gt; 0.1) | (x &lt; -0.1)')
    assert np.array_equal(res, np.array([True, False])), res"><pre class="notranslate"><code class="notranslate">def test_unary():
    df = pd.DataFrame({'x': np.array([0.11, 0], dtype=np.float32)})
    res = df.eval('(x &gt; 0.1) | (x &lt; -0.1)')
    assert np.array_equal(res, np.array([True, False])), res
</code></pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto">This is related to <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="109686658" data-permission-text="Title is private" data-url="https://github.com/pandas-dev/pandas/issues/11235" data-hovercard-type="issue" data-hovercard-url="/pandas-dev/pandas/issues/11235/hovercard" href="https://github.com/pandas-dev/pandas/issues/11235">#11235</a>.<br>
on python 3.6, pandas 20.1, this raises an error the traceback ends with:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="  File &quot;.../envs/py3/lib/python3.6/site-packages/pandas/core/computation/expr.py&quot;, line 370, in _maybe_downcast_constants
    name = self.env.add_tmp(np.float32(right.value))
AttributeError: 'UnaryOp' object has no attribute 'value'"><pre class="notranslate"><code class="notranslate">  File ".../envs/py3/lib/python3.6/site-packages/pandas/core/computation/expr.py", line 370, in _maybe_downcast_constants
    name = self.env.add_tmp(np.float32(right.value))
AttributeError: 'UnaryOp' object has no attribute 'value'
</code></pre></div>
<p dir="auto">In that case the right is -(0.1)</p>
<details>
INSTALLED VERSIONS
------------------
commit: None
python: 3.6.1.final.0
python-bits: 64
OS: Linux
OS-release: 4.8.0-49-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
<p dir="auto">pandas: 0.20.1<br>
pytest: None<br>
pip: 9.0.1<br>
setuptools: 27.2.0<br>
Cython: 0.25.2<br>
numpy: 1.12.1<br>
scipy: 0.19.0<br>
xarray: None<br>
IPython: 6.0.0<br>
sphinx: None<br>
patsy: None<br>
dateutil: 2.6.0<br>
pytz: 2017.2<br>
blosc: None<br>
bottleneck: None<br>
tables: None<br>
numexpr: None<br>
feather: None<br>
matplotlib: 2.0.2<br>
openpyxl: None<br>
xlrd: None<br>
xlwt: None<br>
xlsxwriter: None<br>
lxml: None<br>
bs4: None<br>
html5lib: None<br>
sqlalchemy: None<br>
pymysql: None<br>
psycopg2: None<br>
jinja2: None<br>
s3fs: None<br>
pandas_gbq: None<br>
pandas_datareader: None</p>
</details>
<p dir="auto">Another example:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; df = pd.DataFrame({'x':[1,2,3,4,5]})
&gt;&gt;&gt; df.eval('x.shift(-1)')"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">df</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>({<span class="pl-s">'x'</span>:[<span class="pl-c1">1</span>,<span class="pl-c1">2</span>,<span class="pl-c1">3</span>,<span class="pl-c1">4</span>,<span class="pl-c1">5</span>]})
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">df</span>.<span class="pl-en">eval</span>(<span class="pl-s">'x.shift(-1)'</span>)</pre></div>