Skip to content

Commit b386184

Browse files
committed
BUG: float trunc in eval with py 2
1 parent a7469cf commit b386184

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

doc/source/whatsnew/v0.19.0.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1567,7 +1567,7 @@ Bug Fixes
15671567
- Bug in ``DataFrame.to_csv()`` with ``MultiIndex`` columns in which a stray empty line was added (:issue:`6618`)
15681568
- Bug in ``DatetimeIndex``, ``TimedeltaIndex`` and ``PeriodIndex.equals()`` may return ``True`` when input isn't ``Index`` but contains the same values (:issue:`13107`)
15691569
- Bug in assignment against datetime with timezone may not work if it contains datetime near DST boundary (:issue:`14146`)
1570-
1570+
- Bug in ``pd.eval()`` truncating long float literals with python 2 (:issue:`14241`)
15711571
- Bug in ``Index`` raises ``KeyError`` displaying incorrect column when column is not in the df and columns contains duplicate values (:issue:`13822`)
15721572
- Bug in ``Period`` and ``PeriodIndex`` creating wrong dates when frequency has combined offset aliases (:issue:`13874`)
15731573
- Bug in ``.to_string()`` when called with an integer ``line_width`` and ``index=False`` raises an UnboundLocalError exception because ``idx`` referenced before assignment.

pandas/computation/ops.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ def _resolve_name(self):
166166
def name(self):
167167
return self.value
168168

169+
def __unicode__(self):
170+
# in python 2 str() of float
171+
# can truncate shorter than repr()
172+
return repr(self.name)
173+
169174

170175
_bool_op_map = {'not': '~', 'and': '&', 'or': '|'}
171176

pandas/computation/tests/test_eval.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,18 @@ def test_line_continuation(self):
678678
result = pd.eval(exp, engine=self.engine, parser=self.parser)
679679
self.assertEqual(result, 12)
680680

681+
def test_float_truncation(self):
682+
# GH 14241
683+
exp = '1000000000.006'
684+
result = pd.eval(exp, engine=self.engine, parser=self.parser)
685+
expected = np.float64(exp)
686+
self.assertEqual(result, expected)
687+
688+
df = pd.DataFrame([{"A": 1000000000.0099}])
689+
cutoff = 1000000000.006
690+
result = df.query("A < %.3f" % cutoff)
691+
self.assertTrue(result.empty)
692+
681693

682694
class TestEvalNumexprPython(TestEvalNumexprPandas):
683695

0 commit comments

Comments
 (0)