Skip to content

DOC: Improved doc for Index.equals #33289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4193,15 +4193,43 @@ def putmask(self, mask, value):
# coerces to object
return self.astype(object).putmask(mask, value)

def equals(self, other) -> bool:
def equals(self, other: Any) -> bool:
"""
Determine if two Index objects contain the same elements.
Determine if two Index objects contain the same elements with the same order.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or we could also simplify "... contain the same elements with the same order" to "... are equal" ?

We can in a second paragraph detail on what is considered as equal


Returns
-------
bool
True if "other" is an Index and it has the same elements as calling
index; False otherwise.
True if "other" is an Index and it has the same elements and order
as the calling index; False otherwise.

Examples
--------
>>> idx1 = pd.Index([1, 2, 3])
>>> idx1
Int64Index([1, 2, 3], dtype='int64')
>>> idx1.equals(pd.Index([1, 2, 3]))
True

The dtype is been compared as well
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is been -> is

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dtype is actually not compared:

In [9]: pd.Int64Index([1, 2, 3]).equals(pd.UInt64Index([1, 2, 3]))                                                                                                                            
Out[9]: True

(in your example here, the values are not equal as a string is not equal to an integer)


>>> idx2 = pd.Index(["1", "2", "3"])
>>> idx2
Index(['1', '2', '3'], dtype='object')

>>> idx1.equals(idx2)
False

The oreder is also been compared
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oreder is also been compared -> order is also compared


>>> ascending_idx = pd.Index([1, 2, 3])
>>> ascending_idx
Int64Index([1, 2, 3], dtype='int64')
>>> descending_idx = pd.Index([3, 2, 1])
>>> descending_idx
Int64Index([3, 2, 1], dtype='int64')
>>> ascending_idx.equals(descending_idx)
False
"""
if self.is_(other):
return True
Expand Down