Skip to content

Commit cfc40bb

Browse files
committed
fixed Incorrect Exception comes with df.compare method #50083
1 parent e1dd15b commit cfc40bb

File tree

5 files changed

+16
-7
lines changed

5 files changed

+16
-7
lines changed

pandas/core/ops/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ def to_series(right):
313313
left, right = left.align(right, join="outer", level=level, copy=False)
314314
else:
315315
raise ValueError(
316-
"Can only compare identically-labeled DataFrame objects"
316+
"Can only compare identically-labeled (both index and columns)\
317+
DataFrame objects"
317318
)
318319
elif isinstance(right, ABCSeries):
319320
# axis=1 is default for DataFrame-with-Series op

pandas/tests/frame/methods/test_compare.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,16 @@ def test_compare_multi_index(align_axis):
170170

171171
def test_compare_unaligned_objects():
172172
# test DataFrames with different indices
173-
msg = "Can only compare identically-labeled DataFrame objects"
173+
msg = "Can only compare identically-labeled (both index and columns) DataFrame\
174+
objects"
174175
with pytest.raises(ValueError, match=msg):
175176
df1 = pd.DataFrame([1, 2, 3], index=["a", "b", "c"])
176177
df2 = pd.DataFrame([1, 2, 3], index=["a", "b", "d"])
177178
df1.compare(df2)
178179

179180
# test DataFrames with different shapes
180-
msg = "Can only compare identically-labeled DataFrame objects"
181+
msg = "Can only compare identically-labeled (both index and columns) DataFrame\
182+
objects"
181183
with pytest.raises(ValueError, match=msg):
182184
df1 = pd.DataFrame(np.ones((3, 3)))
183185
df2 = pd.DataFrame(np.zeros((2, 1)))

pandas/tests/frame/test_arithmetic.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,8 @@ def test_comparisons(self, simple_frame, float_frame, func):
15361536
result3 = func(float_frame, 0)
15371537
tm.assert_numpy_array_equal(result3.values, func(float_frame.values, 0))
15381538

1539-
msg = "Can only compare identically-labeled DataFrame"
1539+
msg = "Can only compare identically-labeled (both index and columns)\
1540+
DataFrame objects"
15401541
with pytest.raises(ValueError, match=msg):
15411542
func(simple_frame, simple_frame[:2])
15421543

pandas/tests/frame/test_nonunique_indexes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ def test_dup_columns_comparisons(self):
203203
df2 = DataFrame([[0, 1], [2, 4], [2, np.nan], [4, 5]], columns=["A", "A"])
204204

205205
# not-comparing like-labelled
206-
msg = "Can only compare identically-labeled DataFrame objects"
206+
msg = "Can only compare identically-labeled (both index and columns) \
207+
DataFrame objects"
207208
with pytest.raises(ValueError, match=msg):
208209
df1 == df2
209210

pandas/tests/series/test_arithmetic.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -624,11 +624,15 @@ def test_ne(self):
624624
)
625625
def test_comp_ops_df_compat(self, left, right, frame_or_series):
626626
# GH 1134
627-
msg = f"Can only compare identically-labeled {frame_or_series.__name__} objects"
627+
# GH 50083 to clarify that index and columns must be identically labeled
628628
if frame_or_series is not Series:
629+
msg = f"Can only compare identically-labeled (both index and columns)\
630+
{frame_or_series.__name__} objects"
629631
left = left.to_frame()
630632
right = right.to_frame()
631-
633+
else:
634+
msg = f"Can only compare identically-labeled {frame_or_series.__name__}\
635+
objects"
632636
with pytest.raises(ValueError, match=msg):
633637
left == right
634638
with pytest.raises(ValueError, match=msg):

0 commit comments

Comments
 (0)