Skip to content

Commit 59ffa25

Browse files
CLN: consistent signatures for equals methods (#35636)
1 parent 3c87b01 commit 59ffa25

File tree

11 files changed

+38
-25
lines changed

11 files changed

+38
-25
lines changed

pandas/_libs/sparse.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ cdef class IntIndex(SparseIndex):
103103
if not monotonic:
104104
raise ValueError("Indices must be strictly increasing")
105105

106-
def equals(self, other) -> bool:
106+
def equals(self, other: object) -> bool:
107107
if not isinstance(other, IntIndex):
108108
return False
109109

@@ -399,7 +399,7 @@ cdef class BlockIndex(SparseIndex):
399399
if blengths[i] == 0:
400400
raise ValueError(f'Zero-length block {i}')
401401

402-
def equals(self, other) -> bool:
402+
def equals(self, other: object) -> bool:
403403
if not isinstance(other, BlockIndex):
404404
return False
405405

pandas/core/arrays/base.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
without warning.
88
"""
99
import operator
10-
from typing import Any, Callable, Dict, Optional, Sequence, Tuple, Union
10+
from typing import Any, Callable, Dict, Optional, Sequence, Tuple, Union, cast
1111

1212
import numpy as np
1313

@@ -20,7 +20,12 @@
2020
from pandas.util._validators import validate_fillna_kwargs
2121

2222
from pandas.core.dtypes.cast import maybe_cast_to_extension_array
23-
from pandas.core.dtypes.common import is_array_like, is_list_like, pandas_dtype
23+
from pandas.core.dtypes.common import (
24+
is_array_like,
25+
is_dtype_equal,
26+
is_list_like,
27+
pandas_dtype,
28+
)
2429
from pandas.core.dtypes.dtypes import ExtensionDtype
2530
from pandas.core.dtypes.generic import ABCDataFrame, ABCIndexClass, ABCSeries
2631
from pandas.core.dtypes.missing import isna
@@ -742,7 +747,7 @@ def searchsorted(self, value, side="left", sorter=None):
742747
arr = self.astype(object)
743748
return arr.searchsorted(value, side=side, sorter=sorter)
744749

745-
def equals(self, other: "ExtensionArray") -> bool:
750+
def equals(self, other: object) -> bool:
746751
"""
747752
Return if another array is equivalent to this array.
748753
@@ -762,7 +767,8 @@ def equals(self, other: "ExtensionArray") -> bool:
762767
"""
763768
if not type(self) == type(other):
764769
return False
765-
elif not self.dtype == other.dtype:
770+
other = cast(ExtensionArray, other)
771+
if not is_dtype_equal(self.dtype, other.dtype):
766772
return False
767773
elif not len(self) == len(other):
768774
return False

pandas/core/arrays/categorical.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -2242,7 +2242,7 @@ def _from_factorized(cls, uniques, original):
22422242
original.categories.take(uniques), dtype=original.dtype
22432243
)
22442244

2245-
def equals(self, other):
2245+
def equals(self, other: object) -> bool:
22462246
"""
22472247
Returns True if categorical arrays are equal.
22482248
@@ -2254,7 +2254,9 @@ def equals(self, other):
22542254
-------
22552255
bool
22562256
"""
2257-
if self.is_dtype_equal(other):
2257+
if not isinstance(other, Categorical):
2258+
return False
2259+
elif self.is_dtype_equal(other):
22582260
if self.categories.equals(other.categories):
22592261
# fastpath to avoid re-coding
22602262
other_codes = other._codes

pandas/core/generic.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
Tuple,
2323
Type,
2424
Union,
25+
cast,
2526
)
2627
import warnings
2728
import weakref
@@ -1196,7 +1197,7 @@ def _indexed_same(self, other) -> bool:
11961197
self._get_axis(a).equals(other._get_axis(a)) for a in self._AXIS_ORDERS
11971198
)
11981199

1199-
def equals(self, other):
1200+
def equals(self, other: object) -> bool:
12001201
"""
12011202
Test whether two objects contain the same elements.
12021203
@@ -1276,6 +1277,7 @@ def equals(self, other):
12761277
"""
12771278
if not (isinstance(other, type(self)) or isinstance(self, type(other))):
12781279
return False
1280+
other = cast(NDFrame, other)
12791281
return self._mgr.equals(other._mgr)
12801282

12811283
# -------------------------------------------------------------------------

pandas/core/indexes/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4176,7 +4176,7 @@ def putmask(self, mask, value):
41764176
# coerces to object
41774177
return self.astype(object).putmask(mask, value)
41784178

4179-
def equals(self, other: Any) -> bool:
4179+
def equals(self, other: object) -> bool:
41804180
"""
41814181
Determine if two Index object are equal.
41824182

pandas/core/indexes/category.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ def _is_dtype_compat(self, other) -> bool:
290290

291291
return other
292292

293-
def equals(self, other) -> bool:
293+
def equals(self, other: object) -> bool:
294294
"""
295295
Determine if two CategoricalIndex objects contain the same elements.
296296

pandas/core/indexes/datetimelike.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
is_scalar,
2525
)
2626
from pandas.core.dtypes.concat import concat_compat
27-
from pandas.core.dtypes.generic import ABCIndex, ABCIndexClass, ABCSeries
27+
from pandas.core.dtypes.generic import ABCIndex, ABCSeries
2828

2929
from pandas.core import algorithms
3030
from pandas.core.arrays import DatetimeArray, PeriodArray, TimedeltaArray
@@ -130,14 +130,14 @@ def __array_wrap__(self, result, context=None):
130130

131131
# ------------------------------------------------------------------------
132132

133-
def equals(self, other) -> bool:
133+
def equals(self, other: object) -> bool:
134134
"""
135135
Determines if two Index objects contain the same elements.
136136
"""
137137
if self.is_(other):
138138
return True
139139

140-
if not isinstance(other, ABCIndexClass):
140+
if not isinstance(other, Index):
141141
return False
142142
elif not isinstance(other, type(self)):
143143
try:

pandas/core/indexes/interval.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1005,19 +1005,20 @@ def _format_space(self) -> str:
10051005
def argsort(self, *args, **kwargs) -> np.ndarray:
10061006
return np.lexsort((self.right, self.left))
10071007

1008-
def equals(self, other) -> bool:
1008+
def equals(self, other: object) -> bool:
10091009
"""
10101010
Determines if two IntervalIndex objects contain the same elements.
10111011
"""
10121012
if self.is_(other):
10131013
return True
10141014

1015-
# if we can coerce to an II
1016-
# then we can compare
1015+
# if we can coerce to an IntervalIndex then we can compare
10171016
if not isinstance(other, IntervalIndex):
10181017
if not is_interval_dtype(other):
10191018
return False
10201019
other = Index(other)
1020+
if not isinstance(other, IntervalIndex):
1021+
return False
10211022

10221023
return (
10231024
self.left.equals(other.left)

pandas/core/indexes/multi.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -3221,7 +3221,7 @@ def truncate(self, before=None, after=None):
32213221
verify_integrity=False,
32223222
)
32233223

3224-
def equals(self, other) -> bool:
3224+
def equals(self, other: object) -> bool:
32253225
"""
32263226
Determines if two MultiIndex objects have the same labeling information
32273227
(the levels themselves do not necessarily have to be the same)
@@ -3264,11 +3264,10 @@ def equals(self, other) -> bool:
32643264
np.asarray(other.levels[i]._values), other_codes, allow_fill=False
32653265
)
32663266

3267-
# since we use NaT both datetime64 and timedelta64
3268-
# we can have a situation where a level is typed say
3269-
# timedelta64 in self (IOW it has other values than NaT)
3270-
# but types datetime64 in other (where its all NaT)
3271-
# but these are equivalent
3267+
# since we use NaT both datetime64 and timedelta64 we can have a
3268+
# situation where a level is typed say timedelta64 in self (IOW it
3269+
# has other values than NaT) but types datetime64 in other (where
3270+
# its all NaT) but these are equivalent
32723271
if len(self_values) == 0 and len(other_values) == 0:
32733272
continue
32743273

pandas/core/indexes/range.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ def argsort(self, *args, **kwargs) -> np.ndarray:
433433
else:
434434
return np.arange(len(self) - 1, -1, -1)
435435

436-
def equals(self, other) -> bool:
436+
def equals(self, other: object) -> bool:
437437
"""
438438
Determines if two Index objects contain the same elements.
439439
"""

pandas/core/internals/managers.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1437,7 +1437,10 @@ def take(self, indexer, axis: int = 1, verify: bool = True, convert: bool = True
14371437
new_axis=new_labels, indexer=indexer, axis=axis, allow_dups=True
14381438
)
14391439

1440-
def equals(self, other: "BlockManager") -> bool:
1440+
def equals(self, other: object) -> bool:
1441+
if not isinstance(other, BlockManager):
1442+
return False
1443+
14411444
self_axes, other_axes = self.axes, other.axes
14421445
if len(self_axes) != len(other_axes):
14431446
return False

0 commit comments

Comments
 (0)