Skip to content

Commit 029e098

Browse files
authored
Incorrect identically-labeled DataFrame objects Exception comes with df.compare method #50083 (#50287)
* added back in changes * updated whatsnew note * reworded the whatsnew comment
1 parent b233faa commit 029e098

File tree

6 files changed

+29
-6
lines changed

6 files changed

+29
-6
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ Other enhancements
9999
- Added :meth:`Index.infer_objects` analogous to :meth:`Series.infer_objects` (:issue:`50034`)
100100
- Added ``copy`` parameter to :meth:`Series.infer_objects` and :meth:`DataFrame.infer_objects`, passing ``False`` will avoid making copies for series or columns that are already non-object or where no better dtype can be inferred (:issue:`50096`)
101101
- :meth:`DataFrame.plot.hist` now recognizes ``xlabel`` and ``ylabel`` arguments (:issue:`49793`)
102+
- Improved error message when trying to align :class:`DataFrame` objects (for example, in :func:`DataFrame.compare`) to clarify that "identically labelled" refers to both index and columns (:issue:`50083`)
102103
-
103104

104105
.. ---------------------------------------------------------------------------

pandas/core/ops/__init__.py

+2-1
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

+8-2
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,20 @@ 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 = (
174+
r"Can only compare identically-labeled \(both index and columns\) DataFrame "
175+
"objects"
176+
)
174177
with pytest.raises(ValueError, match=msg):
175178
df1 = pd.DataFrame([1, 2, 3], index=["a", "b", "c"])
176179
df2 = pd.DataFrame([1, 2, 3], index=["a", "b", "d"])
177180
df1.compare(df2)
178181

179182
# test DataFrames with different shapes
180-
msg = "Can only compare identically-labeled DataFrame objects"
183+
msg = (
184+
r"Can only compare identically-labeled \(both index and columns\) DataFrame "
185+
"objects"
186+
)
181187
with pytest.raises(ValueError, match=msg):
182188
df1 = pd.DataFrame(np.ones((3, 3)))
183189
df2 = pd.DataFrame(np.zeros((2, 1)))

pandas/tests/frame/test_arithmetic.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,10 @@ 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 = (
1540+
r"Can only compare identically-labeled \(both index and columns\) "
1541+
"DataFrame objects"
1542+
)
15401543
with pytest.raises(ValueError, match=msg):
15411544
func(simple_frame, simple_frame[:2])
15421545

pandas/tests/frame/test_nonunique_indexes.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,10 @@ def test_dup_columns_comparisons(self):
201201
df2 = DataFrame([[0, 1], [2, 4], [2, np.nan], [4, 5]], columns=["A", "A"])
202202

203203
# not-comparing like-labelled
204-
msg = "Can only compare identically-labeled DataFrame objects"
204+
msg = (
205+
r"Can only compare identically-labeled \(both index and columns\) "
206+
"DataFrame objects"
207+
)
205208
with pytest.raises(ValueError, match=msg):
206209
df1 == df2
207210

pandas/tests/series/test_arithmetic.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -643,10 +643,19 @@ def test_ne(self):
643643
)
644644
def test_comp_ops_df_compat(self, left, right, frame_or_series):
645645
# GH 1134
646-
msg = f"Can only compare identically-labeled {frame_or_series.__name__} objects"
646+
# GH 50083 to clarify that index and columns must be identically labeled
647647
if frame_or_series is not Series:
648+
msg = (
649+
rf"Can only compare identically-labeled \(both index and columns\) "
650+
f"{frame_or_series.__name__} objects"
651+
)
648652
left = left.to_frame()
649653
right = right.to_frame()
654+
else:
655+
msg = (
656+
f"Can only compare identically-labeled {frame_or_series.__name__} "
657+
f"objects"
658+
)
650659

651660
with pytest.raises(ValueError, match=msg):
652661
left == right

0 commit comments

Comments
 (0)