Skip to content

TST: Make numpy_array test strict #13311

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
16 changes: 13 additions & 3 deletions pandas/computation/tests/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,16 @@ def test_chained_cmp_op(self):
mids, cmp_ops, self.rhses):
self.check_chained_cmp_op(lhs, cmp1, mid, cmp2, rhs)

def check_equal(self, result, expected):
if isinstance(result, DataFrame):
tm.assert_frame_equal(result, expected)
elif isinstance(result, Series):
tm.assert_series_equal(result, expected)
elif isinstance(result, np.ndarray):
tm.assert_numpy_array_equal(result, expected)
else:
self.assertEqual(result, expected)

def check_complex_cmp_op(self, lhs, cmp1, rhs, binop, cmp2):
skip_these = _scalar_skip
ex = '(lhs {cmp1} rhs) {binop} (lhs {cmp2} rhs)'.format(cmp1=cmp1,
Expand Down Expand Up @@ -218,7 +228,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_equal(result, expected)
self.check_equal(result, expected)

def check_chained_cmp_op(self, lhs, cmp1, mid, cmp2, rhs):
skip_these = _scalar_skip
Expand Down Expand Up @@ -249,7 +259,7 @@ 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_equal(result, expected)
self.check_equal(result, expected)

def check_binary_arith_op(self, lhs, arith1, rhs):
ex = 'lhs {0} rhs'.format(arith1)
Expand Down Expand Up @@ -293,7 +303,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_equal(res, expected)
self.check_equal(res, expected)
else:
self.assertRaises(TypeError, pd.eval, ex, local_dict={'lhs': lhs,
'rhs': rhs},
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,8 @@ def array_equivalent(left, right, strict_nan=False):

if not strict_nan:
# pd.isnull considers NaN and None to be equivalent.
return lib.array_equivalent_object(
_ensure_object(left.ravel()), _ensure_object(right.ravel()))
return lib.array_equivalent_object(_ensure_object(left.ravel()),
_ensure_object(right.ravel()))

for left_value, right_value in zip(left, right):
if left_value is tslib.NaT and right_value is not tslib.NaT:
Expand Down
17 changes: 8 additions & 9 deletions pandas/io/tests/json/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ def test_frame_non_unique_index(self):
assert_frame_equal(df, read_json(df.to_json(orient='split'),
orient='split'))
unser = read_json(df.to_json(orient='records'), orient='records')
self.assertTrue(df.columns.equals(unser.columns))
tm.assert_numpy_array_equal(df.values, unser.values)
self.assert_index_equal(df.columns, unser.columns)
np.testing.assert_equal(df.values, unser.values)
unser = read_json(df.to_json(orient='values'), orient='values')
tm.assert_numpy_array_equal(df.values, unser.values)

Expand Down Expand Up @@ -183,7 +183,8 @@ def _check_orient(df, orient, dtype=None, numpy=False,
# index is not captured in this orientation
assert_almost_equal(df.values, unser.values,
check_dtype=check_numpy_dtype)
self.assertTrue(df.columns.equals(unser.columns))
self.assert_index_equal(df.columns, unser.columns,
exact=check_column_type)
elif orient == "values":
# index and cols are not captured in this orientation
if numpy is True and df.shape == (0, 0):
Expand Down Expand Up @@ -302,12 +303,10 @@ def _check_all_orients(df, dtype=None, convert_axes=True,

# mixed data
index = pd.Index(['a', 'b', 'c', 'd', 'e'])
data = {
'A': [0., 1., 2., 3., 4.],
'B': [0., 1., 0., 1., 0.],
'C': ['foo1', 'foo2', 'foo3', 'foo4', 'foo5'],
'D': [True, False, True, False, True]
}
data = {'A': [0., 1., 2., 3., 4.],
'B': [0., 1., 0., 1., 0.],
'C': ['foo1', 'foo2', 'foo3', 'foo4', 'foo5'],
'D': [True, False, True, False, True]}
df = DataFrame(data=data, index=index)
_check_orient(df, "split", check_dtype=False)
_check_orient(df, "records", check_dtype=False)
Expand Down
86 changes: 45 additions & 41 deletions pandas/io/tests/json/test_ujson.py
Original file line number Diff line number Diff line change
Expand Up @@ -1201,28 +1201,28 @@ def testDataFrame(self):
# column indexed
outp = DataFrame(ujson.decode(ujson.encode(df)))
self.assertTrue((df == outp).values.all())
tm.assert_numpy_array_equal(df.columns, outp.columns)
tm.assert_numpy_array_equal(df.index, outp.index)
tm.assert_index_equal(df.columns, outp.columns)
tm.assert_index_equal(df.index, outp.index)

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

outp = DataFrame(ujson.decode(ujson.encode(df, orient="records")))
outp.index = df.index
self.assertTrue((df == outp).values.all())
tm.assert_numpy_array_equal(df.columns, outp.columns)
tm.assert_index_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())
tm.assert_numpy_array_equal(df.transpose().columns, outp.columns)
tm.assert_numpy_array_equal(df.transpose().index, outp.index)
tm.assert_index_equal(df.transpose().columns, outp.columns)
tm.assert_index_equal(df.transpose().index, outp.index)

def testDataFrameNumpy(self):
df = DataFrame([[1, 2, 3], [4, 5, 6]], index=[
Expand All @@ -1231,21 +1231,21 @@ def testDataFrameNumpy(self):
# column indexed
outp = DataFrame(ujson.decode(ujson.encode(df), numpy=True))
self.assertTrue((df == outp).values.all())
tm.assert_numpy_array_equal(df.columns, outp.columns)
tm.assert_numpy_array_equal(df.index, outp.index)
tm.assert_index_equal(df.columns, outp.columns)
tm.assert_index_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())
tm.assert_numpy_array_equal(df.columns, outp.columns)
tm.assert_numpy_array_equal(df.index, outp.index)
tm.assert_index_equal(df.columns, outp.columns)
tm.assert_index_equal(df.index, outp.index)

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

def testDataFrameNested(self):
df = DataFrame([[1, 2, 3], [4, 5, 6]], index=[
Expand Down Expand Up @@ -1285,20 +1285,20 @@ def testDataFrameNumpyLabelled(self):
outp = DataFrame(*ujson.decode(ujson.encode(df),
numpy=True, labelled=True))
self.assertTrue((df.T == outp).values.all())
tm.assert_numpy_array_equal(df.T.columns, outp.columns)
tm.assert_numpy_array_equal(df.T.index, outp.index)
tm.assert_index_equal(df.T.columns, outp.columns)
tm.assert_index_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())
tm.assert_numpy_array_equal(df.columns, outp.columns)
tm.assert_index_equal(df.columns, outp.columns)

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

def testSeries(self):
s = Series([10, 20, 30, 40, 50, 60], name="series",
Expand Down Expand Up @@ -1378,42 +1378,46 @@ def testIndex(self):
i = Index([23, 45, 18, 98, 43, 11], name="index")

# column indexed
outp = Index(ujson.decode(ujson.encode(i)))
self.assertTrue(i.equals(outp))
outp = Index(ujson.decode(ujson.encode(i)), name='index')
tm.assert_index_equal(i, outp)

outp = Index(ujson.decode(ujson.encode(i), numpy=True))
self.assertTrue(i.equals(outp))
outp = Index(ujson.decode(ujson.encode(i), numpy=True), name='index')
tm.assert_index_equal(i, outp)

dec = _clean_dict(ujson.decode(ujson.encode(i, orient="split")))
outp = Index(**dec)
self.assertTrue(i.equals(outp))
tm.assert_index_equal(i, outp)
self.assertTrue(i.name == outp.name)

dec = _clean_dict(ujson.decode(ujson.encode(i, orient="split"),
numpy=True))
outp = Index(**dec)
self.assertTrue(i.equals(outp))
tm.assert_index_equal(i, outp)
self.assertTrue(i.name == outp.name)

outp = Index(ujson.decode(ujson.encode(i, orient="values")))
self.assertTrue(i.equals(outp))
outp = Index(ujson.decode(ujson.encode(i, orient="values")),
name='index')
tm.assert_index_equal(i, outp)

outp = Index(ujson.decode(ujson.encode(
i, orient="values"), numpy=True))
self.assertTrue(i.equals(outp))
outp = Index(ujson.decode(ujson.encode(i, orient="values"),
numpy=True), name='index')
tm.assert_index_equal(i, outp)

outp = Index(ujson.decode(ujson.encode(i, orient="records")))
self.assertTrue(i.equals(outp))
outp = Index(ujson.decode(ujson.encode(i, orient="records")),
name='index')
tm.assert_index_equal(i, outp)

outp = Index(ujson.decode(ujson.encode(
i, orient="records"), numpy=True))
self.assertTrue(i.equals(outp))
outp = Index(ujson.decode(ujson.encode(i, orient="records"),
numpy=True), name='index')
tm.assert_index_equal(i, outp)

outp = Index(ujson.decode(ujson.encode(i, orient="index")))
self.assertTrue(i.equals(outp))
outp = Index(ujson.decode(ujson.encode(i, orient="index")),
name='index')
tm.assert_index_equal(i, outp)

outp = Index(ujson.decode(ujson.encode(i, orient="index"), numpy=True))
self.assertTrue(i.equals(outp))
outp = Index(ujson.decode(ujson.encode(i, orient="index"),
numpy=True), name='index')
tm.assert_index_equal(i, outp)

def test_datetimeindex(self):
from pandas.tseries.index import date_range
Expand All @@ -1423,7 +1427,7 @@ def test_datetimeindex(self):
encoded = ujson.encode(rng, date_unit='ns')
decoded = DatetimeIndex(np.array(ujson.decode(encoded)))

self.assertTrue(rng.equals(decoded))
tm.assert_index_equal(rng, decoded)

ts = Series(np.random.randn(len(rng)), index=rng)
decoded = Series(ujson.decode(ujson.encode(ts, date_unit='ns')))
Expand Down
32 changes: 16 additions & 16 deletions pandas/io/tests/parser/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ def test_comment(self):
1,2.,4.#hello world
5.,NaN,10.0
"""
expected = [[1., 2., 4.],
[5., np.nan, 10.]]
expected = np.array([[1., 2., 4.],
[5., np.nan, 10.]])
df = self.read_csv(StringIO(data), comment='#')
tm.assert_almost_equal(df.values, expected)
tm.assert_numpy_array_equal(df.values, expected)

df = self.read_table(StringIO(data), sep=',', comment='#',
na_values=['NaN'])
tm.assert_almost_equal(df.values, expected)
tm.assert_numpy_array_equal(df.values, expected)

def test_line_comment(self):
data = """# empty
Expand All @@ -35,10 +35,10 @@ def test_line_comment(self):
#ignore this line
5.,NaN,10.0
"""
expected = [[1., 2., 4.],
[5., np.nan, 10.]]
expected = np.array([[1., 2., 4.],
[5., np.nan, 10.]])
df = self.read_csv(StringIO(data), comment='#')
tm.assert_almost_equal(df.values, expected)
tm.assert_numpy_array_equal(df.values, expected)

# check with delim_whitespace=True
df = self.read_csv(StringIO(data.replace(',', ' ')), comment='#',
Expand All @@ -48,11 +48,11 @@ def test_line_comment(self):
# custom line terminator is not supported
# with the Python parser yet
if self.engine == 'c':
expected = [[1., 2., 4.],
[5., np.nan, 10.]]
expected = np.array([[1., 2., 4.],
[5., np.nan, 10.]])
df = self.read_csv(StringIO(data.replace('\n', '*')),
comment='#', lineterminator='*')
tm.assert_almost_equal(df.values, expected)
tm.assert_numpy_array_equal(df.values, expected)

def test_comment_skiprows(self):
data = """# empty
Expand All @@ -64,9 +64,9 @@ def test_comment_skiprows(self):
5.,NaN,10.0
"""
# this should ignore the first four lines (including comments)
expected = [[1., 2., 4.], [5., np.nan, 10.]]
expected = np.array([[1., 2., 4.], [5., np.nan, 10.]])
df = self.read_csv(StringIO(data), comment='#', skiprows=4)
tm.assert_almost_equal(df.values, expected)
tm.assert_numpy_array_equal(df.values, expected)

def test_comment_header(self):
data = """# empty
Expand All @@ -77,9 +77,9 @@ def test_comment_header(self):
5.,NaN,10.0
"""
# header should begin at the second non-comment line
expected = [[1., 2., 4.], [5., np.nan, 10.]]
expected = np.array([[1., 2., 4.], [5., np.nan, 10.]])
df = self.read_csv(StringIO(data), comment='#', header=1)
tm.assert_almost_equal(df.values, expected)
tm.assert_numpy_array_equal(df.values, expected)

def test_comment_skiprows_header(self):
data = """# empty
Expand All @@ -94,9 +94,9 @@ def test_comment_skiprows_header(self):
# skiprows should skip the first 4 lines (including comments), while
# header should start from the second non-commented line starting
# with line 5
expected = [[1., 2., 4.], [5., np.nan, 10.]]
expected = np.array([[1., 2., 4.], [5., np.nan, 10.]])
df = self.read_csv(StringIO(data), comment='#', skiprows=4, header=1)
tm.assert_almost_equal(df.values, expected)
tm.assert_numpy_array_equal(df.values, expected)

def test_custom_comment_char(self):
data = "a,b,c\n1,2,3#ignore this!\n4,5,6#ignorethistoo"
Expand Down
Loading