Skip to content

Commit 887fe22

Browse files
jankatinsjreback
authored andcommitted
TST: Add TestCase.assert_numpy_array_equivalent(..) and docstrings
1 parent 586e317 commit 887fe22

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

pandas/util/testing.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import numpy as np
2424

2525
import pandas as pd
26-
from pandas.core.common import _is_sequence
26+
from pandas.core.common import _is_sequence, array_equivalent
2727
import pandas.core.index as index
2828
import pandas.core.series as series
2929
import pandas.core.frame as frame
@@ -86,10 +86,31 @@ def reset_display_options(self):
8686
pd.reset_option('^display.',silent=True)
8787

8888
def assert_numpy_array_equal(self, np_array, assert_equal):
89+
"""Checks that 'np_array' is equal to 'assert_equal'
90+
91+
Note that the expected array should not contain `np.nan`! Two numpy arrays are equal if all
92+
elements are equal, which is not possible if `np.nan` is such an element!
93+
94+
If the expected array includes `np.nan` use `assert_numpy_array_equivalent(...)`.
95+
"""
8996
if np.array_equal(np_array, assert_equal):
9097
return
9198
raise AssertionError('{0} is not equal to {1}.'.format(np_array, assert_equal))
9299

100+
def assert_numpy_array_equivalent(self, np_array, assert_equal):
101+
"""Checks that 'np_array' is equivalent to 'assert_equal'
102+
103+
Two numpy arrays are equivalent if the arrays have equal non-NaN elements, and
104+
`np.nan` in corresponding locations.
105+
106+
If the the expected array does not contain `np.nan` `assert_numpy_array_equivalent` is the
107+
similar to `assert_numpy_array_equal()`. If the expected array includes `np.nan` use this
108+
function.
109+
"""
110+
if array_equivalent(np_array, assert_equal):
111+
return
112+
raise AssertionError('{0} is not equivalent to {1}.'.format(np_array, assert_equal))
113+
93114
def assertIs(self, first, second, msg=''):
94115
"""Checks that 'first' is 'second'"""
95116
a, b = first, second

0 commit comments

Comments
 (0)