Skip to content

Commit f2ae8e2

Browse files
committed
TST: Check ndarray dtype by default
1 parent 1296ab3 commit f2ae8e2

37 files changed

+718
-506
lines changed

pandas/computation/tests/test_eval.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ def test_unary_in_array(self):
607607
'-37, 37, ~37, +37]'),
608608
np.array([-True, True, ~True, +True,
609609
-False, False, ~False, +False,
610-
-37, 37, ~37, +37]))
610+
-37, 37, ~37, +37], dtype=np.object_))
611611

612612
def test_disallow_scalar_bool_ops(self):
613613
exprs = '1 or 2', '1 and 2'

pandas/core/common.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,8 @@ def array_equivalent(left, right, strict_nan=False):
307307
return False
308308

309309
# Object arrays can contain None, NaN and NaT.
310-
if is_object_dtype(left) or is_object_dtype(right):
310+
# string dtypes must be come to this path for NumPy 1.7.1 compat
311+
if is_string_dtype(left) or is_string_dtype(right):
311312

312313
if not strict_nan:
313314
# pd.isnull considers NaN and None to be equivalent.

pandas/io/tests/json/test_pandas.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def test_frame_from_json_to_json(self):
139139
def _check_orient(df, orient, dtype=None, numpy=False,
140140
convert_axes=True, check_dtype=True, raise_ok=None,
141141
sort=None, check_index_type=True,
142-
check_column_type=True):
142+
check_column_type=True, check_numpy_dtype=False):
143143
if sort is not None:
144144
df = df.sort_values(sort)
145145
else:
@@ -181,22 +181,25 @@ def _check_orient(df, orient, dtype=None, numpy=False,
181181
unser.index.values.astype('i8') * 1e6)
182182
if orient == "records":
183183
# index is not captured in this orientation
184-
assert_almost_equal(df.values, unser.values)
184+
assert_almost_equal(df.values, unser.values,
185+
check_dtype=check_numpy_dtype)
185186
self.assertTrue(df.columns.equals(unser.columns))
186187
elif orient == "values":
187188
# index and cols are not captured in this orientation
188189
if numpy is True and df.shape == (0, 0):
189190
assert unser.shape[0] == 0
190191
else:
191-
assert_almost_equal(df.values, unser.values)
192+
assert_almost_equal(df.values, unser.values,
193+
check_dtype=check_numpy_dtype)
192194
elif orient == "split":
193195
# index and col labels might not be strings
194196
unser.index = [str(i) for i in unser.index]
195197
unser.columns = [str(i) for i in unser.columns]
196198

197199
if sort is None:
198200
unser = unser.sort_index()
199-
assert_almost_equal(df.values, unser.values)
201+
assert_almost_equal(df.values, unser.values,
202+
check_dtype=check_numpy_dtype)
200203
else:
201204
if convert_axes:
202205
assert_frame_equal(df, unser, check_dtype=check_dtype,

pandas/sparse/tests/test_array.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ def test_constructor_bool(self):
272272
self.assertEqual(arr.dtype, bool)
273273
tm.assert_numpy_array_equal(arr.sp_values, np.array([True, True]))
274274
tm.assert_numpy_array_equal(arr.sp_values, np.asarray(arr))
275-
tm.assert_numpy_array_equal(arr.sp_index.indices, np.array([2, 3]))
275+
tm.assert_numpy_array_equal(arr.sp_index.indices,
276+
np.array([2, 3], np.int32))
276277

277278
for dense in [arr.to_dense(), arr.values]:
278279
self.assertEqual(dense.dtype, bool)
@@ -297,9 +298,11 @@ def test_constructor_float32(self):
297298
arr = SparseArray(data, dtype=np.float32)
298299

299300
self.assertEqual(arr.dtype, np.float32)
300-
tm.assert_numpy_array_equal(arr.sp_values, np.array([1, 3]))
301+
tm.assert_numpy_array_equal(arr.sp_values,
302+
np.array([1, 3], dtype=np.float32))
301303
tm.assert_numpy_array_equal(arr.sp_values, np.asarray(arr))
302-
tm.assert_numpy_array_equal(arr.sp_index.indices, np.array([0, 2]))
304+
tm.assert_numpy_array_equal(arr.sp_index.indices,
305+
np.array([0, 2], dtype=np.int32))
303306

304307
for dense in [arr.to_dense(), arr.values]:
305308
self.assertEqual(dense.dtype, np.float32)
@@ -516,7 +519,7 @@ def test_fillna_overlap(self):
516519
# filling with existing value doesn't replace existing value with
517520
# fill_value, i.e. existing 3 remains in sp_values
518521
res = s.fillna(3)
519-
exp = np.array([1, 3, 3, 3, 3])
522+
exp = np.array([1, 3, 3, 3, 3], dtype=np.float64)
520523
tm.assert_numpy_array_equal(res.to_dense(), exp)
521524

522525
s = SparseArray([1, np.nan, np.nan, 3, np.nan], fill_value=0)

pandas/src/testing.pyx

+47-54
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import numpy as np
22

33
from pandas import compat
4-
from pandas.core.common import isnull, array_equivalent
4+
from pandas.core.common import isnull, array_equivalent, is_dtype_equal
55

66
cdef NUMERIC_TYPES = (
77
bool,
@@ -55,7 +55,7 @@ cpdef assert_dict_equal(a, b, bint compare_keys=True):
5555

5656
return True
5757

58-
cpdef assert_almost_equal(a, b, bint check_less_precise=False,
58+
cpdef assert_almost_equal(a, b, bint check_less_precise=False, check_dtype=True,
5959
obj=None, lobj=None, robj=None):
6060
"""Check that left and right objects are almost equal.
6161
@@ -66,6 +66,8 @@ cpdef assert_almost_equal(a, b, bint check_less_precise=False,
6666
check_less_precise : bool, default False
6767
Specify comparison precision.
6868
5 digits (False) or 3 digits (True) after decimal points are compared.
69+
check_dtype: bool, default True
70+
check dtype if both a and b are np.ndarray
6971
obj : str, default None
7072
Specify object name being compared, internally used to show appropriate
7173
assertion message
@@ -82,7 +84,7 @@ cpdef assert_almost_equal(a, b, bint check_less_precise=False,
8284
double diff = 0.0
8385
Py_ssize_t i, na, nb
8486
double fa, fb
85-
bint is_unequal = False
87+
bint is_unequal = False, a_is_ndarray, b_is_ndarray
8688

8789
if lobj is None:
8890
lobj = a
@@ -97,36 +99,43 @@ cpdef assert_almost_equal(a, b, bint check_less_precise=False,
9799
assert a == b, "%r != %r" % (a, b)
98100
return True
99101

102+
a_is_ndarray = isinstance(a, np.ndarray)
103+
b_is_ndarray = isinstance(b, np.ndarray)
104+
105+
if obj is None:
106+
if a_is_ndarray or b_is_ndarray:
107+
obj = 'numpy array'
108+
else:
109+
obj = 'Iterable'
110+
100111
if isiterable(a):
101112

102113
if not isiterable(b):
103-
from pandas.util.testing import raise_assert_detail
104-
if obj is None:
105-
obj = 'Iterable'
106-
msg = "First object is iterable, second isn't"
107-
raise_assert_detail(obj, msg, a, b)
114+
from pandas.util.testing import assert_class_equal
115+
# classes can't be the same, to raise error
116+
assert_class_equal(a, b, obj=obj)
108117

109118
assert has_length(a) and has_length(b), (
110119
"Can't compare objects without length, one or both is invalid: "
111-
"(%r, %r)" % (a, b)
112-
)
120+
"(%r, %r)" % (a, b))
113121

114-
if isinstance(a, np.ndarray) and isinstance(b, np.ndarray):
115-
if obj is None:
116-
obj = 'numpy array'
122+
if a_is_ndarray and b_is_ndarray:
117123
na, nb = a.size, b.size
118124
if a.shape != b.shape:
119125
from pandas.util.testing import raise_assert_detail
120126
raise_assert_detail(obj, '{0} shapes are different'.format(obj),
121127
a.shape, b.shape)
128+
129+
if check_dtype and not is_dtype_equal(a, b):
130+
from pandas.util.testing import assert_attr_equal
131+
assert_attr_equal('dtype', a, b, obj=obj)
132+
122133
try:
123134
if array_equivalent(a, b, strict_nan=True):
124135
return True
125136
except:
126137
pass
127138
else:
128-
if obj is None:
129-
obj = 'Iterable'
130139
na, nb = len(a), len(b)
131140

132141
if na != nb:
@@ -149,54 +158,38 @@ cpdef assert_almost_equal(a, b, bint check_less_precise=False,
149158
return True
150159

151160
elif isiterable(b):
152-
from pandas.util.testing import raise_assert_detail
153-
if obj is None:
154-
obj = 'Iterable'
155-
msg = "Second object is iterable, first isn't"
156-
raise_assert_detail(obj, msg, a, b)
161+
from pandas.util.testing import assert_class_equal
162+
# classes can't be the same, to raise error
163+
assert_class_equal(a, b, obj=obj)
157164

158-
if isnull(a):
159-
assert isnull(b), (
160-
"First object is null, second isn't: %r != %r" % (a, b)
161-
)
165+
if a == b:
166+
# object comparison
162167
return True
163-
elif isnull(b):
164-
assert isnull(a), (
165-
"First object is not null, second is null: %r != %r" % (a, b)
166-
)
168+
if isnull(a) and isnull(b):
169+
# nan / None comparison
167170
return True
168-
169-
if is_comparable_as_number(a):
170-
assert is_comparable_as_number(b), (
171-
"First object is numeric, second is not: %r != %r" % (a, b)
172-
)
171+
if is_comparable_as_number(a) and is_comparable_as_number(b):
172+
if array_equivalent(a, b, strict_nan=True):
173+
# inf comparison
174+
return True
173175

174176
decimal = 5
175177

176178
# deal with differing dtypes
177179
if check_less_precise:
178180
decimal = 3
179181

180-
if np.isinf(a):
181-
assert np.isinf(b), "First object is inf, second isn't"
182-
if np.isposinf(a):
183-
assert np.isposinf(b), "First object is positive inf, second is negative inf"
184-
else:
185-
assert np.isneginf(b), "First object is negative inf, second is positive inf"
182+
fa, fb = a, b
183+
184+
# case for zero
185+
if abs(fa) < 1e-5:
186+
if not decimal_almost_equal(fa, fb, decimal):
187+
assert False, (
188+
'(very low values) expected %.5f but got %.5f, with decimal %d' % (fb, fa, decimal)
189+
)
186190
else:
187-
fa, fb = a, b
188-
189-
# case for zero
190-
if abs(fa) < 1e-5:
191-
if not decimal_almost_equal(fa, fb, decimal):
192-
assert False, (
193-
'(very low values) expected %.5f but got %.5f, with decimal %d' % (fb, fa, decimal)
194-
)
195-
else:
196-
if not decimal_almost_equal(1, fb / fa, decimal):
197-
assert False, 'expected %.5f but got %.5f, with decimal %d' % (fb, fa, decimal)
198-
199-
else:
200-
assert a == b, "%r != %r" % (a, b)
191+
if not decimal_almost_equal(1, fb / fa, decimal):
192+
assert False, 'expected %.5f but got %.5f, with decimal %d' % (fb, fa, decimal)
193+
return True
201194

202-
return True
195+
raise AssertionError("{0} != {1}".format(a, b))

pandas/tests/frame/test_indexing.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1430,17 +1430,18 @@ def test_setitem_frame(self):
14301430

14311431
# already aligned
14321432
f = self.mixed_frame.copy()
1433-
piece = DataFrame([[1, 2], [3, 4]], index=f.index[
1434-
0:2], columns=['A', 'B'])
1433+
piece = DataFrame([[1., 2.], [3., 4.]],
1434+
index=f.index[0:2], columns=['A', 'B'])
14351435
key = (slice(None, 2), ['A', 'B'])
14361436
f.ix[key] = piece
14371437
assert_almost_equal(f.ix[0:2, ['A', 'B']].values,
14381438
piece.values)
14391439

14401440
# rows unaligned
14411441
f = self.mixed_frame.copy()
1442-
piece = DataFrame([[1, 2], [3, 4], [5, 6], [7, 8]], index=list(
1443-
f.index[0:2]) + ['foo', 'bar'], columns=['A', 'B'])
1442+
piece = DataFrame([[1., 2.], [3., 4.], [5., 6.], [7., 8.]],
1443+
index=list(f.index[0:2]) + ['foo', 'bar'],
1444+
columns=['A', 'B'])
14441445
key = (slice(None, 2), ['A', 'B'])
14451446
f.ix[key] = piece
14461447
assert_almost_equal(f.ix[0:2:, ['A', 'B']].values,

pandas/tests/indexes/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ def test_argsort(self):
274274

275275
result = ind.argsort()
276276
expected = np.array(ind).argsort()
277-
tm.assert_numpy_array_equal(result, expected)
277+
tm.assert_numpy_array_equal(result, expected, check_dtype=False)
278278

279279
def test_numpy_argsort(self):
280280
for k, ind in self.indices.items():

pandas/tests/indexes/test_category.py

+31-24
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,15 @@ def test_map(self):
216216
ordered=False)
217217
tm.assert_categorical_equal(result, exp)
218218

219-
tm.assert_numpy_array_equal(ci.map(lambda x: 1), np.array([1] * 5))
219+
tm.assert_numpy_array_equal(ci.map(lambda x: 1),
220+
np.array([1] * 5, dtype=np.int64))
220221

221222
# change categories dtype
222223
ci = pd.CategoricalIndex(list('ABABC'), categories=list('BAC'),
223224
ordered=False)
224225
def f(x):
225226
return {'A': 10, 'B': 20, 'C': 30}.get(x)
227+
226228
result = ci.map(f)
227229
exp = pd.Categorical([10, 20, 10, 20, 30], categories=[20, 10, 30],
228230
ordered=False)
@@ -340,30 +342,35 @@ def test_reindexing(self):
340342
tm.assert_numpy_array_equal(expected, actual)
341343

342344
def test_reindex_dtype(self):
343-
res, indexer = CategoricalIndex(['a', 'b', 'c', 'a']).reindex(['a', 'c'
344-
])
345+
c = CategoricalIndex(['a', 'b', 'c', 'a'])
346+
res, indexer = c.reindex(['a', 'c'])
345347
tm.assert_index_equal(res, Index(['a', 'a', 'c']), exact=True)
346-
tm.assert_numpy_array_equal(indexer, np.array([0, 3, 2]))
347-
348-
res, indexer = CategoricalIndex(['a', 'b', 'c', 'a']).reindex(
349-
Categorical(['a', 'c']))
350-
tm.assert_index_equal(res, CategoricalIndex(
351-
['a', 'a', 'c'], categories=['a', 'c']), exact=True)
352-
tm.assert_numpy_array_equal(indexer, np.array([0, 3, 2]))
353-
354-
res, indexer = CategoricalIndex(
355-
['a', 'b', 'c', 'a'
356-
], categories=['a', 'b', 'c', 'd']).reindex(['a', 'c'])
357-
tm.assert_index_equal(res, Index(
358-
['a', 'a', 'c'], dtype='object'), exact=True)
359-
tm.assert_numpy_array_equal(indexer, np.array([0, 3, 2]))
360-
361-
res, indexer = CategoricalIndex(
362-
['a', 'b', 'c', 'a'],
363-
categories=['a', 'b', 'c', 'd']).reindex(Categorical(['a', 'c']))
364-
tm.assert_index_equal(res, CategoricalIndex(
365-
['a', 'a', 'c'], categories=['a', 'c']), exact=True)
366-
tm.assert_numpy_array_equal(indexer, np.array([0, 3, 2]))
348+
tm.assert_numpy_array_equal(indexer,
349+
np.array([0, 3, 2], dtype=np.int64))
350+
351+
c = CategoricalIndex(['a', 'b', 'c', 'a'])
352+
res, indexer = c.reindex(Categorical(['a', 'c']))
353+
354+
exp = CategoricalIndex(['a', 'a', 'c'], categories=['a', 'c'])
355+
tm.assert_index_equal(res, exp, exact=True)
356+
tm.assert_numpy_array_equal(indexer,
357+
np.array([0, 3, 2], dtype=np.int64))
358+
359+
c = CategoricalIndex(['a', 'b', 'c', 'a'],
360+
categories=['a', 'b', 'c', 'd'])
361+
res, indexer = c.reindex(['a', 'c'])
362+
exp = Index(['a', 'a', 'c'], dtype='object')
363+
tm.assert_index_equal(res, exp, exact=True)
364+
tm.assert_numpy_array_equal(indexer,
365+
np.array([0, 3, 2], dtype=np.int64))
366+
367+
c = CategoricalIndex(['a', 'b', 'c', 'a'],
368+
categories=['a', 'b', 'c', 'd'])
369+
res, indexer = c.reindex(Categorical(['a', 'c']))
370+
exp = CategoricalIndex(['a', 'a', 'c'], categories=['a', 'c'])
371+
tm.assert_index_equal(res, exp, exact=True)
372+
tm.assert_numpy_array_equal(indexer,
373+
np.array([0, 3, 2], dtype=np.int64))
367374

368375
def test_duplicates(self):
369376

0 commit comments

Comments
 (0)