Skip to content

Commit 9540a88

Browse files
jbrockmendeltopper-123
authored andcommitted
PERF: needs_i8_conversion expect dtype object (pandas-dev#52288)
1 parent 405c35f commit 9540a88

File tree

4 files changed

+24
-28
lines changed

4 files changed

+24
-28
lines changed

pandas/core/dtypes/common.py

+15-20
Original file line numberDiff line numberDiff line change
@@ -1090,22 +1090,20 @@ def is_numeric_v_string_like(a: ArrayLike, b) -> bool:
10901090
)
10911091

10921092

1093-
def needs_i8_conversion(arr_or_dtype) -> bool:
1093+
def needs_i8_conversion(dtype: DtypeObj | None) -> bool:
10941094
"""
1095-
Check whether the array or dtype should be converted to int64.
1095+
Check whether the dtype should be converted to int64.
10961096
1097-
An array-like or dtype "needs" such a conversion if the array-like
1098-
or dtype is of a datetime-like dtype
1097+
Dtype "needs" such a conversion if the dtype is of a datetime-like dtype
10991098
11001099
Parameters
11011100
----------
1102-
arr_or_dtype : array-like or dtype
1103-
The array or dtype to check.
1101+
dtype : np.dtype, ExtensionDtype, or None
11041102
11051103
Returns
11061104
-------
11071105
boolean
1108-
Whether or not the array or dtype should be converted to int64.
1106+
Whether or not the dtype should be converted to int64.
11091107
11101108
Examples
11111109
--------
@@ -1114,30 +1112,27 @@ def needs_i8_conversion(arr_or_dtype) -> bool:
11141112
>>> needs_i8_conversion(np.int64)
11151113
False
11161114
>>> needs_i8_conversion(np.datetime64)
1115+
False
1116+
>>> needs_i8_conversion(np.dtype(np.datetime64))
11171117
True
11181118
>>> needs_i8_conversion(np.array(['a', 'b']))
11191119
False
11201120
>>> needs_i8_conversion(pd.Series([1, 2]))
11211121
False
11221122
>>> needs_i8_conversion(pd.Series([], dtype="timedelta64[ns]"))
1123-
True
1123+
False
11241124
>>> needs_i8_conversion(pd.DatetimeIndex([1, 2, 3], tz="US/Eastern"))
1125+
False
1126+
>>> needs_i8_conversion(pd.DatetimeIndex([1, 2, 3], tz="US/Eastern").dtype)
11251127
True
11261128
"""
1127-
if arr_or_dtype is None:
1128-
return False
1129-
if isinstance(arr_or_dtype, np.dtype):
1130-
return arr_or_dtype.kind in ["m", "M"]
1131-
elif isinstance(arr_or_dtype, ExtensionDtype):
1132-
return isinstance(arr_or_dtype, (PeriodDtype, DatetimeTZDtype))
1133-
1134-
try:
1135-
dtype = get_dtype(arr_or_dtype)
1136-
except (TypeError, ValueError):
1129+
if dtype is None:
11371130
return False
11381131
if isinstance(dtype, np.dtype):
1139-
return dtype.kind in ["m", "M"]
1140-
return isinstance(dtype, (PeriodDtype, DatetimeTZDtype))
1132+
return dtype.kind in "mM"
1133+
elif isinstance(dtype, ExtensionDtype):
1134+
return isinstance(dtype, (PeriodDtype, DatetimeTZDtype))
1135+
return False
11411136

11421137

11431138
def is_numeric_dtype(arr_or_dtype) -> bool:

pandas/core/indexes/base.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -960,9 +960,7 @@ def view(self, cls=None):
960960
if isinstance(cls, str):
961961
dtype = pandas_dtype(cls)
962962

963-
if isinstance(dtype, (np.dtype, ExtensionDtype)) and needs_i8_conversion(
964-
dtype
965-
):
963+
if needs_i8_conversion(dtype):
966964
if dtype.kind == "m" and dtype != "m8[ns]":
967965
# e.g. m8[s]
968966
return self._data.view(cls)

pandas/core/reshape/merge.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2015,7 +2015,7 @@ def _get_merge_keys(
20152015
f"with type {repr(lt.dtype)}"
20162016
)
20172017

2018-
if needs_i8_conversion(lt):
2018+
if needs_i8_conversion(getattr(lt, "dtype", None)):
20192019
if not isinstance(self.tolerance, datetime.timedelta):
20202020
raise MergeError(msg)
20212021
if self.tolerance < Timedelta(0):
@@ -2101,7 +2101,7 @@ def injection(obj):
21012101
raise ValueError(f"{side} keys must be sorted")
21022102

21032103
# initial type conversion as needed
2104-
if needs_i8_conversion(left_values):
2104+
if needs_i8_conversion(getattr(left_values, "dtype", None)):
21052105
if tolerance is not None:
21062106
tolerance = Timedelta(tolerance)
21072107

pandas/tests/dtypes/test_common.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -518,9 +518,12 @@ def test_needs_i8_conversion():
518518
assert not com.needs_i8_conversion(pd.Series([1, 2]))
519519
assert not com.needs_i8_conversion(np.array(["a", "b"]))
520520

521-
assert com.needs_i8_conversion(np.datetime64)
522-
assert com.needs_i8_conversion(pd.Series([], dtype="timedelta64[ns]"))
523-
assert com.needs_i8_conversion(pd.DatetimeIndex(["2000"], tz="US/Eastern"))
521+
assert not com.needs_i8_conversion(np.datetime64)
522+
assert com.needs_i8_conversion(np.dtype(np.datetime64))
523+
assert not com.needs_i8_conversion(pd.Series([], dtype="timedelta64[ns]"))
524+
assert com.needs_i8_conversion(pd.Series([], dtype="timedelta64[ns]").dtype)
525+
assert not com.needs_i8_conversion(pd.DatetimeIndex(["2000"], tz="US/Eastern"))
526+
assert com.needs_i8_conversion(pd.DatetimeIndex(["2000"], tz="US/Eastern").dtype)
524527

525528

526529
def test_is_numeric_dtype():

0 commit comments

Comments
 (0)