Skip to content

Commit b6f0292

Browse files
sinhrksjreback
authored andcommitted
TST: allow assert_almost_equal to handle pandas instances
Closes #11584. Because many existing tests expects ``assert_almost_equal`` to compare ``pandas`` instances, it looks better to call appropriate test functions rather than raise. I've also fixed some failure cases caused by the change (comparing ``Series`` and ``list``, etc). Author: sinhrks <[email protected]> Closes #12247 from sinhrks/assert_almost and squashes the following commits: dbf4cc6 [sinhrks] TST: allow assert_almost_equal to handle pandas instances
1 parent a11766f commit b6f0292

21 files changed

+190
-130
lines changed

Diff for: pandas/computation/tests/test_eval.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1580,7 +1580,7 @@ def test_unary_functions(self):
15801580
expr = "{0}(a)".format(fn)
15811581
got = self.eval(expr)
15821582
expect = getattr(np, fn)(a)
1583-
pd.util.testing.assert_almost_equal(got, expect)
1583+
tm.assert_series_equal(got, expect, check_names=False)
15841584

15851585
def test_binary_functions(self):
15861586
df = DataFrame({'a': np.random.randn(10),
@@ -1601,7 +1601,7 @@ def test_df_use_case(self):
16011601
parser=self.parser, inplace=True)
16021602
got = df.e
16031603
expect = np.arctan2(np.sin(df.a), df.b)
1604-
pd.util.testing.assert_almost_equal(got, expect)
1604+
tm.assert_series_equal(got, expect, check_names=False)
16051605

16061606
def test_df_arithmetic_subexpression(self):
16071607
df = DataFrame({'a': np.random.randn(10),
@@ -1611,7 +1611,7 @@ def test_df_arithmetic_subexpression(self):
16111611
parser=self.parser, inplace=True)
16121612
got = df.e
16131613
expect = np.sin(df.a + df.b)
1614-
pd.util.testing.assert_almost_equal(got, expect)
1614+
tm.assert_series_equal(got, expect, check_names=False)
16151615

16161616
def check_result_type(self, dtype, expect_dtype):
16171617
df = DataFrame({'a': np.random.randn(10).astype(dtype)})
@@ -1623,7 +1623,7 @@ def check_result_type(self, dtype, expect_dtype):
16231623
expect = np.sin(df.a)
16241624
self.assertEqual(expect.dtype, got.dtype)
16251625
self.assertEqual(expect_dtype, got.dtype)
1626-
pd.util.testing.assert_almost_equal(got, expect)
1626+
tm.assert_series_equal(got, expect, check_names=False)
16271627

16281628
def test_result_types(self):
16291629
self.check_result_type(np.int32, np.float64)

Diff for: pandas/io/tests/test_cparser.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ def test_integer_thousands_alt(self):
130130
thousands='.', header=None)
131131
result = reader.read()
132132

133-
expected = [123456, 12500]
134-
tm.assert_almost_equal(result[0], expected)
133+
expected = DataFrame([123456, 12500])
134+
tm.assert_frame_equal(result, expected)
135135

136136
def test_skip_bad_lines(self):
137137
# too many lines, see #2430 for why

Diff for: pandas/sparse/tests/test_sparse.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,8 @@ def _compare(idx):
440440

441441
# Corner case
442442
sp = SparseSeries(np.ones(10) * nan)
443-
assert_almost_equal(sp.take([0, 1, 2, 3, 4]), np.repeat(nan, 5))
443+
exp = pd.Series(np.repeat(nan, 5))
444+
tm.assert_series_equal(sp.take([0, 1, 2, 3, 4]), exp)
444445

445446
def test_setitem(self):
446447
self.bseries[5] = 7.
@@ -1872,8 +1873,10 @@ def test_setitem(self):
18721873

18731874
assert_sp_frame_equal(self.panel['ItemE'], self.panel['ItemC'])
18741875
assert_sp_frame_equal(self.panel['ItemF'], self.panel['ItemC'])
1875-
assert_almost_equal(self.panel.items, ['ItemA', 'ItemB', 'ItemC',
1876-
'ItemD', 'ItemE', 'ItemF'])
1876+
1877+
expected = pd.Index(['ItemA', 'ItemB', 'ItemC',
1878+
'ItemD', 'ItemE', 'ItemF'])
1879+
tm.assert_index_equal(self.panel.items, expected)
18771880

18781881
self.assertRaises(Exception, self.panel.__setitem__, 'item6', 1)
18791882

@@ -1890,11 +1893,12 @@ def _check_loc(item, major, minor, val=1.5):
18901893

18911894
def test_delitem_pop(self):
18921895
del self.panel['ItemB']
1893-
assert_almost_equal(self.panel.items, ['ItemA', 'ItemC', 'ItemD'])
1896+
tm.assert_index_equal(self.panel.items,
1897+
pd.Index(['ItemA', 'ItemC', 'ItemD']))
18941898
crackle = self.panel['ItemC']
18951899
pop = self.panel.pop('ItemC')
18961900
self.assertIs(pop, crackle)
1897-
assert_almost_equal(self.panel.items, ['ItemA', 'ItemD'])
1901+
tm.assert_almost_equal(self.panel.items, pd.Index(['ItemA', 'ItemD']))
18981902

18991903
self.assertRaises(KeyError, self.panel.__delitem__, 'ItemC')
19001904

Diff for: pandas/tests/frame/test_alter_axes.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
from pandas import DataFrame, Series, Index, MultiIndex, RangeIndex
1111
import pandas as pd
1212

13-
from pandas.util.testing import (assert_almost_equal,
14-
assert_series_equal,
13+
from pandas.util.testing import (assert_series_equal,
1514
assert_frame_equal,
1615
assertRaisesRegexp)
1716

@@ -447,7 +446,7 @@ def test_reset_index(self):
447446
stacked.index.labels)):
448447
values = lev.take(lab)
449448
name = names[i]
450-
assert_almost_equal(values, deleveled[name])
449+
tm.assert_index_equal(values, Index(deleveled[name]))
451450

452451
stacked.index.names = [None, None]
453452
deleveled2 = stacked.reset_index()

Diff for: pandas/tests/frame/test_constructors.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828

2929
from pandas.core.dtypes import DatetimeTZDtype
3030

31-
from pandas.util.testing import (assert_almost_equal,
32-
assert_numpy_array_equal,
31+
from pandas.util.testing import (assert_numpy_array_equal,
3332
assert_series_equal,
3433
assert_frame_equal,
3534
assertRaisesRegexp)
@@ -359,7 +358,7 @@ def test_constructor_dict_block(self):
359358
expected = [[4., 3., 2., 1.]]
360359
df = DataFrame({'d': [4.], 'c': [3.], 'b': [2.], 'a': [1.]},
361360
columns=['d', 'c', 'b', 'a'])
362-
assert_almost_equal(df.values, expected)
361+
tm.assert_numpy_array_equal(df.values, expected)
363362

364363
def test_constructor_dict_cast(self):
365364
# cast float tests

Diff for: pandas/tests/frame/test_indexing.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1555,7 +1555,7 @@ def testit(df):
15551555
'mask_c': [False, True, False, True]})
15561556
df['mask'] = df.lookup(df.index, 'mask_' + df['label'])
15571557
exp_mask = alt(df, df.index, 'mask_' + df['label'])
1558-
assert_almost_equal(df['mask'], exp_mask)
1558+
tm.assert_series_equal(df['mask'], pd.Series(exp_mask, name='mask'))
15591559
self.assertEqual(df['mask'].dtype, np.bool_)
15601560

15611561
with tm.assertRaises(KeyError):
@@ -2070,7 +2070,9 @@ def test_xs_corner(self):
20702070
df['E'] = 3.
20712071

20722072
xs = df.xs(0)
2073-
assert_almost_equal(xs, [1., 'foo', 2., 'bar', 3.])
2073+
exp = pd.Series([1., 'foo', 2., 'bar', 3.],
2074+
index=list('ABCDE'), name=0)
2075+
tm.assert_series_equal(xs, exp)
20742076

20752077
# no columns but Index(dtype=object)
20762078
df = DataFrame(index=['a', 'b', 'c'])

Diff for: pandas/tests/frame/test_mutate_columns.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77

88
from pandas import DataFrame, Series
99

10-
from pandas.util.testing import (assert_almost_equal,
11-
assert_series_equal,
10+
from pandas.util.testing import (assert_series_equal,
1211
assert_frame_equal,
1312
assertRaisesRegexp)
1413

@@ -125,12 +124,12 @@ def test_insert(self):
125124

126125
df.insert(0, 'foo', df['a'])
127126
self.assert_numpy_array_equal(df.columns, ['foo', 'c', 'b', 'a'])
128-
assert_almost_equal(df['a'], df['foo'])
127+
tm.assert_series_equal(df['a'], df['foo'], check_names=False)
129128

130129
df.insert(2, 'bar', df['c'])
131130
self.assert_numpy_array_equal(df.columns,
132131
['foo', 'c', 'bar', 'b', 'a'])
133-
assert_almost_equal(df['c'], df['bar'])
132+
tm.assert_almost_equal(df['c'], df['bar'], check_names=False)
134133

135134
# diff dtype
136135

Diff for: pandas/tests/series/test_analytics.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -960,10 +960,10 @@ def test_rank(self):
960960
filled = self.ts.fillna(np.inf)
961961

962962
# rankdata returns a ndarray
963-
exp = Series(rankdata(filled), index=filled.index)
963+
exp = Series(rankdata(filled), index=filled.index, name='ts')
964964
exp[mask] = np.nan
965965

966-
assert_almost_equal(ranks, exp)
966+
tm.assert_series_equal(ranks, exp)
967967

968968
iseries = Series(np.arange(5).repeat(2))
969969

Diff for: pandas/tests/series/test_constructors.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
from pandas.compat import lrange, range, zip, OrderedDict, long
1818
from pandas import compat
19-
from pandas.util.testing import assert_series_equal, assert_almost_equal
19+
from pandas.util.testing import assert_series_equal
2020
import pandas.util.testing as tm
2121

2222
from .common import TestData
@@ -213,7 +213,7 @@ def test_constructor_maskedarray(self):
213213

214214
def test_constructor_default_index(self):
215215
s = Series([0, 1, 2])
216-
assert_almost_equal(s.index, np.arange(3))
216+
tm.assert_index_equal(s.index, pd.Index(np.arange(3)))
217217

218218
def test_constructor_corner(self):
219219
df = tm.makeTimeDataFrame()

Diff for: pandas/tests/series/test_operators.py

+19-13
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,15 @@ def test_div(self):
139139
assert_series_equal(result, expected)
140140

141141
def test_operators(self):
142-
def _check_op(series, other, op, pos_only=False):
142+
def _check_op(series, other, op, pos_only=False,
143+
check_dtype=True):
143144
left = np.abs(series) if pos_only else series
144145
right = np.abs(other) if pos_only else other
145146

146147
cython_or_numpy = op(left, right)
147148
python = left.combine(right, op)
148-
tm.assert_almost_equal(cython_or_numpy, python)
149+
tm.assert_series_equal(cython_or_numpy, python,
150+
check_dtype=check_dtype)
149151

150152
def check(series, other):
151153
simple_ops = ['add', 'sub', 'mul', 'truediv', 'floordiv', 'mod']
@@ -169,15 +171,15 @@ def check(series, other):
169171
check(self.ts, self.ts[::2])
170172
check(self.ts, 5)
171173

172-
def check_comparators(series, other):
173-
_check_op(series, other, operator.gt)
174-
_check_op(series, other, operator.ge)
175-
_check_op(series, other, operator.eq)
176-
_check_op(series, other, operator.lt)
177-
_check_op(series, other, operator.le)
174+
def check_comparators(series, other, check_dtype=True):
175+
_check_op(series, other, operator.gt, check_dtype=check_dtype)
176+
_check_op(series, other, operator.ge, check_dtype=check_dtype)
177+
_check_op(series, other, operator.eq, check_dtype=check_dtype)
178+
_check_op(series, other, operator.lt, check_dtype=check_dtype)
179+
_check_op(series, other, operator.le, check_dtype=check_dtype)
178180

179181
check_comparators(self.ts, 5)
180-
check_comparators(self.ts, self.ts + 1)
182+
check_comparators(self.ts, self.ts + 1, check_dtype=False)
181183

182184
def test_operators_empty_int_corner(self):
183185
s1 = Series([], [], dtype=np.int32)
@@ -1256,10 +1258,14 @@ def test_operators_frame(self):
12561258
# rpow does not work with DataFrame
12571259
df = DataFrame({'A': self.ts})
12581260

1259-
tm.assert_almost_equal(self.ts + self.ts, self.ts + df['A'])
1260-
tm.assert_almost_equal(self.ts ** self.ts, self.ts ** df['A'])
1261-
tm.assert_almost_equal(self.ts < self.ts, self.ts < df['A'])
1262-
tm.assert_almost_equal(self.ts / self.ts, self.ts / df['A'])
1261+
tm.assert_series_equal(self.ts + self.ts, self.ts + df['A'],
1262+
check_names=False)
1263+
tm.assert_series_equal(self.ts ** self.ts, self.ts ** df['A'],
1264+
check_names=False)
1265+
tm.assert_series_equal(self.ts < self.ts, self.ts < df['A'],
1266+
check_names=False)
1267+
tm.assert_series_equal(self.ts / self.ts, self.ts / df['A'],
1268+
check_names=False)
12631269

12641270
def test_operators_combine(self):
12651271
def _check_fill(meth, op, a, b, fill_value=0):

Diff for: pandas/tests/test_groupby.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -960,10 +960,13 @@ def test_aggregate_item_by_item(self):
960960

961961
# GH5782
962962
# odd comparisons can result here, so cast to make easy
963-
assert_almost_equal(
964-
result.xs('foo'), np.array([foo] * K).astype('float64'))
965-
assert_almost_equal(
966-
result.xs('bar'), np.array([bar] * K).astype('float64'))
963+
exp = pd.Series(np.array([foo] * K), index=list('BCD'),
964+
dtype=np.float64, name='foo')
965+
tm.assert_series_equal(result.xs('foo'), exp)
966+
967+
exp = pd.Series(np.array([bar] * K), index=list('BCD'),
968+
dtype=np.float64, name='bar')
969+
tm.assert_almost_equal(result.xs('bar'), exp)
967970

968971
def aggfun(ser):
969972
return ser.size
@@ -1390,7 +1393,8 @@ def test_frame_groupby(self):
13901393
for name, group in grouped:
13911394
mean = group.mean()
13921395
for idx in group.index:
1393-
assert_almost_equal(transformed.xs(idx), mean)
1396+
tm.assert_series_equal(transformed.xs(idx), mean,
1397+
check_names=False)
13941398

13951399
# iterate
13961400
for weekday, group in grouped:

0 commit comments

Comments
 (0)