Fix Index.equals between object and string #61541
Open
+70
−16
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
doc/source/whatsnew/vX.X.X.rst
file if fixing a bug or adding a new feature.Description of the code change on
Index.equals
On the main branch,
Index.equals
castsself
toobject
only whenself.dtype.na_value
isnp.nan
. The comparison actually succeeds whenself.dtype.na_value
isnp.nan
as below.However, since doc stated that
dtype
is not compared,self
should be casted regardless ofself.dtype.na_value
so thatself
could be compared with other dtypes as desired.Description of the code change on
test_mixed_col_index_dtype
using_infer_string
has been removed since I think thatresult
should bestring
regardless ofusing_infer_string
. This is becaus of the code change made onIndex.equals
- sinceIndex.equals
considerdf1.columns
is equal todf2.colums
,Index.intersection
returnsself
(which isstring
). You could see the result becomesobject
(which is the dtype ofdf2
) in case ofresult = df2 + df1
. On the main branch, on the other hand,Index.intersection
returnsobject
becauseIndex.equals
returnsFalse
, and then bothself
andother
are cast toobject
by_find_common_type_compat
. (seeL3287
at pandas/core/indexes/base.py)pandas/pandas/core/indexes/base.py
Lines 3286 to 3290 in 25e6462