Skip to content

Commit

Permalink
Merge pull request #10512 from sinhrks/test_nparray
Browse files Browse the repository at this point in the history
TST: Deprecate assert_numpy_array_equivalent
  • Loading branch information
sinhrks committed Jul 28, 2015
2 parents c06f9ce + 4ae02cf commit e2f1344
Show file tree
Hide file tree
Showing 24 changed files with 332 additions and 361 deletions.
46 changes: 23 additions & 23 deletions pandas/computation/tests/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def check_complex_cmp_op(self, lhs, cmp1, rhs, binop, cmp2):
expected = _eval_single_bin(
lhs_new, binop, rhs_new, self.engine)
result = pd.eval(ex, engine=self.engine, parser=self.parser)
tm.assert_numpy_array_equivalent(result, expected)
tm.assert_numpy_array_equal(result, expected)

def check_chained_cmp_op(self, lhs, cmp1, mid, cmp2, rhs):
skip_these = _scalar_skip
Expand All @@ -240,7 +240,7 @@ def check_operands(left, right, cmp_op):
for ex in (ex1, ex2, ex3):
result = pd.eval(ex, engine=self.engine,
parser=self.parser)
tm.assert_numpy_array_equivalent(result, expected)
tm.assert_numpy_array_equal(result, expected)

def check_simple_cmp_op(self, lhs, cmp1, rhs):
ex = 'lhs {0} rhs'.format(cmp1)
Expand All @@ -251,13 +251,13 @@ def check_simple_cmp_op(self, lhs, cmp1, rhs):
else:
expected = _eval_single_bin(lhs, cmp1, rhs, self.engine)
result = pd.eval(ex, engine=self.engine, parser=self.parser)
tm.assert_numpy_array_equivalent(result, expected)
tm.assert_numpy_array_equal(result, expected)

def check_binary_arith_op(self, lhs, arith1, rhs):
ex = 'lhs {0} rhs'.format(arith1)
result = pd.eval(ex, engine=self.engine, parser=self.parser)
expected = _eval_single_bin(lhs, arith1, rhs, self.engine)
tm.assert_numpy_array_equivalent(result, expected)
tm.assert_numpy_array_equal(result, expected)
ex = 'lhs {0} rhs {0} rhs'.format(arith1)
result = pd.eval(ex, engine=self.engine, parser=self.parser)
nlhs = _eval_single_bin(lhs, arith1, rhs,
Expand All @@ -273,7 +273,7 @@ def check_alignment(self, result, nlhs, ghs, op):
pass
else:
expected = self.ne.evaluate('nlhs {0} ghs'.format(op))
tm.assert_numpy_array_equivalent(result, expected)
tm.assert_numpy_array_equal(result, expected)

# modulus, pow, and floor division require special casing

Expand All @@ -291,7 +291,7 @@ def check_floor_division(self, lhs, arith1, rhs):
if self.engine == 'python':
res = pd.eval(ex, engine=self.engine, parser=self.parser)
expected = lhs // rhs
tm.assert_numpy_array_equivalent(res, expected)
tm.assert_numpy_array_equal(res, expected)
else:
self.assertRaises(TypeError, pd.eval, ex, local_dict={'lhs': lhs,
'rhs': rhs},
Expand Down Expand Up @@ -325,8 +325,8 @@ def check_pow(self, lhs, arith1, rhs):

if (np.isscalar(lhs) and np.isscalar(rhs) and
_is_py3_complex_incompat(result, expected)):
self.assertRaises(AssertionError, tm.assert_numpy_array_equivalent, result,
expected)
self.assertRaises(AssertionError, tm.assert_numpy_array_equal,
result, expected)
else:
assert_allclose(result, expected)

Expand All @@ -345,12 +345,12 @@ def check_single_invert_op(self, lhs, cmp1, rhs):
elb = np.array([bool(el)])
expected = ~elb
result = pd.eval('~elb', engine=self.engine, parser=self.parser)
tm.assert_numpy_array_equivalent(expected, result)
tm.assert_numpy_array_equal(expected, result)

for engine in self.current_engines:
tm.skip_if_no_ne(engine)
tm.assert_numpy_array_equivalent(result, pd.eval('~elb', engine=engine,
parser=self.parser))
tm.assert_numpy_array_equal(result, pd.eval('~elb', engine=engine,
parser=self.parser))

def check_compound_invert_op(self, lhs, cmp1, rhs):
skip_these = 'in', 'not in'
Expand All @@ -370,13 +370,13 @@ def check_compound_invert_op(self, lhs, cmp1, rhs):
else:
expected = ~expected
result = pd.eval(ex, engine=self.engine, parser=self.parser)
tm.assert_numpy_array_equivalent(expected, result)
tm.assert_numpy_array_equal(expected, result)

# make sure the other engines work the same as this one
for engine in self.current_engines:
tm.skip_if_no_ne(engine)
ev = pd.eval(ex, engine=self.engine, parser=self.parser)
tm.assert_numpy_array_equivalent(ev, result)
tm.assert_numpy_array_equal(ev, result)

def ex(self, op, var_name='lhs'):
return '{0}{1}'.format(op, var_name)
Expand Down Expand Up @@ -639,17 +639,17 @@ def test_identical(self):

x = np.array([1])
result = pd.eval('x', engine=self.engine, parser=self.parser)
tm.assert_numpy_array_equivalent(result, np.array([1]))
tm.assert_numpy_array_equal(result, np.array([1]))
self.assertEqual(result.shape, (1, ))

x = np.array([1.5])
result = pd.eval('x', engine=self.engine, parser=self.parser)
tm.assert_numpy_array_equivalent(result, np.array([1.5]))
tm.assert_numpy_array_equal(result, np.array([1.5]))
self.assertEqual(result.shape, (1, ))

x = np.array([False])
result = pd.eval('x', engine=self.engine, parser=self.parser)
tm.assert_numpy_array_equivalent(result, np.array([False]))
tm.assert_numpy_array_equal(result, np.array([False]))
self.assertEqual(result.shape, (1, ))


Expand Down Expand Up @@ -707,7 +707,7 @@ def check_alignment(self, result, nlhs, ghs, op):
pass
else:
expected = eval('nlhs {0} ghs'.format(op))
tm.assert_numpy_array_equivalent(result, expected)
tm.assert_numpy_array_equal(result, expected)


class TestEvalPythonPandas(TestEvalPythonPython):
Expand Down Expand Up @@ -1118,10 +1118,10 @@ def test_truediv(self):

if PY3:
res = self.eval(ex, truediv=False)
tm.assert_numpy_array_equivalent(res, np.array([1.0]))
tm.assert_numpy_array_equal(res, np.array([1.0]))

res = self.eval(ex, truediv=True)
tm.assert_numpy_array_equivalent(res, np.array([1.0]))
tm.assert_numpy_array_equal(res, np.array([1.0]))

res = self.eval('1 / 2', truediv=True)
expec = 0.5
Expand All @@ -1140,10 +1140,10 @@ def test_truediv(self):
self.assertEqual(res, expec)
else:
res = self.eval(ex, truediv=False)
tm.assert_numpy_array_equivalent(res, np.array([1]))
tm.assert_numpy_array_equal(res, np.array([1]))

res = self.eval(ex, truediv=True)
tm.assert_numpy_array_equivalent(res, np.array([1.0]))
tm.assert_numpy_array_equal(res, np.array([1.0]))

res = self.eval('1 / 2', truediv=True)
expec = 0.5
Expand Down Expand Up @@ -1446,8 +1446,8 @@ class TestScope(object):

def check_global_scope(self, e, engine, parser):
tm.skip_if_no_ne(engine)
tm.assert_numpy_array_equivalent(_var_s * 2, pd.eval(e, engine=engine,
parser=parser))
tm.assert_numpy_array_equal(_var_s * 2, pd.eval(e, engine=engine,
parser=parser))

def test_global_scope(self):
e = '_var_s * 2'
Expand Down
3 changes: 1 addition & 2 deletions pandas/io/tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from pandas.util.testing import (assert_series_equal, assert_produces_warning,
network, assert_frame_equal)
import pandas.util.testing as tm
from numpy.testing import assert_array_equal

if compat.PY3:
from urllib.error import HTTPError
Expand Down Expand Up @@ -533,7 +532,7 @@ def test_fred_part2(self):
[848.3],
[933.3]]
result = web.get_data_fred("A09024USA144NNBR", start="1915").ix[:5]
assert_array_equal(result.values, np.array(expected))
tm.assert_numpy_array_equal(result.values, np.array(expected))

@network
def test_invalid_series(self):
Expand Down
77 changes: 38 additions & 39 deletions pandas/io/tests/test_json/test_ujson.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
import pandas.compat as compat

import numpy as np
from numpy.testing import (assert_array_equal,
assert_array_almost_equal_nulp,
from numpy.testing import (assert_array_almost_equal_nulp,
assert_approx_equal)
import pytz
import dateutil
Expand Down Expand Up @@ -166,15 +165,15 @@ def test_encodeArrayOfNestedArrays(self):
#self.assertEqual(output, json.dumps(input))
self.assertEqual(input, ujson.decode(output))
input = np.array(input)
assert_array_equal(input, ujson.decode(output, numpy=True, dtype=input.dtype))
tm.assert_numpy_array_equal(input, ujson.decode(output, numpy=True, dtype=input.dtype))

def test_encodeArrayOfDoubles(self):
input = [ 31337.31337, 31337.31337, 31337.31337, 31337.31337] * 10
output = ujson.encode(input)
self.assertEqual(input, json.loads(output))
#self.assertEqual(output, json.dumps(input))
self.assertEqual(input, ujson.decode(output))
assert_array_equal(np.array(input), ujson.decode(output, numpy=True))
tm.assert_numpy_array_equal(np.array(input), ujson.decode(output, numpy=True))

def test_doublePrecisionTest(self):
input = 30.012345678901234
Expand Down Expand Up @@ -271,7 +270,7 @@ def test_encodeArrayInArray(self):
self.assertEqual(input, json.loads(output))
self.assertEqual(output, json.dumps(input))
self.assertEqual(input, ujson.decode(output))
assert_array_equal(np.array(input), ujson.decode(output, numpy=True))
tm.assert_numpy_array_equal(np.array(input), ujson.decode(output, numpy=True))
pass

def test_encodeIntConversion(self):
Expand Down Expand Up @@ -307,7 +306,7 @@ def test_encodeListConversion(self):
output = ujson.encode(input)
self.assertEqual(input, json.loads(output))
self.assertEqual(input, ujson.decode(output))
assert_array_equal(np.array(input), ujson.decode(output, numpy=True))
tm.assert_numpy_array_equal(np.array(input), ujson.decode(output, numpy=True))
pass

def test_encodeDictConversion(self):
Expand Down Expand Up @@ -676,8 +675,8 @@ def test_encodeListLongConversion(self):
output = ujson.encode(input)
self.assertEqual(input, json.loads(output))
self.assertEqual(input, ujson.decode(output))
assert_array_equal(np.array(input), ujson.decode(output, numpy=True,
dtype=np.int64))
tm.assert_numpy_array_equal(np.array(input), ujson.decode(output, numpy=True,
dtype=np.int64))
pass

def test_encodeLongConversion(self):
Expand Down Expand Up @@ -755,7 +754,7 @@ def test_loadFile(self):
f = StringIO("[1,2,3,4]")
self.assertEqual([1, 2, 3, 4], ujson.load(f))
f = StringIO("[1,2,3,4]")
assert_array_equal(np.array([1, 2, 3, 4]), ujson.load(f, numpy=True))
tm.assert_numpy_array_equal(np.array([1, 2, 3, 4]), ujson.load(f, numpy=True))

def test_loadFileLikeObject(self):
class filelike:
Expand All @@ -768,7 +767,7 @@ def read(self):
f = filelike()
self.assertEqual([1, 2, 3, 4], ujson.load(f))
f = filelike()
assert_array_equal(np.array([1, 2, 3, 4]), ujson.load(f, numpy=True))
tm.assert_numpy_array_equal(np.array([1, 2, 3, 4]), ujson.load(f, numpy=True))

def test_loadFileArgsError(self):
try:
Expand Down Expand Up @@ -906,7 +905,7 @@ def testBoolArray(self):
inpt = np.array([True, False, True, True, False, True, False , False],
dtype=np.bool)
outp = np.array(ujson.decode(ujson.encode(inpt)), dtype=np.bool)
assert_array_equal(inpt, outp)
tm.assert_numpy_array_equal(inpt, outp)

def testInt(self):
num = np.int(2562010)
Expand Down Expand Up @@ -943,7 +942,7 @@ def testIntArray(self):
for dtype in dtypes:
inpt = arr.astype(dtype)
outp = np.array(ujson.decode(ujson.encode(inpt)), dtype=dtype)
assert_array_equal(inpt, outp)
tm.assert_numpy_array_equal(inpt, outp)

def testIntMax(self):
num = np.int(np.iinfo(np.int).max)
Expand Down Expand Up @@ -1008,26 +1007,26 @@ def testArrays(self):
arr = np.arange(100);

arr = arr.reshape((10, 10))
assert_array_equal(np.array(ujson.decode(ujson.encode(arr))), arr)
assert_array_equal(ujson.decode(ujson.encode(arr), numpy=True), arr)
tm.assert_numpy_array_equal(np.array(ujson.decode(ujson.encode(arr))), arr)
tm.assert_numpy_array_equal(ujson.decode(ujson.encode(arr), numpy=True), arr)

arr = arr.reshape((5, 5, 4))
assert_array_equal(np.array(ujson.decode(ujson.encode(arr))), arr)
assert_array_equal(ujson.decode(ujson.encode(arr), numpy=True), arr)
tm.assert_numpy_array_equal(np.array(ujson.decode(ujson.encode(arr))), arr)
tm.assert_numpy_array_equal(ujson.decode(ujson.encode(arr), numpy=True), arr)

arr = arr.reshape((100, 1))
assert_array_equal(np.array(ujson.decode(ujson.encode(arr))), arr)
assert_array_equal(ujson.decode(ujson.encode(arr), numpy=True), arr)
tm.assert_numpy_array_equal(np.array(ujson.decode(ujson.encode(arr))), arr)
tm.assert_numpy_array_equal(ujson.decode(ujson.encode(arr), numpy=True), arr)

arr = np.arange(96);
arr = arr.reshape((2, 2, 2, 2, 3, 2))
assert_array_equal(np.array(ujson.decode(ujson.encode(arr))), arr)
assert_array_equal(ujson.decode(ujson.encode(arr), numpy=True), arr)
tm.assert_numpy_array_equal(np.array(ujson.decode(ujson.encode(arr))), arr)
tm.assert_numpy_array_equal(ujson.decode(ujson.encode(arr), numpy=True), arr)

l = ['a', list(), dict(), dict(), list(),
42, 97.8, ['a', 'b'], {'key': 'val'}]
arr = np.array(l)
assert_array_equal(np.array(ujson.decode(ujson.encode(arr))), arr)
tm.assert_numpy_array_equal(np.array(ujson.decode(ujson.encode(arr))), arr)

arr = np.arange(100.202, 200.202, 1, dtype=np.float32);
arr = arr.reshape((5, 5, 4))
Expand Down Expand Up @@ -1158,49 +1157,49 @@ def testDataFrame(self):
# column indexed
outp = DataFrame(ujson.decode(ujson.encode(df)))
self.assertTrue((df == outp).values.all())
assert_array_equal(df.columns, outp.columns)
assert_array_equal(df.index, outp.index)
tm.assert_numpy_array_equal(df.columns, outp.columns)
tm.assert_numpy_array_equal(df.index, outp.index)

dec = _clean_dict(ujson.decode(ujson.encode(df, orient="split")))
outp = DataFrame(**dec)
self.assertTrue((df == outp).values.all())
assert_array_equal(df.columns, outp.columns)
assert_array_equal(df.index, outp.index)
tm.assert_numpy_array_equal(df.columns, outp.columns)
tm.assert_numpy_array_equal(df.index, outp.index)

outp = DataFrame(ujson.decode(ujson.encode(df, orient="records")))
outp.index = df.index
self.assertTrue((df == outp).values.all())
assert_array_equal(df.columns, outp.columns)
tm.assert_numpy_array_equal(df.columns, outp.columns)

outp = DataFrame(ujson.decode(ujson.encode(df, orient="values")))
outp.index = df.index
self.assertTrue((df.values == outp.values).all())

outp = DataFrame(ujson.decode(ujson.encode(df, orient="index")))
self.assertTrue((df.transpose() == outp).values.all())
assert_array_equal(df.transpose().columns, outp.columns)
assert_array_equal(df.transpose().index, outp.index)
tm.assert_numpy_array_equal(df.transpose().columns, outp.columns)
tm.assert_numpy_array_equal(df.transpose().index, outp.index)

def testDataFrameNumpy(self):
df = DataFrame([[1,2,3], [4,5,6]], index=['a', 'b'], columns=['x', 'y', 'z'])

# column indexed
outp = DataFrame(ujson.decode(ujson.encode(df), numpy=True))
self.assertTrue((df == outp).values.all())
assert_array_equal(df.columns, outp.columns)
assert_array_equal(df.index, outp.index)
tm.assert_numpy_array_equal(df.columns, outp.columns)
tm.assert_numpy_array_equal(df.index, outp.index)

dec = _clean_dict(ujson.decode(ujson.encode(df, orient="split"),
numpy=True))
outp = DataFrame(**dec)
self.assertTrue((df == outp).values.all())
assert_array_equal(df.columns, outp.columns)
assert_array_equal(df.index, outp.index)
tm.assert_numpy_array_equal(df.columns, outp.columns)
tm.assert_numpy_array_equal(df.index, outp.index)

outp = DataFrame(ujson.decode(ujson.encode(df, orient="index"), numpy=True))
self.assertTrue((df.transpose() == outp).values.all())
assert_array_equal(df.transpose().columns, outp.columns)
assert_array_equal(df.transpose().index, outp.index)
tm.assert_numpy_array_equal(df.transpose().columns, outp.columns)
tm.assert_numpy_array_equal(df.transpose().index, outp.index)

def testDataFrameNested(self):
df = DataFrame([[1,2,3], [4,5,6]], index=['a', 'b'], columns=['x', 'y', 'z'])
Expand Down Expand Up @@ -1233,18 +1232,18 @@ def testDataFrameNumpyLabelled(self):
# column indexed
outp = DataFrame(*ujson.decode(ujson.encode(df), numpy=True, labelled=True))
self.assertTrue((df.T == outp).values.all())
assert_array_equal(df.T.columns, outp.columns)
assert_array_equal(df.T.index, outp.index)
tm.assert_numpy_array_equal(df.T.columns, outp.columns)
tm.assert_numpy_array_equal(df.T.index, outp.index)

outp = DataFrame(*ujson.decode(ujson.encode(df, orient="records"), numpy=True, labelled=True))
outp.index = df.index
self.assertTrue((df == outp).values.all())
assert_array_equal(df.columns, outp.columns)
tm.assert_numpy_array_equal(df.columns, outp.columns)

outp = DataFrame(*ujson.decode(ujson.encode(df, orient="index"), numpy=True, labelled=True))
self.assertTrue((df == outp).values.all())
assert_array_equal(df.columns, outp.columns)
assert_array_equal(df.index, outp.index)
tm.assert_numpy_array_equal(df.columns, outp.columns)
tm.assert_numpy_array_equal(df.index, outp.index)

def testSeries(self):
s = Series([10, 20, 30, 40, 50, 60], name="series", index=[6,7,8,9,10,15])
Expand Down
Loading

0 comments on commit e2f1344

Please sign in to comment.