Skip to content

Commit 273946a

Browse files
committed
TST: Add numexpr arithmetic tests.
Only add 'div' if not python 3 Also checks dtype in test cases for series
1 parent fad50af commit 273946a

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

RELEASE.rst

+3
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ pandas 0.11.1
250250
not converting dtypes (GH3911_)
251251
- Fixed a bug where ``DataFrame.replace`` with a compiled regular expression
252252
in the ``to_replace`` argument wasn't working (GH3907_)
253+
- Fixed ``__truediv__`` in Python 2.7 with ``numexpr`` installed to actually do true division when dividing
254+
two integer arrays with at least 10000 cells total (GH3764_)
253255

254256
.. _GH3164: https://github.com/pydata/pandas/issues/3164
255257
.. _GH2786: https://github.com/pydata/pandas/issues/2786
@@ -351,6 +353,7 @@ pandas 0.11.1
351353
.. _GH3907: https://github.com/pydata/pandas/issues/3907
352354
.. _GH3911: https://github.com/pydata/pandas/issues/3911
353355
.. _GH3912: https://github.com/pydata/pandas/issues/3912
356+
.. _GH3764: https://github.com/pydata/pandas/issues/3764
354357

355358
pandas 0.11.0
356359
=============

pandas/tests/test_expressions.py

+49-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
_frame2 = DataFrame(np.random.randn(100, 4), columns = list('ABCD'), dtype='float64')
3131
_mixed = DataFrame({ 'A' : _frame['A'].copy(), 'B' : _frame['B'].astype('float32'), 'C' : _frame['C'].astype('int64'), 'D' : _frame['D'].astype('int32') })
3232
_mixed2 = DataFrame({ 'A' : _frame2['A'].copy(), 'B' : _frame2['B'].astype('float32'), 'C' : _frame2['C'].astype('int64'), 'D' : _frame2['D'].astype('int32') })
33+
_integer = DataFrame(np.random.randint(1, 100, size=(10001, 4)), columns = list('ABCD'), dtype='int64')
3334

3435
class TestExpressions(unittest.TestCase):
3536

@@ -41,7 +42,54 @@ def setUp(self):
4142
self.frame2 = _frame2.copy()
4243
self.mixed = _mixed.copy()
4344
self.mixed2 = _mixed2.copy()
44-
45+
self.integer = _integer.copy()
46+
self._MIN_ELEMENTS = expr._MIN_ELEMENTS
47+
48+
def tearDown(self):
49+
expr._MIN_ELEMENTS = self._MIN_ELEMENTS
50+
51+
@nose.tools.nottest
52+
def run_arithmetic_test(self, df, assert_func, check_dtype=False):
53+
expr._MIN_ELEMENTS = 0
54+
operations = ['add', 'sub', 'mul', 'truediv']
55+
if not py3compat.PY3:
56+
operations.append('div')
57+
for arith in operations:
58+
op = getattr(operator, arith)
59+
expr.set_use_numexpr(False)
60+
expected = op(df, df)
61+
expr.set_use_numexpr(True)
62+
result = op(df, df)
63+
try:
64+
if check_dtype:
65+
if arith == 'div':
66+
assert expected.dtype.kind == df.dtype.kind
67+
if arith == 'truediv':
68+
assert expected.dtype.kind == 'f'
69+
assert_func(expected, result)
70+
except Exception:
71+
print("Failed test with operator %r" % op.__name__)
72+
raise
73+
74+
def test_integer_arithmetic(self):
75+
self.run_arithmetic_test(self.integer, assert_frame_equal)
76+
self.run_arithmetic_test(self.integer.icol(0), assert_series_equal,
77+
check_dtype=True)
78+
79+
def test_float_arithemtic(self):
80+
self.run_arithmetic_test(self.frame, assert_frame_equal)
81+
self.run_arithmetic_test(self.frame.icol(0), assert_series_equal,
82+
check_dtype=True)
83+
84+
def test_mixed_arithmetic(self):
85+
self.run_arithmetic_test(self.mixed, assert_frame_equal)
86+
for col in self.mixed.columns:
87+
self.run_arithmetic_test(self.mixed[col], assert_series_equal)
88+
89+
def test_integer_with_zeros(self):
90+
self.integer *= np.random.randint(0, 2, size=np.shape(self.integer))
91+
self.run_arithmetic_test(self.integer, assert_frame_equal)
92+
self.run_arithmetic_test(self.integer.icol(0), assert_series_equal)
4593

4694
def test_invalid(self):
4795

0 commit comments

Comments
 (0)