Skip to content

Commit 3e439b4

Browse files
BUG: Fix Index.equals with string vs object dtype (#61099)
1 parent 4d80505 commit 3e439b4

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

pandas/core/indexes/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5470,6 +5470,15 @@ def equals(self, other: Any) -> bool:
54705470
if len(self) != len(other):
54715471
# quickly return if the lengths are different
54725472
return False
5473+
5474+
if (
5475+
(isinstance(self.dtype, StringDtype)
5476+
or is_object_dtype(self.dtype))
5477+
and
5478+
(isinstance(other.dtype, StringDtype)
5479+
or is_object_dtype(other.dtype))
5480+
):
5481+
return array_equivalent(self.astype("object")._values, other.astype("object")._values)
54735482

54745483
if (
54755484
isinstance(self.dtype, StringDtype)

pandas/tests/indexes/test_base.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,3 +1717,34 @@ def test_is_monotonic_pyarrow_list_type():
17171717
idx = Index([[1], [2, 3]], dtype=pd.ArrowDtype(pa.list_(pa.int64())))
17181718
assert not idx.is_monotonic_increasing
17191719
assert not idx.is_monotonic_decreasing
1720+
1721+
def test_index_equals_string_vs_object():
1722+
# GH 61099
1723+
idx1 = pd.Index(['a', 'b', 'c'])
1724+
idx2 = pd.Index(['a', 'b', 'c'], dtype='string')
1725+
assert idx1.equals(idx2)
1726+
assert idx2.equals(idx1)
1727+
1728+
def test_compare_string_vs_object_index_equality():
1729+
# GH 61099
1730+
s1 = pd.Series([1, 2, 3], index=['a', 'b', 'c']) # dtype=object
1731+
s2 = pd.Series([0, 1, 2], index=pd.Index(['a', 'b', 'c'], dtype='string')) # dtype=string
1732+
result = s1 > s2
1733+
expected = pd.Series([True, True, True], index=['a', 'b', 'c'])
1734+
pd.testing.assert_series_equal(result, expected)
1735+
1736+
def test_align_string_vs_object_index():
1737+
# GH 61099
1738+
s1 = pd.Series([1, 2, 3], index=['a', 'b', 'c']) # object
1739+
s2 = pd.Series([1, 2, 3], index=pd.Index(['a', 'b', 'c'], dtype='string')) # string
1740+
s1_aligned, s2_aligned = s1.align(s2)
1741+
assert list(s1_aligned.index) == list(s2_aligned.index)
1742+
1743+
def test_comparison_without_manual_casting():
1744+
# GH 61099
1745+
s1 = pd.Series([1, 2, 3], index=['a', 'b', 'c']) # object index
1746+
s2 = pd.Series([1, 2, 3], index=pd.Index(['a', 'b', 'c'], dtype='string'))
1747+
# Should not raise
1748+
result = s1 == s2
1749+
expected = pd.Series([True, True, True], index=['a', 'b', 'c'])
1750+
pd.testing.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)