Skip to content

BUG: reindex_like after shape comparison #15496

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 5 commits 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
27 changes: 14 additions & 13 deletions pandas/tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,17 @@ def _assert_equal(self, x, y, **kwargs):
def _assert_not_equal(self, a, b, **kwargs):
self.assertRaises(AssertionError, assert_frame_equal, a, b, **kwargs)
self.assertRaises(AssertionError, assert_frame_equal, b, a, **kwargs)

def test_equal_with_different_row_order(self):
self._assert_equal(pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]},
index=['a', 'b', 'c']),
pd.DataFrame({'A': [3, 2, 1], 'B': [6, 5, 4]},
index=['c', 'b', 'a']),
check_like=True)

def test_not_equal_with_different_shape(self):
self._assert_not_equal(pd.DataFrame({'A': [1, 2, 3]}),
pd.DataFrame({'A': [1, 2, 3, 4]}))

def test_index_dtype(self):
df1 = DataFrame.from_records(
Expand Down Expand Up @@ -621,19 +632,9 @@ def test_frame_equal_message(self):

expected = """DataFrame are different

DataFrame shape \\(number of rows\\) are different
\\[left\\]: 3, RangeIndex\\(start=0, stop=3, step=1\\)
\\[right\\]: 4, RangeIndex\\(start=0, stop=4, step=1\\)"""

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

expected = """DataFrame are different

DataFrame shape \\(number of columns\\) are different
\\[left\\]: 2, Index\\(\\[u?'A', u?'B'\\], dtype='object'\\)
\\[right\\]: 1, Index\\(\\[u?'A'\\], dtype='object'\\)"""
DataFrame shape mismatch
\\[left\\]: \\(3, 2\\)
\\[right\\]: \\(3, 1\\)"""

with assertRaisesRegexp(AssertionError, expected):
assert_frame_equal(pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}),
Expand Down
25 changes: 8 additions & 17 deletions pandas/util/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,7 @@ def assert_frame_equal(left, right, check_dtype=True,
check_categorical : bool, default True
Whether to compare internal Categorical exactly.
check_like : bool, default False
If true, then reindex_like operands
If true, ignore the order of rows & columns
obj : str, default 'DataFrame'
Specify object name being compared, internally used to show appropriate
assertion message
Expand All @@ -1270,25 +1270,16 @@ def assert_frame_equal(left, right, check_dtype=True,
assertIsInstance(left, type(right))
# assert_class_equal(left, right, obj=obj)

# shape comparison
if left.shape != right.shape:
raise_assert_detail(obj,
'DataFrame shape mismatch',
'({0}, {1})'.format(*left.shape),
'({0}, {1})'.format(*right.shape))

if check_like:
left, right = left.reindex_like(right), right

# shape comparison (row)
if left.shape[0] != right.shape[0]:
raise_assert_detail(obj,
'DataFrame shape (number of rows) are different',
'{0}, {1}'.format(left.shape[0], left.index),
'{0}, {1}'.format(right.shape[0], right.index))
# shape comparison (columns)
if left.shape[1] != right.shape[1]:
raise_assert_detail(obj,
'DataFrame shape (number of columns) '
'are different',
'{0}, {1}'.format(left.shape[1],
left.columns),
'{0}, {1}'.format(right.shape[1],
right.columns))

# index comparison
assert_index_equal(left.index, right.index, exact=check_index_type,
check_names=check_names,
Expand Down