Skip to content

TST: allow assert_almost_equal to handle pandas instances #12247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pandas/computation/tests/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1580,7 +1580,7 @@ def test_unary_functions(self):
expr = "{0}(a)".format(fn)
got = self.eval(expr)
expect = getattr(np, fn)(a)
pd.util.testing.assert_almost_equal(got, expect)
tm.assert_series_equal(got, expect, check_names=False)

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

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

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

def test_result_types(self):
self.check_result_type(np.int32, np.float64)
Expand Down
4 changes: 2 additions & 2 deletions pandas/io/tests/test_cparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ def test_integer_thousands_alt(self):
thousands='.', header=None)
result = reader.read()

expected = [123456, 12500]
tm.assert_almost_equal(result[0], expected)
expected = DataFrame([123456, 12500])
tm.assert_frame_equal(result, expected)

def test_skip_bad_lines(self):
# too many lines, see #2430 for why
Expand Down
14 changes: 9 additions & 5 deletions pandas/sparse/tests/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,8 @@ def _compare(idx):

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

def test_setitem(self):
self.bseries[5] = 7.
Expand Down Expand Up @@ -1872,8 +1873,10 @@ def test_setitem(self):

assert_sp_frame_equal(self.panel['ItemE'], self.panel['ItemC'])
assert_sp_frame_equal(self.panel['ItemF'], self.panel['ItemC'])
assert_almost_equal(self.panel.items, ['ItemA', 'ItemB', 'ItemC',
'ItemD', 'ItemE', 'ItemF'])

expected = pd.Index(['ItemA', 'ItemB', 'ItemC',
'ItemD', 'ItemE', 'ItemF'])
tm.assert_index_equal(self.panel.items, expected)

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

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

def test_delitem_pop(self):
del self.panel['ItemB']
assert_almost_equal(self.panel.items, ['ItemA', 'ItemC', 'ItemD'])
tm.assert_index_equal(self.panel.items,
pd.Index(['ItemA', 'ItemC', 'ItemD']))
crackle = self.panel['ItemC']
pop = self.panel.pop('ItemC')
self.assertIs(pop, crackle)
assert_almost_equal(self.panel.items, ['ItemA', 'ItemD'])
tm.assert_almost_equal(self.panel.items, pd.Index(['ItemA', 'ItemD']))

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

Expand Down
5 changes: 2 additions & 3 deletions pandas/tests/frame/test_alter_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
from pandas import DataFrame, Series, Index, MultiIndex, RangeIndex
import pandas as pd

from pandas.util.testing import (assert_almost_equal,
assert_series_equal,
from pandas.util.testing import (assert_series_equal,
assert_frame_equal,
assertRaisesRegexp)

Expand Down Expand Up @@ -447,7 +446,7 @@ def test_reset_index(self):
stacked.index.labels)):
values = lev.take(lab)
name = names[i]
assert_almost_equal(values, deleveled[name])
tm.assert_index_equal(values, Index(deleveled[name]))

stacked.index.names = [None, None]
deleveled2 = stacked.reset_index()
Expand Down
5 changes: 2 additions & 3 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@

from pandas.core.dtypes import DatetimeTZDtype

from pandas.util.testing import (assert_almost_equal,
assert_numpy_array_equal,
from pandas.util.testing import (assert_numpy_array_equal,
assert_series_equal,
assert_frame_equal,
assertRaisesRegexp)
Expand Down Expand Up @@ -359,7 +358,7 @@ def test_constructor_dict_block(self):
expected = [[4., 3., 2., 1.]]
df = DataFrame({'d': [4.], 'c': [3.], 'b': [2.], 'a': [1.]},
columns=['d', 'c', 'b', 'a'])
assert_almost_equal(df.values, expected)
tm.assert_numpy_array_equal(df.values, expected)

def test_constructor_dict_cast(self):
# cast float tests
Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/frame/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1555,7 +1555,7 @@ def testit(df):
'mask_c': [False, True, False, True]})
df['mask'] = df.lookup(df.index, 'mask_' + df['label'])
exp_mask = alt(df, df.index, 'mask_' + df['label'])
assert_almost_equal(df['mask'], exp_mask)
tm.assert_series_equal(df['mask'], pd.Series(exp_mask, name='mask'))
self.assertEqual(df['mask'].dtype, np.bool_)

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

xs = df.xs(0)
assert_almost_equal(xs, [1., 'foo', 2., 'bar', 3.])
exp = pd.Series([1., 'foo', 2., 'bar', 3.],
index=list('ABCDE'), name=0)
tm.assert_series_equal(xs, exp)

# no columns but Index(dtype=object)
df = DataFrame(index=['a', 'b', 'c'])
Expand Down
7 changes: 3 additions & 4 deletions pandas/tests/frame/test_mutate_columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

from pandas import DataFrame, Series

from pandas.util.testing import (assert_almost_equal,
assert_series_equal,
from pandas.util.testing import (assert_series_equal,
assert_frame_equal,
assertRaisesRegexp)

Expand Down Expand Up @@ -125,12 +124,12 @@ def test_insert(self):

df.insert(0, 'foo', df['a'])
self.assert_numpy_array_equal(df.columns, ['foo', 'c', 'b', 'a'])
assert_almost_equal(df['a'], df['foo'])
tm.assert_series_equal(df['a'], df['foo'], check_names=False)

df.insert(2, 'bar', df['c'])
self.assert_numpy_array_equal(df.columns,
['foo', 'c', 'bar', 'b', 'a'])
assert_almost_equal(df['c'], df['bar'])
tm.assert_almost_equal(df['c'], df['bar'], check_names=False)

# diff dtype

Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/series/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -960,10 +960,10 @@ def test_rank(self):
filled = self.ts.fillna(np.inf)

# rankdata returns a ndarray
exp = Series(rankdata(filled), index=filled.index)
exp = Series(rankdata(filled), index=filled.index, name='ts')
exp[mask] = np.nan

assert_almost_equal(ranks, exp)
tm.assert_series_equal(ranks, exp)

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

Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from pandas.compat import lrange, range, zip, OrderedDict, long
from pandas import compat
from pandas.util.testing import assert_series_equal, assert_almost_equal
from pandas.util.testing import assert_series_equal
import pandas.util.testing as tm

from .common import TestData
Expand Down Expand Up @@ -213,7 +213,7 @@ def test_constructor_maskedarray(self):

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

def test_constructor_corner(self):
df = tm.makeTimeDataFrame()
Expand Down
32 changes: 19 additions & 13 deletions pandas/tests/series/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,15 @@ def test_div(self):
assert_series_equal(result, expected)

def test_operators(self):
def _check_op(series, other, op, pos_only=False):
def _check_op(series, other, op, pos_only=False,
check_dtype=True):
left = np.abs(series) if pos_only else series
right = np.abs(other) if pos_only else other

cython_or_numpy = op(left, right)
python = left.combine(right, op)
tm.assert_almost_equal(cython_or_numpy, python)
tm.assert_series_equal(cython_or_numpy, python,
check_dtype=check_dtype)

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

def check_comparators(series, other):
_check_op(series, other, operator.gt)
_check_op(series, other, operator.ge)
_check_op(series, other, operator.eq)
_check_op(series, other, operator.lt)
_check_op(series, other, operator.le)
def check_comparators(series, other, check_dtype=True):
_check_op(series, other, operator.gt, check_dtype=check_dtype)
_check_op(series, other, operator.ge, check_dtype=check_dtype)
_check_op(series, other, operator.eq, check_dtype=check_dtype)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

were these just not being checked before?

_check_op(series, other, operator.lt, check_dtype=check_dtype)
_check_op(series, other, operator.le, check_dtype=check_dtype)

check_comparators(self.ts, 5)
check_comparators(self.ts, self.ts + 1)
check_comparators(self.ts, self.ts + 1, check_dtype=False)

def test_operators_empty_int_corner(self):
s1 = Series([], [], dtype=np.int32)
Expand Down Expand Up @@ -1245,10 +1247,14 @@ def test_operators_frame(self):
# rpow does not work with DataFrame
df = DataFrame({'A': self.ts})

tm.assert_almost_equal(self.ts + self.ts, self.ts + df['A'])
tm.assert_almost_equal(self.ts ** self.ts, self.ts ** df['A'])
tm.assert_almost_equal(self.ts < self.ts, self.ts < df['A'])
tm.assert_almost_equal(self.ts / self.ts, self.ts / df['A'])
tm.assert_series_equal(self.ts + self.ts, self.ts + df['A'],
check_names=False)
tm.assert_series_equal(self.ts ** self.ts, self.ts ** df['A'],
check_names=False)
tm.assert_series_equal(self.ts < self.ts, self.ts < df['A'],
check_names=False)
tm.assert_series_equal(self.ts / self.ts, self.ts / df['A'],
check_names=False)

def test_operators_combine(self):
def _check_fill(meth, op, a, b, fill_value=0):
Expand Down
14 changes: 9 additions & 5 deletions pandas/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -960,10 +960,13 @@ def test_aggregate_item_by_item(self):

# GH5782
# odd comparisons can result here, so cast to make easy
assert_almost_equal(
result.xs('foo'), np.array([foo] * K).astype('float64'))
assert_almost_equal(
result.xs('bar'), np.array([bar] * K).astype('float64'))
exp = pd.Series(np.array([foo] * K), index=list('BCD'),
dtype=np.float64, name='foo')
tm.assert_series_equal(result.xs('foo'), exp)

exp = pd.Series(np.array([bar] * K), index=list('BCD'),
dtype=np.float64, name='bar')
tm.assert_almost_equal(result.xs('bar'), exp)

def aggfun(ser):
return ser.size
Expand Down Expand Up @@ -1390,7 +1393,8 @@ def test_frame_groupby(self):
for name, group in grouped:
mean = group.mean()
for idx in group.index:
assert_almost_equal(transformed.xs(idx), mean)
tm.assert_series_equal(transformed.xs(idx), mean,
check_names=False)

# iterate
for weekday, group in grouped:
Expand Down
Loading