Skip to content

Commit 3005908

Browse files
Reksbriljreback
authored andcommitted
BUG: resolved problem with DataFrame.equals() (#28839) (#29657)
1 parent 07e6b9d commit 3005908

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ Reshaping
452452
- Fix to ensure all int dtypes can be used in :func:`merge_asof` when using a tolerance value. Previously every non-int64 type would raise an erroneous ``MergeError`` (:issue:`28870`).
453453
- Better error message in :func:`get_dummies` when `columns` isn't a list-like value (:issue:`28383`)
454454
- Bug :meth:`Series.pct_change` where supplying an anchored frequency would throw a ValueError (:issue:`28664`)
455+
- Bug where :meth:`DataFrame.equals` returned True incorrectly in some cases when two DataFrames had the same columns in different orders (:issue:`28839`)
455456

456457
Sparse
457458
^^^^^^

pandas/core/internals/managers.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1394,12 +1394,12 @@ def equals(self, other):
13941394
if len(self.blocks) != len(other.blocks):
13951395
return False
13961396

1397-
# canonicalize block order, using a tuple combining the type
1398-
# name and then mgr_locs because there might be unconsolidated
1397+
# canonicalize block order, using a tuple combining the mgr_locs
1398+
# then type name because there might be unconsolidated
13991399
# blocks (say, Categorical) which can only be distinguished by
14001400
# the iteration order
14011401
def canonicalize(block):
1402-
return (block.dtype.name, block.mgr_locs.as_array.tolist())
1402+
return (block.mgr_locs.as_array.tolist(), block.dtype.name)
14031403

14041404
self_blocks = sorted(self.blocks, key=canonicalize)
14051405
other_blocks = sorted(other.blocks, key=canonicalize)

pandas/tests/internals/test_internals.py

+7
Original file line numberDiff line numberDiff line change
@@ -1297,3 +1297,10 @@ def test_make_block_no_pandas_array():
12971297
result = make_block(arr.to_numpy(), slice(len(arr)), dtype=arr.dtype)
12981298
assert result.is_integer is True
12991299
assert result.is_extension is False
1300+
1301+
1302+
def test_dataframe_not_equal():
1303+
# see GH28839
1304+
df1 = pd.DataFrame({"a": [1, 2], "b": ["s", "d"]})
1305+
df2 = pd.DataFrame({"a": ["s", "d"], "b": [1, 2]})
1306+
assert df1.equals(df2) is False

0 commit comments

Comments
 (0)