Skip to content

Commit d7d26be

Browse files
ajwoodjreback
authored andcommitted
TST: change assertion messages in assert_frame_equal (#27023) (#27068)
1 parent 110c02f commit d7d26be

File tree

2 files changed

+52
-32
lines changed

2 files changed

+52
-32
lines changed

pandas/tests/util/test_assert_frame_equal.py

+49-29
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55

66

77
@pytest.fixture(params=[True, False])
8-
def by_blocks(request):
8+
def by_blocks_fixture(request):
9+
return request.param
10+
11+
12+
@pytest.fixture(params=['DataFrame', 'Series'])
13+
def obj_fixture(request):
914
return request.param
1015

1116

@@ -70,29 +75,35 @@ def _assert_not_frame_equal_both(a, b, **kwargs):
7075

7176

7277
@pytest.mark.parametrize("check_like", [True, False])
73-
def test_frame_equal_row_order_mismatch(check_like):
78+
def test_frame_equal_row_order_mismatch(check_like, obj_fixture):
7479
df1 = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]},
7580
index=["a", "b", "c"])
7681
df2 = DataFrame({"A": [3, 2, 1], "B": [6, 5, 4]},
7782
index=["c", "b", "a"])
7883

7984
if not check_like: # Do not ignore row-column orderings.
80-
msg = "DataFrame.index are different"
85+
msg = "{obj}.index are different".format(obj=obj_fixture)
8186
with pytest.raises(AssertionError, match=msg):
82-
assert_frame_equal(df1, df2, check_like=check_like)
87+
assert_frame_equal(df1,
88+
df2,
89+
check_like=check_like,
90+
obj=obj_fixture)
8391
else:
84-
_assert_frame_equal_both(df1, df2, check_like=check_like)
92+
_assert_frame_equal_both(df1,
93+
df2,
94+
check_like=check_like,
95+
obj=obj_fixture)
8596

8697

8798
@pytest.mark.parametrize("df1,df2", [
8899
(DataFrame({"A": [1, 2, 3]}), DataFrame({"A": [1, 2, 3, 4]})),
89100
(DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}), DataFrame({"A": [1, 2, 3]})),
90101
])
91-
def test_frame_equal_shape_mismatch(df1, df2):
92-
msg = "DataFrame are different"
102+
def test_frame_equal_shape_mismatch(df1, df2, obj_fixture):
103+
msg = "{obj} are different".format(obj=obj_fixture)
93104

94105
with pytest.raises(AssertionError, match=msg):
95-
assert_frame_equal(df1, df2)
106+
assert_frame_equal(df1, df2, obj=obj_fixture)
96107

97108

98109
@pytest.mark.parametrize("df1,df2,msg", [
@@ -136,72 +147,81 @@ def test_empty_dtypes(check_dtype):
136147
assert_frame_equal(df1, df2, **kwargs)
137148

138149

139-
def test_frame_equal_index_mismatch():
140-
msg = """DataFrame\\.index are different
150+
def test_frame_equal_index_mismatch(obj_fixture):
151+
msg = """{obj}\\.index are different
141152
142-
DataFrame\\.index values are different \\(33\\.33333 %\\)
153+
{obj}\\.index values are different \\(33\\.33333 %\\)
143154
\\[left\\]: Index\\(\\['a', 'b', 'c'\\], dtype='object'\\)
144-
\\[right\\]: Index\\(\\['a', 'b', 'd'\\], dtype='object'\\)"""
155+
\\[right\\]: Index\\(\\['a', 'b', 'd'\\], dtype='object'\\)""".format(
156+
obj=obj_fixture)
145157

146158
df1 = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]},
147159
index=["a", "b", "c"])
148160
df2 = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]},
149161
index=["a", "b", "d"])
150162

151163
with pytest.raises(AssertionError, match=msg):
152-
assert_frame_equal(df1, df2)
164+
assert_frame_equal(df1, df2, obj=obj_fixture)
153165

154166

155-
def test_frame_equal_columns_mismatch():
156-
msg = """DataFrame\\.columns are different
167+
def test_frame_equal_columns_mismatch(obj_fixture):
168+
msg = """{obj}\\.columns are different
157169
158-
DataFrame\\.columns values are different \\(50\\.0 %\\)
170+
{obj}\\.columns values are different \\(50\\.0 %\\)
159171
\\[left\\]: Index\\(\\['A', 'B'\\], dtype='object'\\)
160-
\\[right\\]: Index\\(\\['A', 'b'\\], dtype='object'\\)"""
172+
\\[right\\]: Index\\(\\['A', 'b'\\], dtype='object'\\)""".format(
173+
obj=obj_fixture)
161174

162175
df1 = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]},
163176
index=["a", "b", "c"])
164177
df2 = DataFrame({"A": [1, 2, 3], "b": [4, 5, 6]},
165178
index=["a", "b", "c"])
166179

167180
with pytest.raises(AssertionError, match=msg):
168-
assert_frame_equal(df1, df2)
181+
assert_frame_equal(df1, df2, obj=obj_fixture)
169182

170183

171-
def test_frame_equal_block_mismatch(by_blocks):
172-
msg = """DataFrame\\.iloc\\[:, 1\\] are different
184+
def test_frame_equal_block_mismatch(by_blocks_fixture, obj_fixture):
185+
msg = """{obj}\\.iloc\\[:, 1\\] are different
173186
174-
DataFrame\\.iloc\\[:, 1\\] values are different \\(33\\.33333 %\\)
187+
{obj}\\.iloc\\[:, 1\\] values are different \\(33\\.33333 %\\)
175188
\\[left\\]: \\[4, 5, 6\\]
176-
\\[right\\]: \\[4, 5, 7\\]"""
189+
\\[right\\]: \\[4, 5, 7\\]""".format(obj=obj_fixture)
177190

178191
df1 = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
179192
df2 = DataFrame({"A": [1, 2, 3], "B": [4, 5, 7]})
180193

181194
with pytest.raises(AssertionError, match=msg):
182-
assert_frame_equal(df1, df2, by_blocks=by_blocks)
195+
assert_frame_equal(df1,
196+
df2,
197+
by_blocks=by_blocks_fixture,
198+
obj=obj_fixture)
183199

184200

185201
@pytest.mark.parametrize("df1,df2,msg", [
186202
(DataFrame({"A": ["á", "à", "ä"], "E": ["é", "è", "ë"]}),
187203
DataFrame({"A": ["á", "à", "ä"], "E": ["é", "è", "e̊"]}),
188-
"""DataFrame\\.iloc\\[:, 1\\] are different
204+
"""{obj}\\.iloc\\[:, 1\\] are different
189205
190-
DataFrame\\.iloc\\[:, 1\\] values are different \\(33\\.33333 %\\)
206+
{obj}\\.iloc\\[:, 1\\] values are different \\(33\\.33333 %\\)
191207
\\[left\\]: \\[é, è, ë\\]
192208
\\[right\\]: \\[é, è, e̊\\]"""),
193209
(DataFrame({"A": ["á", "à", "ä"], "E": ["é", "è", "ë"]}),
194210
DataFrame({"A": ["a", "a", "a"], "E": ["e", "e", "e"]}),
195-
"""DataFrame\\.iloc\\[:, 0\\] are different
211+
"""{obj}\\.iloc\\[:, 0\\] are different
196212
197-
DataFrame\\.iloc\\[:, 0\\] values are different \\(100\\.0 %\\)
213+
{obj}\\.iloc\\[:, 0\\] values are different \\(100\\.0 %\\)
198214
\\[left\\]: \\[á, à, ä\\]
199215
\\[right\\]: \\[a, a, a\\]"""),
200216
])
201-
def test_frame_equal_unicode(df1, df2, msg, by_blocks):
217+
def test_frame_equal_unicode(df1, df2, msg, by_blocks_fixture, obj_fixture):
202218
# see gh-20503
203219
#
204220
# Test ensures that `assert_frame_equals` raises the right exception
205221
# when comparing DataFrames containing differing unicode objects.
222+
msg = msg.format(obj=obj_fixture)
206223
with pytest.raises(AssertionError, match=msg):
207-
assert_frame_equal(df1, df2, by_blocks=by_blocks)
224+
assert_frame_equal(df1,
225+
df2,
226+
by_blocks=by_blocks_fixture,
227+
obj=obj_fixture)

pandas/util/testing.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1218,7 +1218,7 @@ def assert_frame_equal(left, right, check_dtype=True,
12181218
# shape comparison
12191219
if left.shape != right.shape:
12201220
raise_assert_detail(obj,
1221-
'DataFrame shape mismatch',
1221+
'{obj} shape mismatch'.format(obj=obj),
12221222
'{shape!r}'.format(shape=left.shape),
12231223
'{shape!r}'.format(shape=right.shape))
12241224

@@ -1249,7 +1249,7 @@ def assert_frame_equal(left, right, check_dtype=True,
12491249
assert dtype in lblocks
12501250
assert dtype in rblocks
12511251
assert_frame_equal(lblocks[dtype], rblocks[dtype],
1252-
check_dtype=check_dtype, obj='DataFrame.blocks')
1252+
check_dtype=check_dtype, obj=obj)
12531253

12541254
# compare by columns
12551255
else:
@@ -1264,7 +1264,7 @@ def assert_frame_equal(left, right, check_dtype=True,
12641264
check_exact=check_exact, check_names=check_names,
12651265
check_datetimelike_compat=check_datetimelike_compat,
12661266
check_categorical=check_categorical,
1267-
obj='DataFrame.iloc[:, {idx}]'.format(idx=i))
1267+
obj='{obj}.iloc[:, {idx}]'.format(obj=obj, idx=i))
12681268

12691269

12701270
def assert_equal(left, right, **kwargs):

0 commit comments

Comments
 (0)