Skip to content

Commit 6e653b3

Browse files
jbrockmendelPingviinituutti
authored andcommitted
CLN: Remove/deprecate unused/misleading dtype functions (pandas-dev#23917)
1 parent 43ad336 commit 6e653b3

File tree

20 files changed

+115
-285
lines changed

20 files changed

+115
-285
lines changed

doc/source/whatsnew/v0.24.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,8 @@ Deprecations
10481048
- The ``keep_tz=False`` option (the default) of the ``keep_tz`` keyword of
10491049
:meth:`DatetimeIndex.to_series` is deprecated (:issue:`17832`).
10501050
- Timezone converting a tz-aware ``datetime.datetime`` or :class:`Timestamp` with :class:`Timestamp` and the ``tz`` argument is now deprecated. Instead, use :meth:`Timestamp.tz_convert` (:issue:`23579`)
1051+
- :func:`pandas.types.is_period` is deprecated in favor of `pandas.types.is_period_dtype` (:issue:`23917`)
1052+
- :func:`pandas.types.is_datetimetz` is deprecated in favor of `pandas.types.is_datetime64tz` (:issue:`23917`)
10511053

10521054
.. _whatsnew_0240.deprecations.datetimelike_int_ops:
10531055

pandas/_libs/lib.pyx

+2-29
Original file line numberDiff line numberDiff line change
@@ -1248,25 +1248,19 @@ def infer_dtype(value: object, skipna: bool=False) -> str:
12481248
if util.is_datetime64_object(val):
12491249
if is_datetime64_array(values):
12501250
return 'datetime64'
1251-
elif is_timedelta_or_timedelta64_array(values):
1252-
return 'timedelta'
12531251

12541252
elif is_timedelta(val):
12551253
if is_timedelta_or_timedelta64_array(values):
12561254
return 'timedelta'
12571255

12581256
elif util.is_integer_object(val):
1259-
# a timedelta will show true here as well
1260-
if is_timedelta(val):
1261-
if is_timedelta_or_timedelta64_array(values):
1262-
return 'timedelta'
1257+
# ordering matters here; this check must come after the is_timedelta
1258+
# check otherwise numpy timedelta64 objects would come through here
12631259

12641260
if is_integer_array(values):
12651261
return 'integer'
12661262
elif is_integer_float_array(values):
12671263
return 'mixed-integer-float'
1268-
elif is_timedelta_or_timedelta64_array(values):
1269-
return 'timedelta'
12701264
return 'mixed-integer'
12711265

12721266
elif PyDateTime_Check(val):
@@ -1699,27 +1693,6 @@ cdef class TimedeltaValidator(TemporalValidator):
16991693
return is_null_timedelta64(value)
17001694

17011695

1702-
# TODO: Not used outside of tests; remove?
1703-
def is_timedelta_array(values: ndarray) -> bool:
1704-
cdef:
1705-
TimedeltaValidator validator = TimedeltaValidator(len(values),
1706-
skipna=True)
1707-
return validator.validate(values)
1708-
1709-
1710-
cdef class Timedelta64Validator(TimedeltaValidator):
1711-
cdef inline bint is_value_typed(self, object value) except -1:
1712-
return util.is_timedelta64_object(value)
1713-
1714-
1715-
# TODO: Not used outside of tests; remove?
1716-
def is_timedelta64_array(values: ndarray) -> bool:
1717-
cdef:
1718-
Timedelta64Validator validator = Timedelta64Validator(len(values),
1719-
skipna=True)
1720-
return validator.validate(values)
1721-
1722-
17231696
cdef class AnyTimedeltaValidator(TimedeltaValidator):
17241697
cdef inline bint is_value_typed(self, object value) except -1:
17251698
return is_timedelta(value)

pandas/core/algorithms.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
ensure_float64, ensure_int64, ensure_object, ensure_platform_int,
2020
ensure_uint64, is_array_like, is_bool_dtype, is_categorical_dtype,
2121
is_complex_dtype, is_datetime64_any_dtype, is_datetime64tz_dtype,
22-
is_datetimelike, is_datetimetz, is_extension_array_dtype, is_float_dtype,
22+
is_datetimelike, is_extension_array_dtype, is_float_dtype,
2323
is_integer_dtype, is_interval_dtype, is_list_like, is_numeric_dtype,
2424
is_object_dtype, is_period_dtype, is_scalar, is_signed_integer_dtype,
2525
is_sparse, is_timedelta64_dtype, is_unsigned_integer_dtype,
@@ -1581,7 +1581,7 @@ def take_nd(arr, indexer, axis=0, out=None, fill_value=np.nan, mask_info=None,
15811581
# dispatch to internal type takes
15821582
if is_extension_array_dtype(arr):
15831583
return arr.take(indexer, fill_value=fill_value, allow_fill=allow_fill)
1584-
elif is_datetimetz(arr):
1584+
elif is_datetime64tz_dtype(arr):
15851585
return arr.take(indexer, fill_value=fill_value, allow_fill=allow_fill)
15861586
elif is_interval_dtype(arr):
15871587
return arr.take(indexer, fill_value=fill_value, allow_fill=allow_fill)

pandas/core/arrays/datetimes.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -1458,15 +1458,18 @@ def maybe_convert_dtype(data, copy):
14581458
"""
14591459
Convert data based on dtype conventions, issuing deprecation warnings
14601460
or errors where appropriate.
1461-
Parameters
1461+
1462+
Parameters
14621463
----------
14631464
data : np.ndarray or pd.Index
14641465
copy : bool
1465-
Returns
1466+
1467+
Returns
14661468
-------
14671469
data : np.ndarray or pd.Index
14681470
copy : bool
1469-
Raises
1471+
1472+
Raises
14701473
------
14711474
TypeError : PeriodDType data is passed
14721475
"""

pandas/core/dtypes/cast.py

+8-36
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
ensure_int8, ensure_int16, ensure_int32, ensure_int64, ensure_object,
1414
is_bool, is_bool_dtype, is_categorical_dtype, is_complex, is_complex_dtype,
1515
is_datetime64_dtype, is_datetime64_ns_dtype, is_datetime64tz_dtype,
16-
is_datetime_or_timedelta_dtype, is_datetimelike, is_datetimetz,
17-
is_dtype_equal, is_extension_array_dtype, is_extension_type, is_float,
18-
is_float_dtype, is_integer, is_integer_dtype, is_object_dtype, is_scalar,
19-
is_string_dtype, is_timedelta64_dtype, is_timedelta64_ns_dtype,
20-
is_unsigned_integer_dtype, pandas_dtype)
16+
is_datetime_or_timedelta_dtype, is_datetimelike, is_dtype_equal,
17+
is_extension_array_dtype, is_extension_type, is_float, is_float_dtype,
18+
is_integer, is_integer_dtype, is_object_dtype, is_scalar, is_string_dtype,
19+
is_timedelta64_dtype, is_timedelta64_ns_dtype, is_unsigned_integer_dtype,
20+
pandas_dtype)
2121
from .dtypes import (
2222
DatetimeTZDtype, ExtensionDtype, PandasExtensionDtype, PeriodDtype)
2323
from .generic import ABCDatetimeIndex, ABCPeriodIndex, ABCSeries
@@ -267,7 +267,7 @@ def maybe_promote(dtype, fill_value=np.nan):
267267
fill_value = tslibs.Timestamp(fill_value).value
268268
elif issubclass(dtype.type, np.timedelta64):
269269
fill_value = tslibs.Timedelta(fill_value).value
270-
elif is_datetimetz(dtype):
270+
elif is_datetime64tz_dtype(dtype):
271271
if isna(fill_value):
272272
fill_value = iNaT
273273
elif is_extension_array_dtype(dtype) and isna(fill_value):
@@ -310,7 +310,7 @@ def maybe_promote(dtype, fill_value=np.nan):
310310
# in case we have a string that looked like a number
311311
if is_extension_array_dtype(dtype):
312312
pass
313-
elif is_datetimetz(dtype):
313+
elif is_datetime64tz_dtype(dtype):
314314
pass
315315
elif issubclass(np.dtype(dtype).type, string_types):
316316
dtype = np.object_
@@ -546,34 +546,6 @@ def invalidate_string_dtypes(dtype_set):
546546
raise TypeError("string dtypes are not allowed, use 'object' instead")
547547

548548

549-
def maybe_convert_string_to_object(values):
550-
"""
551-
552-
Convert string-like and string-like array to convert object dtype.
553-
This is to avoid numpy to handle the array as str dtype.
554-
"""
555-
if isinstance(values, string_types):
556-
values = np.array([values], dtype=object)
557-
elif (isinstance(values, np.ndarray) and
558-
issubclass(values.dtype.type, (np.string_, np.unicode_))):
559-
values = values.astype(object)
560-
return values
561-
562-
563-
def maybe_convert_scalar(values):
564-
"""
565-
Convert a python scalar to the appropriate numpy dtype if possible
566-
This avoids numpy directly converting according to platform preferences
567-
"""
568-
if is_scalar(values):
569-
dtype, values = infer_dtype_from_scalar(values)
570-
try:
571-
values = dtype(values)
572-
except TypeError:
573-
pass
574-
return values
575-
576-
577549
def coerce_indexer_dtype(indexer, categories):
578550
""" coerce the indexer input array to the smallest dtype possible """
579551
length = len(categories)
@@ -1188,7 +1160,7 @@ def construct_1d_arraylike_from_scalar(value, length, dtype):
11881160
np.ndarray / pandas type of length, filled with value
11891161
11901162
"""
1191-
if is_datetimetz(dtype):
1163+
if is_datetime64tz_dtype(dtype):
11921164
from pandas import DatetimeIndex
11931165
subarr = DatetimeIndex([value] * length, dtype=dtype)
11941166
elif is_categorical_dtype(dtype):

pandas/core/dtypes/common.py

+17-76
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
""" common type operations """
2+
import warnings
3+
24
import numpy as np
35

46
from pandas._libs import algos, lib
@@ -287,6 +289,8 @@ def is_datetimetz(arr):
287289
Check whether an array-like is a datetime array-like with a timezone
288290
component in its dtype.
289291
292+
.. deprecated:: 0.24.0
293+
290294
Parameters
291295
----------
292296
arr : array-like
@@ -320,12 +324,10 @@ def is_datetimetz(arr):
320324
True
321325
"""
322326

323-
# TODO: do we need this function?
324-
# It seems like a repeat of is_datetime64tz_dtype.
325-
326-
return ((isinstance(arr, ABCDatetimeIndex) and
327-
getattr(arr, 'tz', None) is not None) or
328-
is_datetime64tz_dtype(arr))
327+
warnings.warn("'is_datetimetz' is deprecated and will be removed in a "
328+
"future version. Use 'is_datetime64tz_dtype' instead.",
329+
FutureWarning, stacklevel=2)
330+
return is_datetime64tz_dtype(arr)
329331

330332

331333
def is_offsetlike(arr_or_obj):
@@ -363,6 +365,8 @@ def is_period(arr):
363365
"""
364366
Check whether an array-like is a periodical index.
365367
368+
.. deprecated:: 0.24.0
369+
366370
Parameters
367371
----------
368372
arr : array-like
@@ -382,8 +386,10 @@ def is_period(arr):
382386
True
383387
"""
384388

385-
# TODO: do we need this function?
386-
# It seems like a repeat of is_period_arraylike.
389+
warnings.warn("'is_period' is deprecated and will be removed in a future "
390+
"version. Use 'is_period_dtype' or is_period_arraylike' "
391+
"instead.", FutureWarning, stacklevel=2)
392+
387393
return isinstance(arr, ABCPeriodIndex) or is_period_arraylike(arr)
388394

389395

@@ -743,8 +749,7 @@ def is_datetimelike(arr):
743749

744750
return (is_datetime64_dtype(arr) or is_datetime64tz_dtype(arr) or
745751
is_timedelta64_dtype(arr) or
746-
isinstance(arr, ABCPeriodIndex) or
747-
is_datetimetz(arr))
752+
isinstance(arr, ABCPeriodIndex))
748753

749754

750755
def is_dtype_equal(source, target):
@@ -1050,54 +1055,6 @@ def is_int64_dtype(arr_or_dtype):
10501055
return issubclass(tipo, np.int64)
10511056

10521057

1053-
def is_int_or_datetime_dtype(arr_or_dtype):
1054-
"""
1055-
Check whether the provided array or dtype is of an
1056-
integer, timedelta64, or datetime64 dtype.
1057-
1058-
Parameters
1059-
----------
1060-
arr_or_dtype : array-like
1061-
The array or dtype to check.
1062-
1063-
Returns
1064-
-------
1065-
boolean : Whether or not the array or dtype is of an
1066-
integer, timedelta64, or datetime64 dtype.
1067-
1068-
Examples
1069-
--------
1070-
>>> is_int_or_datetime_dtype(str)
1071-
False
1072-
>>> is_int_or_datetime_dtype(int)
1073-
True
1074-
>>> is_int_or_datetime_dtype(float)
1075-
False
1076-
>>> is_int_or_datetime_dtype(np.uint64)
1077-
True
1078-
>>> is_int_or_datetime_dtype(np.datetime64)
1079-
True
1080-
>>> is_int_or_datetime_dtype(np.timedelta64)
1081-
True
1082-
>>> is_int_or_datetime_dtype(np.array(['a', 'b']))
1083-
False
1084-
>>> is_int_or_datetime_dtype(pd.Series([1, 2]))
1085-
True
1086-
>>> is_int_or_datetime_dtype(np.array([], dtype=np.timedelta64))
1087-
True
1088-
>>> is_int_or_datetime_dtype(np.array([], dtype=np.datetime64))
1089-
True
1090-
>>> is_int_or_datetime_dtype(pd.Index([1, 2.])) # float
1091-
False
1092-
"""
1093-
1094-
if arr_or_dtype is None:
1095-
return False
1096-
tipo = _get_dtype_type(arr_or_dtype)
1097-
return (issubclass(tipo, np.integer) or
1098-
issubclass(tipo, (np.datetime64, np.timedelta64)))
1099-
1100-
11011058
def is_datetime64_any_dtype(arr_or_dtype):
11021059
"""
11031060
Check whether the provided array or dtype is of the datetime64 dtype.
@@ -1619,22 +1576,6 @@ def is_float_dtype(arr_or_dtype):
16191576
return issubclass(tipo, np.floating)
16201577

16211578

1622-
def is_floating_dtype(arr_or_dtype):
1623-
"""Check whether the provided array or dtype is an instance of
1624-
numpy's float dtype.
1625-
1626-
.. deprecated:: 0.20.0
1627-
1628-
Unlike, `is_float_dtype`, this check is a lot stricter, as it requires
1629-
`isinstance` of `np.floating` and not `issubclass`.
1630-
"""
1631-
1632-
if arr_or_dtype is None:
1633-
return False
1634-
tipo = _get_dtype_type(arr_or_dtype)
1635-
return isinstance(tipo, np.floating)
1636-
1637-
16381579
def is_bool_dtype(arr_or_dtype):
16391580
"""
16401581
Check whether the provided array or dtype is of a boolean dtype.
@@ -1758,7 +1699,7 @@ def is_extension_type(arr):
17581699
return True
17591700
elif is_sparse(arr):
17601701
return True
1761-
elif is_datetimetz(arr):
1702+
elif is_datetime64tz_dtype(arr):
17621703
return True
17631704
return False
17641705

@@ -1991,7 +1932,7 @@ def _get_dtype_from_object(dtype):
19911932
return dtype
19921933
elif is_categorical(dtype):
19931934
return CategoricalDtype().type
1994-
elif is_datetimetz(dtype):
1935+
elif is_datetime64tz_dtype(dtype):
19951936
return DatetimeTZDtype(dtype).type
19961937
elif isinstance(dtype, np.dtype): # dtype object
19971938
try:

pandas/core/dtypes/concat.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from pandas.core.dtypes.common import (
1010
_NS_DTYPE, _TD_DTYPE, is_bool_dtype, is_categorical_dtype,
11-
is_datetime64_dtype, is_datetimetz, is_dtype_equal,
11+
is_datetime64_dtype, is_datetime64tz_dtype, is_dtype_equal,
1212
is_extension_array_dtype, is_interval_dtype, is_object_dtype,
1313
is_period_dtype, is_sparse, is_timedelta64_dtype)
1414
from pandas.core.dtypes.generic import (
@@ -39,7 +39,7 @@ def get_dtype_kinds(l):
3939
typ = 'sparse'
4040
elif isinstance(arr, ABCRangeIndex):
4141
typ = 'range'
42-
elif is_datetimetz(arr):
42+
elif is_datetime64tz_dtype(arr):
4343
# if to_concat contains different tz,
4444
# the result must be object dtype
4545
typ = str(arr.dtype)

pandas/core/frame.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
is_object_dtype,
5555
is_extension_type,
5656
is_extension_array_dtype,
57-
is_datetimetz,
57+
is_datetime64tz_dtype,
5858
is_datetime64_any_dtype,
5959
is_bool_dtype,
6060
is_integer_dtype,
@@ -542,7 +542,8 @@ def _get_axes(N, K, index=index, columns=columns):
542542
index, columns = _get_axes(len(values), 1)
543543
return _arrays_to_mgr([values], columns, index, columns,
544544
dtype=dtype)
545-
elif (is_datetimetz(values) or is_extension_array_dtype(values)):
545+
elif (is_datetime64tz_dtype(values) or
546+
is_extension_array_dtype(values)):
546547
# GH19157
547548
if columns is None:
548549
columns = [0]

0 commit comments

Comments
 (0)