Skip to content

Commit 55eccd9

Browse files
jojomdtjreback
authored andcommitted
BUG: reindex_like after shape comparison
in assert_frame_equal, if check_like, the former code reindex_like before shape comparison. for example: if left.shape=(2,2), right.shpae=(2.0), after reindex_like, left.shape=(2,0),right.shape=(2,0),then the shape comparison will not find out that the two dataframes are different. For that, the assert_frame_equal will not raise assertion errors. But in fact it should raise. Author: jojomdt <[email protected]> Closes pandas-dev#15496 from jojomdt/master and squashes the following commits: 7b3437b [jojomdt] fix test_frame_equal_message error 0340b5c [jojomdt] change check_like description c03e0af [jojomdt] add test for TestAssertFrameEqual 470dbaa [jojomdt] combine row and column shape comparison ce7bd74 [jojomdt] reindex_like after shape comparison
1 parent 25dcff5 commit 55eccd9

File tree

2 files changed

+25
-32
lines changed

2 files changed

+25
-32
lines changed

pandas/tests/test_testing.py

+17-15
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
RNGContext)
1414
from pandas.compat import is_platform_windows
1515

16-
# let's get meta.
17-
1816

1917
class TestAssertAlmostEqual(tm.TestCase):
2018

@@ -594,6 +592,20 @@ def _assert_not_equal(self, a, b, **kwargs):
594592
self.assertRaises(AssertionError, assert_frame_equal, a, b, **kwargs)
595593
self.assertRaises(AssertionError, assert_frame_equal, b, a, **kwargs)
596594

595+
def test_equal_with_different_row_order(self):
596+
# check_like=True ignores row-column orderings
597+
df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]},
598+
index=['a', 'b', 'c'])
599+
df2 = pd.DataFrame({'A': [3, 2, 1], 'B': [6, 5, 4]},
600+
index=['c', 'b', 'a'])
601+
602+
self._assert_equal(df1, df2, check_like=True)
603+
self._assert_not_equal(df1, df2)
604+
605+
def test_not_equal_with_different_shape(self):
606+
self._assert_not_equal(pd.DataFrame({'A': [1, 2, 3]}),
607+
pd.DataFrame({'A': [1, 2, 3, 4]}))
608+
597609
def test_index_dtype(self):
598610
df1 = DataFrame.from_records(
599611
{'a': [1, 2], 'c': ['l1', 'l2']}, index=['a'])
@@ -621,19 +633,9 @@ def test_frame_equal_message(self):
621633

622634
expected = """DataFrame are different
623635
624-
DataFrame shape \\(number of rows\\) are different
625-
\\[left\\]: 3, RangeIndex\\(start=0, stop=3, step=1\\)
626-
\\[right\\]: 4, RangeIndex\\(start=0, stop=4, step=1\\)"""
627-
628-
with assertRaisesRegexp(AssertionError, expected):
629-
assert_frame_equal(pd.DataFrame({'A': [1, 2, 3]}),
630-
pd.DataFrame({'A': [1, 2, 3, 4]}))
631-
632-
expected = """DataFrame are different
633-
634-
DataFrame shape \\(number of columns\\) are different
635-
\\[left\\]: 2, Index\\(\\[u?'A', u?'B'\\], dtype='object'\\)
636-
\\[right\\]: 1, Index\\(\\[u?'A'\\], dtype='object'\\)"""
636+
DataFrame shape mismatch
637+
\\[left\\]: \\(3, 2\\)
638+
\\[right\\]: \\(3, 1\\)"""
637639

638640
with assertRaisesRegexp(AssertionError, expected):
639641
assert_frame_equal(pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}),

pandas/util/testing.py

+8-17
Original file line numberDiff line numberDiff line change
@@ -1254,7 +1254,7 @@ def assert_frame_equal(left, right, check_dtype=True,
12541254
check_categorical : bool, default True
12551255
Whether to compare internal Categorical exactly.
12561256
check_like : bool, default False
1257-
If true, then reindex_like operands
1257+
If true, ignore the order of rows & columns
12581258
obj : str, default 'DataFrame'
12591259
Specify object name being compared, internally used to show appropriate
12601260
assertion message
@@ -1270,25 +1270,16 @@ def assert_frame_equal(left, right, check_dtype=True,
12701270
assertIsInstance(left, type(right))
12711271
# assert_class_equal(left, right, obj=obj)
12721272

1273+
# shape comparison
1274+
if left.shape != right.shape:
1275+
raise_assert_detail(obj,
1276+
'DataFrame shape mismatch',
1277+
'({0}, {1})'.format(*left.shape),
1278+
'({0}, {1})'.format(*right.shape))
1279+
12731280
if check_like:
12741281
left, right = left.reindex_like(right), right
12751282

1276-
# shape comparison (row)
1277-
if left.shape[0] != right.shape[0]:
1278-
raise_assert_detail(obj,
1279-
'DataFrame shape (number of rows) are different',
1280-
'{0}, {1}'.format(left.shape[0], left.index),
1281-
'{0}, {1}'.format(right.shape[0], right.index))
1282-
# shape comparison (columns)
1283-
if left.shape[1] != right.shape[1]:
1284-
raise_assert_detail(obj,
1285-
'DataFrame shape (number of columns) '
1286-
'are different',
1287-
'{0}, {1}'.format(left.shape[1],
1288-
left.columns),
1289-
'{0}, {1}'.format(right.shape[1],
1290-
right.columns))
1291-
12921283
# index comparison
12931284
assert_index_equal(left.index, right.index, exact=check_index_type,
12941285
check_names=check_names,

0 commit comments

Comments
 (0)