Skip to content

Commit 7e6d509

Browse files
jbrockmendeljreback
authored andcommitted
CLN: remove is_datetimelike (#29452)
1 parent 5983b46 commit 7e6d509

File tree

10 files changed

+15
-85
lines changed

10 files changed

+15
-85
lines changed

pandas/core/algorithms.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
is_complex_dtype,
3030
is_datetime64_any_dtype,
3131
is_datetime64_ns_dtype,
32-
is_datetimelike,
3332
is_extension_array_dtype,
3433
is_float_dtype,
3534
is_integer,
@@ -834,7 +833,7 @@ def mode(values, dropna: bool = True) -> ABCSeries:
834833
return Series(values.values.mode(dropna=dropna), name=values.name)
835834
return values.mode(dropna=dropna)
836835

837-
if dropna and is_datetimelike(values):
836+
if dropna and needs_i8_conversion(values.dtype):
838837
mask = values.isnull()
839838
values = values[~mask]
840839

pandas/core/arrays/categorical.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
ensure_platform_int,
2626
is_categorical_dtype,
2727
is_datetime64_dtype,
28-
is_datetimelike,
2928
is_dict_like,
3029
is_dtype_equal,
3130
is_extension_array_dtype,
@@ -37,6 +36,7 @@
3736
is_scalar,
3837
is_sequence,
3938
is_timedelta64_dtype,
39+
needs_i8_conversion,
4040
)
4141
from pandas.core.dtypes.dtypes import CategoricalDtype
4242
from pandas.core.dtypes.generic import ABCDataFrame, ABCIndexClass, ABCSeries
@@ -1533,7 +1533,7 @@ def get_values(self):
15331533

15341534
def _internal_get_values(self):
15351535
# if we are a datetime and period index, return Index to keep metadata
1536-
if is_datetimelike(self.categories):
1536+
if needs_i8_conversion(self.categories):
15371537
return self.categories.take(self._codes, fill_value=np.nan)
15381538
elif is_integer_dtype(self.categories) and -1 in self._codes:
15391539
return self.categories.astype("object").take(self._codes, fill_value=np.nan)

pandas/core/base.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
is_categorical_dtype,
2222
is_datetime64_ns_dtype,
2323
is_datetime64tz_dtype,
24-
is_datetimelike,
2524
is_extension_array_dtype,
2625
is_extension_type,
2726
is_list_like,
@@ -1172,7 +1171,7 @@ def tolist(self):
11721171
--------
11731172
numpy.ndarray.tolist
11741173
"""
1175-
if is_datetimelike(self._values):
1174+
if self.dtype.kind in ["m", "M"]:
11761175
return [com.maybe_box_datetimelike(x) for x in self._values]
11771176
elif is_extension_array_dtype(self._values):
11781177
return list(self._values)
@@ -1194,7 +1193,7 @@ def __iter__(self):
11941193
iterator
11951194
"""
11961195
# We are explicitly making element iterators.
1197-
if is_datetimelike(self._values):
1196+
if self.dtype.kind in ["m", "M"]:
11981197
return map(com.maybe_box_datetimelike, self._values)
11991198
elif is_extension_array_dtype(self._values):
12001199
return iter(self._values)

pandas/core/dtypes/cast.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
is_datetime64_ns_dtype,
2828
is_datetime64tz_dtype,
2929
is_datetime_or_timedelta_dtype,
30-
is_datetimelike,
3130
is_dtype_equal,
3231
is_extension_array_dtype,
3332
is_extension_type,
@@ -274,7 +273,7 @@ def maybe_upcast_putmask(result: np.ndarray, mask: np.ndarray, other):
274273
# in np.place:
275274
# NaN -> NaT
276275
# integer or integer array -> date-like array
277-
if is_datetimelike(result.dtype):
276+
if result.dtype.kind in ["m", "M"]:
278277
if is_scalar(other):
279278
if isna(other):
280279
other = result.dtype.type("nat")

pandas/core/dtypes/common.py

+2-51
Original file line numberDiff line numberDiff line change
@@ -799,54 +799,6 @@ def is_datetime_arraylike(arr):
799799
return getattr(arr, "inferred_type", None) == "datetime"
800800

801801

802-
def is_datetimelike(arr):
803-
"""
804-
Check whether an array-like is a datetime-like array-like.
805-
806-
Acceptable datetime-like objects are (but not limited to) datetime
807-
indices, periodic indices, and timedelta indices.
808-
809-
Parameters
810-
----------
811-
arr : array-like
812-
The array-like to check.
813-
814-
Returns
815-
-------
816-
boolean
817-
Whether or not the array-like is a datetime-like array-like.
818-
819-
Examples
820-
--------
821-
>>> is_datetimelike([1, 2, 3])
822-
False
823-
>>> is_datetimelike(pd.Index([1, 2, 3]))
824-
False
825-
>>> is_datetimelike(pd.DatetimeIndex([1, 2, 3]))
826-
True
827-
>>> is_datetimelike(pd.DatetimeIndex([1, 2, 3], tz="US/Eastern"))
828-
True
829-
>>> is_datetimelike(pd.PeriodIndex([], freq="A"))
830-
True
831-
>>> is_datetimelike(np.array([], dtype=np.datetime64))
832-
True
833-
>>> is_datetimelike(pd.Series([], dtype="timedelta64[ns]"))
834-
True
835-
>>>
836-
>>> dtype = DatetimeTZDtype("ns", tz="US/Eastern")
837-
>>> s = pd.Series([], dtype=dtype)
838-
>>> is_datetimelike(s)
839-
True
840-
"""
841-
842-
return (
843-
is_datetime64_dtype(arr)
844-
or is_datetime64tz_dtype(arr)
845-
or is_timedelta64_dtype(arr)
846-
or isinstance(arr, ABCPeriodIndex)
847-
)
848-
849-
850802
def is_dtype_equal(source, target):
851803
"""
852804
Check if two dtypes are equal.
@@ -1446,9 +1398,8 @@ def is_numeric(x):
14461398
"""
14471399
return is_integer_dtype(x) or is_float_dtype(x)
14481400

1449-
is_datetimelike = needs_i8_conversion
1450-
return (is_datetimelike(a) and is_numeric(b)) or (
1451-
is_datetimelike(b) and is_numeric(a)
1401+
return (needs_i8_conversion(a) and is_numeric(b)) or (
1402+
needs_i8_conversion(b) and is_numeric(a)
14521403
)
14531404

14541405

pandas/core/dtypes/missing.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
is_complex_dtype,
1818
is_datetime64_dtype,
1919
is_datetime64tz_dtype,
20-
is_datetimelike,
2120
is_datetimelike_v_numeric,
2221
is_dtype_equal,
2322
is_extension_array_dtype,
@@ -494,7 +493,7 @@ def _infer_fill_value(val):
494493
if not is_list_like(val):
495494
val = [val]
496495
val = np.array(val, copy=False)
497-
if is_datetimelike(val):
496+
if needs_i8_conversion(val):
498497
return np.array("NaT", dtype=val.dtype)
499498
elif is_object_dtype(val.dtype):
500499
dtype = lib.infer_dtype(ensure_object(val), skipna=False)

pandas/core/groupby/generic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@
4040
ensure_int64,
4141
ensure_platform_int,
4242
is_bool,
43-
is_datetimelike,
4443
is_dict_like,
4544
is_integer_dtype,
4645
is_interval_dtype,
4746
is_list_like,
4847
is_numeric_dtype,
4948
is_object_dtype,
5049
is_scalar,
50+
needs_i8_conversion,
5151
)
5252
from pandas.core.dtypes.missing import _isna_ndarraylike, isna, notna
5353

@@ -1287,7 +1287,7 @@ def first_not_none(values):
12871287
# if we have date/time like in the original, then coerce dates
12881288
# as we are stacking can easily have object dtypes here
12891289
so = self._selected_obj
1290-
if so.ndim == 2 and so.dtypes.apply(is_datetimelike).any():
1290+
if so.ndim == 2 and so.dtypes.apply(needs_i8_conversion).any():
12911291
result = _recast_datetimelike_result(result)
12921292
else:
12931293
result = result._convert(datetime=True)

pandas/core/reshape/merge.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
is_bool_dtype,
2525
is_categorical_dtype,
2626
is_datetime64tz_dtype,
27-
is_datetimelike,
2827
is_dtype_equal,
2928
is_extension_array_dtype,
3029
is_float_dtype,
@@ -1120,9 +1119,9 @@ def _maybe_coerce_merge_keys(self):
11201119
raise ValueError(msg)
11211120

11221121
# datetimelikes must match exactly
1123-
elif is_datetimelike(lk) and not is_datetimelike(rk):
1122+
elif needs_i8_conversion(lk) and not needs_i8_conversion(rk):
11241123
raise ValueError(msg)
1125-
elif not is_datetimelike(lk) and is_datetimelike(rk):
1124+
elif not needs_i8_conversion(lk) and needs_i8_conversion(rk):
11261125
raise ValueError(msg)
11271126
elif is_datetime64tz_dtype(lk) and not is_datetime64tz_dtype(rk):
11281127
raise ValueError(msg)
@@ -1637,7 +1636,7 @@ def _get_merge_keys(self):
16371636
)
16381637
)
16391638

1640-
if is_datetimelike(lt):
1639+
if needs_i8_conversion(lt):
16411640
if not isinstance(self.tolerance, datetime.timedelta):
16421641
raise MergeError(msg)
16431642
if self.tolerance < Timedelta(0):

pandas/core/series.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
is_categorical,
2626
is_categorical_dtype,
2727
is_datetime64_dtype,
28-
is_datetimelike,
2928
is_dict_like,
3029
is_extension_array_dtype,
3130
is_extension_type,
@@ -2886,7 +2885,7 @@ def combine_first(self, other):
28862885
new_index = self.index.union(other.index)
28872886
this = self.reindex(new_index, copy=False)
28882887
other = other.reindex(new_index, copy=False)
2889-
if is_datetimelike(this) and not is_datetimelike(other):
2888+
if this.dtype.kind == "M" and other.dtype.kind != "M":
28902889
other = to_datetime(other)
28912890

28922891
return this.where(notna(this), other)

pandas/tests/dtypes/test_common.py

-15
Original file line numberDiff line numberDiff line change
@@ -309,21 +309,6 @@ def test_is_datetime_arraylike():
309309
assert com.is_datetime_arraylike(pd.DatetimeIndex([1, 2, 3]))
310310

311311

312-
def test_is_datetimelike():
313-
assert not com.is_datetimelike([1, 2, 3])
314-
assert not com.is_datetimelike(pd.Index([1, 2, 3]))
315-
316-
assert com.is_datetimelike(pd.DatetimeIndex([1, 2, 3]))
317-
assert com.is_datetimelike(pd.PeriodIndex([], freq="A"))
318-
assert com.is_datetimelike(np.array([], dtype=np.datetime64))
319-
assert com.is_datetimelike(pd.Series([], dtype="timedelta64[ns]"))
320-
assert com.is_datetimelike(pd.DatetimeIndex(["2000"], tz="US/Eastern"))
321-
322-
dtype = DatetimeTZDtype("ns", tz="US/Eastern")
323-
s = pd.Series([], dtype=dtype)
324-
assert com.is_datetimelike(s)
325-
326-
327312
integer_dtypes = [] # type: List
328313

329314

0 commit comments

Comments
 (0)