Skip to content

PERF: avoid is_dtype_equal #52424

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 7 commits into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
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
7 changes: 2 additions & 5 deletions pandas/core/arrays/_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@
validate_insert_loc,
)

from pandas.core.dtypes.common import (
is_dtype_equal,
pandas_dtype,
)
from pandas.core.dtypes.common import pandas_dtype
from pandas.core.dtypes.dtypes import (
DatetimeTZDtype,
ExtensionDtype,
Expand Down Expand Up @@ -172,7 +169,7 @@ def take(
def equals(self, other) -> bool:
if type(self) is not type(other):
return False
if not is_dtype_equal(self.dtype, other.dtype):
if self.dtype != other.dtype:
return False
return bool(array_equivalent(self._ndarray, other._ndarray, dtype_equal=True))

Expand Down
5 changes: 2 additions & 3 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@

from pandas.core.dtypes.cast import maybe_cast_to_extension_array
from pandas.core.dtypes.common import (
is_dtype_equal,
is_list_like,
is_scalar,
pandas_dtype,
Expand Down Expand Up @@ -570,7 +569,7 @@ def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike:
"""

dtype = pandas_dtype(dtype)
if is_dtype_equal(dtype, self.dtype):
if dtype == self.dtype:
if not copy:
return self
else:
Expand Down Expand Up @@ -958,7 +957,7 @@ def equals(self, other: object) -> bool:
if type(self) != type(other):
return False
other = cast(ExtensionArray, other)
if not is_dtype_equal(self.dtype, other.dtype):
if self.dtype != other.dtype:
return False
elif len(self) != len(other):
return False
Expand Down
5 changes: 2 additions & 3 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
is_any_real_numeric_dtype,
is_bool_dtype,
is_dict_like,
is_dtype_equal,
is_hashable,
is_integer_dtype,
is_list_like,
Expand Down Expand Up @@ -1394,7 +1393,7 @@ def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
categorical.categories.dtype.
"""
ret = take_nd(self.categories._values, self._codes)
if dtype and not is_dtype_equal(dtype, self.categories.dtype):
if dtype and np.dtype(dtype) != self.categories.dtype:
return np.asarray(ret, dtype)
# When we're a Categorical[ExtensionArray], like Interval,
# we need to ensure __array__ gets all the way to an
Expand Down Expand Up @@ -2018,7 +2017,7 @@ def _validate_listlike(self, value):

# require identical categories set
if isinstance(value, Categorical):
if not is_dtype_equal(self.dtype, value.dtype):
if self.dtype != value.dtype:
raise TypeError(
"Cannot set a Categorical with another, "
"without identical categories"
Expand Down
5 changes: 2 additions & 3 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@

from pandas.core.dtypes.common import (
is_all_strings,
is_dtype_equal,
is_integer_dtype,
is_list_like,
is_object_dtype,
Expand Down Expand Up @@ -664,7 +663,7 @@ def _validate_listlike(self, value, allow_object: bool = False):

if isinstance(value.dtype, CategoricalDtype):
# e.g. we have a Categorical holding self.dtype
if is_dtype_equal(value.categories.dtype, self.dtype):
if value.categories.dtype == self.dtype:
# TODO: do we need equal dtype or just comparable?
value = value._internal_get_values()
value = extract_array(value, extract_numpy=True)
Expand Down Expand Up @@ -1859,7 +1858,7 @@ def __init__(

if dtype is not None:
dtype = pandas_dtype(dtype)
if not is_dtype_equal(dtype, values.dtype):
if dtype != values.dtype:
# TODO: we only have tests for this for DTA, not TDA (2022-07-01)
raise TypeError(
f"dtype={dtype} does not match data dtype {values.dtype}"
Expand Down
5 changes: 2 additions & 3 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
DT64NS_DTYPE,
INT64_DTYPE,
is_bool_dtype,
is_dtype_equal,
is_float_dtype,
is_string_dtype,
pandas_dtype,
Expand Down Expand Up @@ -642,7 +641,7 @@ def astype(self, dtype, copy: bool = True):
# DatetimeLikeArrayMixin Super handles the rest.
dtype = pandas_dtype(dtype)

if is_dtype_equal(dtype, self.dtype):
if dtype == self.dtype:
if copy:
return self.copy()
return self
Expand Down Expand Up @@ -2316,7 +2315,7 @@ def _validate_dt64_dtype(dtype):
"""
if dtype is not None:
dtype = pandas_dtype(dtype)
if is_dtype_equal(dtype, np.dtype("M8")):
if dtype == np.dtype("M8"):
# no precision, disallowed GH#24806
msg = (
"Passing in 'datetime64' dtype with no precision is not allowed. "
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
maybe_upcast_numeric_to_64bit,
)
from pandas.core.dtypes.common import (
is_dtype_equal,
is_float_dtype,
is_integer_dtype,
is_list_like,
Expand Down Expand Up @@ -1684,7 +1683,7 @@ def isin(self, values) -> npt.NDArray[np.bool_]:
# not comparable -> no overlap
return np.zeros(self.shape, dtype=bool)

if is_dtype_equal(self.dtype, values.dtype):
if self.dtype == values.dtype:
# GH#38353 instead of casting to object, operating on a
# complex128 ndarray is much more performant.
left = self._combined.view("complex128")
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/arrays/masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
from pandas.core.dtypes.base import ExtensionDtype
from pandas.core.dtypes.common import (
is_bool,
is_dtype_equal,
is_integer_dtype,
is_list_like,
is_scalar,
Expand Down Expand Up @@ -451,7 +450,7 @@ def astype(self, dtype: AstypeArg, copy: bool = ...) -> ArrayLike:
def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike:
dtype = pandas_dtype(dtype)

if is_dtype_equal(dtype, self.dtype):
if dtype == self.dtype:
if copy:
return self.copy()
return self
Expand Down
7 changes: 2 additions & 5 deletions pandas/core/arrays/numpy_.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@

from pandas.core.dtypes.astype import astype_array
from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike
from pandas.core.dtypes.common import (
is_dtype_equal,
pandas_dtype,
)
from pandas.core.dtypes.common import pandas_dtype
from pandas.core.dtypes.dtypes import PandasDtype
from pandas.core.dtypes.missing import isna

Expand Down Expand Up @@ -199,7 +196,7 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
def astype(self, dtype, copy: bool = True):
dtype = pandas_dtype(dtype)

if is_dtype_equal(dtype, self.dtype):
if dtype == self.dtype:
if copy:
return self.copy()
return self
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@

from pandas.core.dtypes.common import (
ensure_object,
is_dtype_equal,
pandas_dtype,
)
from pandas.core.dtypes.dtypes import (
Expand Down Expand Up @@ -650,7 +649,7 @@ def astype(self, dtype, copy: bool = True):
# We handle Period[T] -> Period[U]
# Our parent handles everything else.
dtype = pandas_dtype(dtype)
if is_dtype_equal(dtype, self._dtype):
if dtype == self._dtype:
if not copy:
return self
else:
Expand Down
5 changes: 2 additions & 3 deletions pandas/core/arrays/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
from pandas.core.dtypes.common import (
is_array_like,
is_bool_dtype,
is_dtype_equal,
is_integer,
is_list_like,
is_object_dtype,
Expand Down Expand Up @@ -180,7 +179,7 @@ def _sparse_array_op(
ltype = left.dtype.subtype
rtype = right.dtype.subtype

if not is_dtype_equal(ltype, rtype):
if ltype != rtype:
subtype = find_common_type([ltype, rtype])
ltype = SparseDtype(subtype, left.fill_value)
rtype = SparseDtype(subtype, right.fill_value)
Expand Down Expand Up @@ -1240,7 +1239,7 @@ def astype(self, dtype: AstypeArg | None = None, copy: bool = True):
IntIndex
Indices: array([2, 3], dtype=int32)
"""
if is_dtype_equal(dtype, self._dtype):
if dtype == self._dtype:
if not copy:
return self
else:
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/arrays/string_.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from pandas.core.dtypes.common import (
is_array_like,
is_bool_dtype,
is_dtype_equal,
is_integer_dtype,
is_object_dtype,
is_string_dtype,
Expand Down Expand Up @@ -444,7 +443,7 @@ def _putmask(self, mask: npt.NDArray[np.bool_], value) -> None:
def astype(self, dtype, copy: bool = True):
dtype = pandas_dtype(dtype)

if is_dtype_equal(dtype, self.dtype):
if dtype == self.dtype:
if copy:
return self.copy()
return self
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/arrays/string_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

from pandas.core.dtypes.common import (
is_bool_dtype,
is_dtype_equal,
is_integer_dtype,
is_object_dtype,
is_scalar,
Expand Down Expand Up @@ -210,7 +209,7 @@ def isin(self, values) -> npt.NDArray[np.bool_]:
def astype(self, dtype, copy: bool = True):
dtype = pandas_dtype(dtype)

if is_dtype_equal(dtype, self.dtype):
if dtype == self.dtype:
if copy:
return self.copy()
return self
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@

from pandas.core.dtypes.common import (
TD64NS_DTYPE,
is_dtype_equal,
is_float_dtype,
is_integer_dtype,
is_object_dtype,
Expand Down Expand Up @@ -1048,7 +1047,7 @@ def _objects_to_td64ns(data, unit=None, errors: DateTimeErrorChoices = "raise"):

def _validate_td64_dtype(dtype) -> DtypeObj:
dtype = pandas_dtype(dtype)
if is_dtype_equal(dtype, np.dtype("timedelta64")):
if dtype == np.dtype("m8"):
# no precision disallowed GH#24806
msg = (
"Passing in 'timedelta' dtype with no precision is not allowed. "
Expand Down
5 changes: 1 addition & 4 deletions pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
)
from pandas.core.dtypes.common import (
is_datetime64_ns_dtype,
is_dtype_equal,
is_extension_array_dtype,
is_list_like,
is_object_dtype,
Expand Down Expand Up @@ -313,9 +312,7 @@ def array(
if isinstance(dtype, str):
dtype = registry.find(dtype) or dtype

if isinstance(data, ExtensionArray) and (
dtype is None or is_dtype_equal(dtype, data.dtype)
):
if isinstance(data, ExtensionArray) and (dtype is None or data.dtype == dtype):
# e.g. TimedeltaArray[s], avoid casting to PandasArray
if copy:
return data.copy()
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/dtypes/astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from pandas.errors import IntCastingNaNError

from pandas.core.dtypes.common import (
is_dtype_equal,
is_object_dtype,
is_string_dtype,
pandas_dtype,
Expand Down Expand Up @@ -171,7 +170,7 @@ def astype_array(values: ArrayLike, dtype: DtypeObj, copy: bool = False) -> Arra
-------
ndarray or ExtensionArray
"""
if is_dtype_equal(values.dtype, dtype):
if values.dtype == dtype:
if copy:
return values.copy()
return values
Expand Down
6 changes: 1 addition & 5 deletions pandas/core/dtypes/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
common_dtype_categorical_compat,
find_common_type,
)
from pandas.core.dtypes.common import is_dtype_equal
from pandas.core.dtypes.dtypes import (
CategoricalDtype,
DatetimeTZDtype,
Expand Down Expand Up @@ -276,10 +275,7 @@ def _maybe_unwrap(x):
to_union = [_maybe_unwrap(x) for x in to_union]
first = to_union[0]

if not all(
is_dtype_equal(other.categories.dtype, first.categories.dtype)
for other in to_union[1:]
):
if not lib.dtypes_all_equal([obj.categories.dtype for obj in to_union]):
raise TypeError("dtype of categories must be the same")

ordered = False
Expand Down
4 changes: 1 addition & 3 deletions pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1248,9 +1248,7 @@ def __eq__(self, other: Any) -> bool:
elif self.closed != other.closed:
return False
else:
from pandas.core.dtypes.common import is_dtype_equal

return is_dtype_equal(self.subtype, other.subtype)
return self.subtype == other.subtype

def __setstate__(self, state) -> None:
# for pickle compat. __get_state__ is defined in the
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/dtypes/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
DT64NS_DTYPE,
TD64NS_DTYPE,
ensure_object,
is_dtype_equal,
is_scalar,
is_string_or_object_np_dtype,
)
Expand Down Expand Up @@ -592,7 +591,7 @@ def array_equals(left: ArrayLike, right: ArrayLike) -> bool:
"""
ExtensionArray-compatible implementation of array_equivalent.
"""
if not is_dtype_equal(left.dtype, right.dtype):
if left.dtype != right.dtype:
return False
elif isinstance(left, ABCExtensionArray):
return left.equals(right)
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@
is_bool_dtype,
is_dataclass,
is_dict_like,
is_dtype_equal,
is_float,
is_float_dtype,
is_hashable,
Expand Down Expand Up @@ -8331,7 +8330,7 @@ def combiner(x, y):
dtypes = {
col: find_common_type([self.dtypes[col], other.dtypes[col]])
for col in self.columns.intersection(other.columns)
if not is_dtype_equal(combined.dtypes[col], self.dtypes[col])
if combined.dtypes[col] != self.dtypes[col]
}

if dtypes:
Expand Down
Loading