Skip to content

DEPR: is_datetime64tz_dtype, is_interval_dtype #52607

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 5 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ Deprecations
- Deprecated ``freq`` parameter in :class:`PeriodArray` constructor, pass ``dtype`` instead (:issue:`52462`)
- Deprecated :func:`is_categorical_dtype`, use ``isinstance(obj.dtype, pd.CategoricalDtype)`` instead (:issue:`52527`)
- Deprecated :func:`is_int64_dtype`, check ``dtype == np.dtype(np.int64)`` instead (:issue:`52564`)
- Deprecated :func:`is_interval_dtype`, check ``isinstance(dtype, pd.IntervalDtype)`` instead (:issue:`52607`)
- Deprecated :func:`is_datetime64tz_dtype`, check ``isinstance(dtype, pd.DatetimeTZDtype)`` instead (:issue:`52607`)
-

.. ---------------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ def pytest_collection_modifyitems(items, config) -> None:
# Each entry specifies (path, message) - see the ignore_doctest_warning function
ignored_doctest_warnings = [
("is_int64_dtype", "is_int64_dtype is deprecated"),
("is_interval_dtype", "is_interval_dtype is deprecated"),
("is_datetime64tz_dtype", "is_datetime64tz_dtype is deprecated"),
# Docstring divides by zero to show behavior difference
("missing.mask_zero_div_zero", "divide by zero encountered"),
(
Expand Down
23 changes: 22 additions & 1 deletion pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,13 @@ def is_datetime64tz_dtype(arr_or_dtype) -> bool:
>>> is_datetime64tz_dtype(s)
True
"""
# GH#52607
warnings.warn(
"is_datetime64tz_dtype is deprecated and will be removed in a future "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be useful if we want to check whether it's a numpy or arrow tz type, but idk if we have a use case for that yet.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could. I think the fact that it is ambiguous (based on the name) is all the more reason to deprecate it

"version. Check `isinstance(dtype, pd.DatetimeTZDtype)` instead.",
FutureWarning,
stacklevel=find_stack_level(),
)
if isinstance(arr_or_dtype, DatetimeTZDtype):
# GH#33400 fastpath for dtype object
# GH 34986
Expand Down Expand Up @@ -431,6 +438,13 @@ def is_interval_dtype(arr_or_dtype) -> bool:
>>> is_interval_dtype(pd.IntervalIndex([interval]))
True
"""
# GH#52607
warnings.warn(
"is_interval_dtype is deprecated and will be removed in a future version. "
"Use `isinstance(dtype, pd.IntervalDtype)` instead",
FutureWarning,
stacklevel=find_stack_level(),
)
if isinstance(arr_or_dtype, ExtensionDtype):
# GH#33400 fastpath for dtype object
return arr_or_dtype.type is Interval
Expand Down Expand Up @@ -854,7 +868,14 @@ def is_datetime64_any_dtype(arr_or_dtype) -> bool:

if arr_or_dtype is None:
return False
return is_datetime64_dtype(arr_or_dtype) or is_datetime64tz_dtype(arr_or_dtype)

try:
tipo = get_dtype(arr_or_dtype)
except TypeError:
return False
return (isinstance(tipo, np.dtype) and tipo.kind == "M") or isinstance(
tipo, DatetimeTZDtype
)


def is_datetime64_ns_dtype(arr_or_dtype) -> bool:
Expand Down
10 changes: 6 additions & 4 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
)
from pandas.core.dtypes.common import (
ensure_platform_int,
is_datetime64tz_dtype,
is_datetime_or_timedelta_dtype,
is_dtype_equal,
is_float,
Expand All @@ -55,7 +54,10 @@
is_object_dtype,
is_scalar,
)
from pandas.core.dtypes.dtypes import IntervalDtype
from pandas.core.dtypes.dtypes import (
DatetimeTZDtype,
IntervalDtype,
)
from pandas.core.dtypes.missing import is_valid_na_for_dtype

from pandas.core.algorithms import unique
Expand Down Expand Up @@ -114,7 +116,7 @@ def _get_next_label(label):
dtype = getattr(label, "dtype", type(label))
if isinstance(label, (Timestamp, Timedelta)):
dtype = "datetime64"
if is_datetime_or_timedelta_dtype(dtype) or is_datetime64tz_dtype(dtype):
if is_datetime_or_timedelta_dtype(dtype) or isinstance(dtype, DatetimeTZDtype):
return label + np.timedelta64(1, "ns")
elif is_integer_dtype(dtype):
return label + 1
Expand All @@ -128,7 +130,7 @@ def _get_prev_label(label):
dtype = getattr(label, "dtype", type(label))
if isinstance(label, (Timestamp, Timedelta)):
dtype = "datetime64"
if is_datetime_or_timedelta_dtype(dtype) or is_datetime64tz_dtype(dtype):
if is_datetime_or_timedelta_dtype(dtype) or isinstance(dtype, DatetimeTZDtype):
return label - np.timedelta64(1, "ns")
elif is_integer_dtype(dtype):
return label - 1
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/reshape/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
ensure_platform_int,
is_bool_dtype,
is_datetime64_dtype,
is_datetime64tz_dtype,
is_datetime_or_timedelta_dtype,
is_integer,
is_list_like,
Expand Down Expand Up @@ -285,7 +284,7 @@ def cut(
raise ValueError("Overlapping IntervalIndex is not accepted.")

else:
if is_datetime64tz_dtype(bins):
if isinstance(getattr(bins, "dtype", None), DatetimeTZDtype):
bins = np.asarray(bins, dtype=DT64NS_DTYPE)
else:
bins = np.asarray(bins)
Expand Down
6 changes: 2 additions & 4 deletions pandas/tests/base/test_unique.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import numpy as np
import pytest

from pandas.core.dtypes.common import is_datetime64tz_dtype

import pandas as pd
import pandas._testing as tm
from pandas.tests.base.common import allow_na_ops
Expand All @@ -21,7 +19,7 @@ def test_unique(index_or_series_obj):
tm.assert_index_equal(result, expected, exact=True)
elif isinstance(obj, pd.Index):
expected = pd.Index(unique_values, dtype=obj.dtype)
if is_datetime64tz_dtype(obj.dtype):
if isinstance(obj.dtype, pd.DatetimeTZDtype):
expected = expected.normalize()
tm.assert_index_equal(result, expected, exact=True)
else:
Expand Down Expand Up @@ -56,7 +54,7 @@ def test_unique_null(null_obj, index_or_series_obj):

if isinstance(obj, pd.Index):
expected = pd.Index(unique_values, dtype=obj.dtype)
if is_datetime64tz_dtype(obj.dtype):
if isinstance(obj.dtype, pd.DatetimeTZDtype):
result = result.normalize()
expected = expected.normalize()
tm.assert_index_equal(result, expected, exact=True)
Expand Down
37 changes: 24 additions & 13 deletions pandas/tests/dtypes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,12 @@ def test_get_dtype_error_catch(func):

msg = f"{func.__name__} is deprecated"
warn = None
if func is com.is_int64_dtype or func is com.is_categorical_dtype:
if (
func is com.is_int64_dtype
or func is com.is_interval_dtype
or func is com.is_datetime64tz_dtype
or func is com.is_categorical_dtype
):
warn = FutureWarning

with tm.assert_produces_warning(warn, match=msg):
Expand Down Expand Up @@ -221,10 +226,12 @@ def test_is_datetime64_dtype():


def test_is_datetime64tz_dtype():
assert not com.is_datetime64tz_dtype(object)
assert not com.is_datetime64tz_dtype([1, 2, 3])
assert not com.is_datetime64tz_dtype(pd.DatetimeIndex([1, 2, 3]))
assert com.is_datetime64tz_dtype(pd.DatetimeIndex(["2000"], tz="US/Eastern"))
msg = "is_datetime64tz_dtype is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
assert not com.is_datetime64tz_dtype(object)
assert not com.is_datetime64tz_dtype([1, 2, 3])
assert not com.is_datetime64tz_dtype(pd.DatetimeIndex([1, 2, 3]))
assert com.is_datetime64tz_dtype(pd.DatetimeIndex(["2000"], tz="US/Eastern"))


def test_custom_ea_kind_M_not_datetime64tz():
Expand All @@ -235,8 +242,10 @@ def kind(self) -> str:
return "M"

not_tz_dtype = NotTZDtype()
assert not com.is_datetime64tz_dtype(not_tz_dtype)
assert not com.needs_i8_conversion(not_tz_dtype)
msg = "is_datetime64tz_dtype is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
assert not com.is_datetime64tz_dtype(not_tz_dtype)
assert not com.needs_i8_conversion(not_tz_dtype)


def test_is_timedelta64_dtype():
Expand Down Expand Up @@ -264,14 +273,16 @@ def test_is_period_dtype():


def test_is_interval_dtype():
assert not com.is_interval_dtype(object)
assert not com.is_interval_dtype([1, 2, 3])
msg = "is_interval_dtype is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
assert not com.is_interval_dtype(object)
assert not com.is_interval_dtype([1, 2, 3])

assert com.is_interval_dtype(IntervalDtype())
assert com.is_interval_dtype(IntervalDtype())

interval = pd.Interval(1, 2, closed="right")
assert not com.is_interval_dtype(interval)
assert com.is_interval_dtype(pd.IntervalIndex([interval]))
interval = pd.Interval(1, 2, closed="right")
assert not com.is_interval_dtype(interval)
assert com.is_interval_dtype(pd.IntervalIndex([interval]))


def test_is_categorical_dtype():
Expand Down
82 changes: 50 additions & 32 deletions pandas/tests/dtypes/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,10 @@ def test_subclass(self):
assert issubclass(type(a), type(b))

def test_compat(self, dtype):
assert is_datetime64tz_dtype(dtype)
assert is_datetime64tz_dtype("datetime64[ns, US/Eastern]")
msg = "is_datetime64tz_dtype is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
assert is_datetime64tz_dtype(dtype)
assert is_datetime64tz_dtype("datetime64[ns, US/Eastern]")
assert is_datetime64_any_dtype(dtype)
assert is_datetime64_any_dtype("datetime64[ns, US/Eastern]")
assert is_datetime64_ns_dtype(dtype)
Expand Down Expand Up @@ -349,25 +351,28 @@ def test_equality(self, dtype):
assert dtype == "M8[ns, US/Eastern]"

def test_basic(self, dtype):
assert is_datetime64tz_dtype(dtype)
msg = "is_datetime64tz_dtype is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
assert is_datetime64tz_dtype(dtype)

dr = date_range("20130101", periods=3, tz="US/Eastern")
s = Series(dr, name="A")

# dtypes
assert is_datetime64tz_dtype(s.dtype)
assert is_datetime64tz_dtype(s)
assert not is_datetime64tz_dtype(np.dtype("float64"))
assert not is_datetime64tz_dtype(1.0)
with tm.assert_produces_warning(FutureWarning, match=msg):
assert is_datetime64tz_dtype(s.dtype)
assert is_datetime64tz_dtype(s)
assert not is_datetime64tz_dtype(np.dtype("float64"))
assert not is_datetime64tz_dtype(1.0)

def test_dst(self):
dr1 = date_range("2013-01-01", periods=3, tz="US/Eastern")
s1 = Series(dr1, name="A")
assert is_datetime64tz_dtype(s1)
assert isinstance(s1.dtype, DatetimeTZDtype)

dr2 = date_range("2013-08-01", periods=3, tz="US/Eastern")
s2 = Series(dr2, name="A")
assert is_datetime64tz_dtype(s2)
assert isinstance(s2.dtype, DatetimeTZDtype)
assert s1.dtype == s2.dtype

@pytest.mark.parametrize("tz", ["UTC", "US/Eastern"])
Expand Down Expand Up @@ -595,7 +600,9 @@ def test_hash_vs_equality(self, dtype):
def test_construction(self, subtype):
i = IntervalDtype(subtype, closed="right")
assert i.subtype == np.dtype("int64")
assert is_interval_dtype(i)
msg = "is_interval_dtype is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
assert is_interval_dtype(i)

@pytest.mark.parametrize(
"subtype", ["interval[int64]", "Interval[int64]", "int64", np.dtype("int64")]
Expand All @@ -616,7 +623,9 @@ def test_construction_generic(self, subtype):
# generic
i = IntervalDtype(subtype)
assert i.subtype is None
assert is_interval_dtype(i)
msg = "is_interval_dtype is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
assert is_interval_dtype(i)

@pytest.mark.parametrize(
"subtype",
Expand Down Expand Up @@ -787,31 +796,35 @@ def test_name_repr_generic(self, subtype):
assert dtype.name == "interval"

def test_basic(self, dtype):
assert is_interval_dtype(dtype)
msg = "is_interval_dtype is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
assert is_interval_dtype(dtype)

ii = IntervalIndex.from_breaks(range(3))
ii = IntervalIndex.from_breaks(range(3))

assert is_interval_dtype(ii.dtype)
assert is_interval_dtype(ii)
assert is_interval_dtype(ii.dtype)
assert is_interval_dtype(ii)

s = Series(ii, name="A")
s = Series(ii, name="A")

assert is_interval_dtype(s.dtype)
assert is_interval_dtype(s)
assert is_interval_dtype(s.dtype)
assert is_interval_dtype(s)

def test_basic_dtype(self):
assert is_interval_dtype("interval[int64, both]")
assert is_interval_dtype(IntervalIndex.from_tuples([(0, 1)]))
assert is_interval_dtype(IntervalIndex.from_breaks(np.arange(4)))
assert is_interval_dtype(
IntervalIndex.from_breaks(date_range("20130101", periods=3))
)
assert not is_interval_dtype("U")
assert not is_interval_dtype("S")
assert not is_interval_dtype("foo")
assert not is_interval_dtype(np.object_)
assert not is_interval_dtype(np.int64)
assert not is_interval_dtype(np.float64)
msg = "is_interval_dtype is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
assert is_interval_dtype("interval[int64, both]")
assert is_interval_dtype(IntervalIndex.from_tuples([(0, 1)]))
assert is_interval_dtype(IntervalIndex.from_breaks(np.arange(4)))
assert is_interval_dtype(
IntervalIndex.from_breaks(date_range("20130101", periods=3))
)
assert not is_interval_dtype("U")
assert not is_interval_dtype("S")
assert not is_interval_dtype("foo")
assert not is_interval_dtype(np.object_)
assert not is_interval_dtype(np.int64)
assert not is_interval_dtype(np.float64)

def test_caching(self):
IntervalDtype.reset_cache()
Expand Down Expand Up @@ -1113,9 +1126,14 @@ def test_is_dtype_no_warning(check):
data = pd.DataFrame({"A": [1, 2]})

warn = None
msg = "is_categorical_dtype is deprecated"
if check is is_categorical_dtype:
msg = f"{check.__name__} is deprecated"
if (
check is is_categorical_dtype
or check is is_interval_dtype
or check is is_datetime64tz_dtype
):
warn = FutureWarning

with tm.assert_produces_warning(warn, match=msg):
check(data)

Expand Down
16 changes: 11 additions & 5 deletions pandas/tests/dtypes/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -1821,6 +1821,8 @@ def test_is_datetime_dtypes(self):
ts = pd.date_range("20130101", periods=3)
tsa = pd.date_range("20130101", periods=3, tz="US/Eastern")

msg = "is_datetime64tz_dtype is deprecated"

assert is_datetime64_dtype("datetime64")
assert is_datetime64_dtype("datetime64[ns]")
assert is_datetime64_dtype(ts)
Expand All @@ -1836,16 +1838,20 @@ def test_is_datetime_dtypes(self):
assert is_datetime64_any_dtype(ts)
assert is_datetime64_any_dtype(tsa)

assert not is_datetime64tz_dtype("datetime64")
assert not is_datetime64tz_dtype("datetime64[ns]")
assert not is_datetime64tz_dtype(ts)
assert is_datetime64tz_dtype(tsa)
with tm.assert_produces_warning(FutureWarning, match=msg):
assert not is_datetime64tz_dtype("datetime64")
assert not is_datetime64tz_dtype("datetime64[ns]")
assert not is_datetime64tz_dtype(ts)
assert is_datetime64tz_dtype(tsa)

@pytest.mark.parametrize("tz", ["US/Eastern", "UTC"])
def test_is_datetime_dtypes_with_tz(self, tz):
dtype = f"datetime64[ns, {tz}]"
assert not is_datetime64_dtype(dtype)
assert is_datetime64tz_dtype(dtype)

msg = "is_datetime64tz_dtype is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
assert is_datetime64tz_dtype(dtype)
assert is_datetime64_ns_dtype(dtype)
assert is_datetime64_any_dtype(dtype)

Expand Down
Loading