diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index fca355069ae74..17bd833cb746f 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -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`) - .. --------------------------------------------------------------------------- diff --git a/pandas/conftest.py b/pandas/conftest.py index 750fff1b1aea7..7773d8de37705 100644 --- a/pandas/conftest.py +++ b/pandas/conftest.py @@ -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"), ( diff --git a/pandas/core/dtypes/common.py b/pandas/core/dtypes/common.py index ab8dbb136ec22..183fd5b2652b6 100644 --- a/pandas/core/dtypes/common.py +++ b/pandas/core/dtypes/common.py @@ -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 " + "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 @@ -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 @@ -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: diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index b9902572c5846..965c0ba9be1e3 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -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, @@ -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 @@ -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 @@ -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 diff --git a/pandas/core/reshape/tile.py b/pandas/core/reshape/tile.py index dbf5fe8e6fd95..bb5c943c6318e 100644 --- a/pandas/core/reshape/tile.py +++ b/pandas/core/reshape/tile.py @@ -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, @@ -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) diff --git a/pandas/tests/base/test_unique.py b/pandas/tests/base/test_unique.py index 624d3d68d37fd..ab49dd3052d31 100644 --- a/pandas/tests/base/test_unique.py +++ b/pandas/tests/base/test_unique.py @@ -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 @@ -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: @@ -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) diff --git a/pandas/tests/dtypes/test_common.py b/pandas/tests/dtypes/test_common.py index 6702d219caa83..2c7651e0489d8 100644 --- a/pandas/tests/dtypes/test_common.py +++ b/pandas/tests/dtypes/test_common.py @@ -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): @@ -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(): @@ -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(): @@ -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(): diff --git a/pandas/tests/dtypes/test_dtypes.py b/pandas/tests/dtypes/test_dtypes.py index e0ae572e17a8e..09e100c660ddf 100644 --- a/pandas/tests/dtypes/test_dtypes.py +++ b/pandas/tests/dtypes/test_dtypes.py @@ -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) @@ -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"]) @@ -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")] @@ -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", @@ -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() @@ -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) diff --git a/pandas/tests/dtypes/test_inference.py b/pandas/tests/dtypes/test_inference.py index 650eb033dcd9e..9358b44c243bc 100644 --- a/pandas/tests/dtypes/test_inference.py +++ b/pandas/tests/dtypes/test_inference.py @@ -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) @@ -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) diff --git a/pandas/tests/frame/indexing/test_setitem.py b/pandas/tests/frame/indexing/test_setitem.py index f31b27897bf82..049874a4dd46f 100644 --- a/pandas/tests/frame/indexing/test_setitem.py +++ b/pandas/tests/frame/indexing/test_setitem.py @@ -6,10 +6,7 @@ import pandas.util._test_decorators as td from pandas.core.dtypes.base import _registry as ea_registry -from pandas.core.dtypes.common import ( - is_interval_dtype, - is_object_dtype, -) +from pandas.core.dtypes.common import is_object_dtype from pandas.core.dtypes.dtypes import ( CategoricalDtype, DatetimeTZDtype, @@ -484,14 +481,14 @@ def test_setitem_intervals(self): df["F"] = ser.astype(object) assert isinstance(df["B"].dtype, CategoricalDtype) - assert is_interval_dtype(df["B"].cat.categories) + assert isinstance(df["B"].cat.categories.dtype, IntervalDtype) assert isinstance(df["D"].dtype, CategoricalDtype) - assert is_interval_dtype(df["D"].cat.categories) + assert isinstance(df["D"].cat.categories.dtype, IntervalDtype) # These go through the Series constructor and so get inferred back # to IntervalDtype - assert is_interval_dtype(df["C"]) - assert is_interval_dtype(df["E"]) + assert isinstance(df["C"].dtype, IntervalDtype) + assert isinstance(df["E"].dtype, IntervalDtype) # But the Series constructor doesn't do inference on Series objects, # so setting df["F"] doesn't get cast back to IntervalDtype diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index 272166e25e8a5..73de3ee60339a 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -8,16 +8,14 @@ from pandas._libs.tslibs import Timestamp -from pandas.core.dtypes.common import ( - is_datetime64tz_dtype, - is_integer_dtype, -) +from pandas.core.dtypes.common import is_integer_dtype from pandas.core.dtypes.dtypes import CategoricalDtype import pandas as pd from pandas import ( CategoricalIndex, DatetimeIndex, + DatetimeTZDtype, Index, IntervalIndex, MultiIndex, @@ -211,7 +209,7 @@ def test_ensure_copied_data(self, index): index_type = type(index) result = index_type(index.values, copy=True, **init_kwargs) - if is_datetime64tz_dtype(index.dtype): + if isinstance(index.dtype, DatetimeTZDtype): result = result.tz_localize("UTC").tz_convert(index.tz) if isinstance(index, (DatetimeIndex, TimedeltaIndex)): index = index._with_freq(None) diff --git a/pandas/tests/indexes/test_setops.py b/pandas/tests/indexes/test_setops.py index 1be4a1835de09..6a18bc4aaf856 100644 --- a/pandas/tests/indexes/test_setops.py +++ b/pandas/tests/indexes/test_setops.py @@ -12,6 +12,7 @@ from pandas import ( CategoricalIndex, + DatetimeTZDtype, Index, MultiIndex, RangeIndex, @@ -21,7 +22,6 @@ import pandas._testing as tm from pandas.api.types import ( is_bool_dtype, - is_datetime64tz_dtype, is_signed_integer_dtype, pandas_dtype, ) @@ -193,7 +193,7 @@ def test_intersection_base(self, index): intersect = first.intersection(second) assert tm.equalContents(intersect, second) - if is_datetime64tz_dtype(index.dtype): + if isinstance(index.dtype, DatetimeTZDtype): # The second.values below will drop tz, so the rest of this test # is not applicable. return @@ -220,7 +220,7 @@ def test_union_base(self, index): union = first.union(second) assert tm.equalContents(union, everything) - if is_datetime64tz_dtype(index.dtype): + if isinstance(index.dtype, DatetimeTZDtype): # The second.values below will drop tz, so the rest of this test # is not applicable. return diff --git a/pandas/tests/io/test_sql.py b/pandas/tests/io/test_sql.py index cd1f0ce6fcfd8..d0b7492f8d9ba 100644 --- a/pandas/tests/io/test_sql.py +++ b/pandas/tests/io/test_sql.py @@ -36,14 +36,12 @@ from pandas._libs import lib import pandas.util._test_decorators as td -from pandas.core.dtypes.common import ( - is_datetime64_dtype, - is_datetime64tz_dtype, -) +from pandas.core.dtypes.common import is_datetime64_dtype import pandas as pd from pandas import ( DataFrame, + DatetimeTZDtype, Index, MultiIndex, Series, @@ -1935,7 +1933,7 @@ def check(col): # "2000-06-01 07:00:00" assert col[1] == Timestamp("2000-06-01 07:00:00") - elif is_datetime64tz_dtype(col.dtype): + elif isinstance(col.dtype, DatetimeTZDtype): assert str(col.dt.tz) == "UTC" # "2000-01-01 00:00:00-08:00" should convert to @@ -1966,7 +1964,7 @@ def check(col): # even with the same versions of psycopg2 & sqlalchemy, possibly a # Postgresql server version difference col = df.DateColWithTz - assert is_datetime64tz_dtype(col.dtype) + assert isinstance(col.dtype, DatetimeTZDtype) df = read_sql_query( "select * from types", self.conn, parse_dates=["DateColWithTz"] @@ -1976,7 +1974,7 @@ def check(col): pytest.mark.xfail(reason="no column with datetime with time zone") ) col = df.DateColWithTz - assert is_datetime64tz_dtype(col.dtype) + assert isinstance(col.dtype, DatetimeTZDtype) assert str(col.dt.tz) == "UTC" check(df.DateColWithTz) @@ -1985,11 +1983,11 @@ def check(col): ignore_index=True, ) col = df.DateColWithTz - assert is_datetime64tz_dtype(col.dtype) + assert isinstance(col.dtype, DatetimeTZDtype) assert str(col.dt.tz) == "UTC" expected = sql.read_sql_table("types", self.conn) col = expected.DateColWithTz - assert is_datetime64tz_dtype(col.dtype) + assert isinstance(col.dtype, DatetimeTZDtype) tm.assert_series_equal(df.DateColWithTz, expected.DateColWithTz) # xref #7139 diff --git a/pandas/tests/series/test_arithmetic.py b/pandas/tests/series/test_arithmetic.py index d0d75ded14693..948e1e626aa5e 100644 --- a/pandas/tests/series/test_arithmetic.py +++ b/pandas/tests/series/test_arithmetic.py @@ -11,14 +11,12 @@ from pandas._libs.tslibs import IncompatibleFrequency -from pandas.core.dtypes.common import ( - is_datetime64_dtype, - is_datetime64tz_dtype, -) +from pandas.core.dtypes.common import is_datetime64_dtype import pandas as pd from pandas import ( Categorical, + DatetimeTZDtype, Index, Series, Timedelta, @@ -907,7 +905,7 @@ def test_none_comparison(request, series_with_simple_index): assert result.iat[0] assert result.iat[1] - if is_datetime64_dtype(series.dtype) or is_datetime64tz_dtype(series.dtype): + if is_datetime64_dtype(series.dtype) or isinstance(series.dtype, DatetimeTZDtype): # Following DatetimeIndex (and Timestamp) convention, # inequality comparisons with Series[datetime64] raise msg = "Invalid comparison" diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index 77ff5af0e6f5d..8e883f9cec8ea 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -17,10 +17,7 @@ from pandas.errors import IntCastingNaNError import pandas.util._test_decorators as td -from pandas.core.dtypes.common import ( - is_categorical_dtype, - is_datetime64tz_dtype, -) +from pandas.core.dtypes.common import is_categorical_dtype from pandas.core.dtypes.dtypes import CategoricalDtype import pandas as pd @@ -28,6 +25,7 @@ Categorical, DataFrame, DatetimeIndex, + DatetimeTZDtype, Index, Interval, IntervalIndex, @@ -1104,7 +1102,7 @@ def test_constructor_with_datetime_tz(self): s = Series(dr) assert s.dtype.name == "datetime64[ns, US/Eastern]" assert s.dtype == "datetime64[ns, US/Eastern]" - assert is_datetime64tz_dtype(s.dtype) + assert isinstance(s.dtype, DatetimeTZDtype) assert "datetime64[ns, US/Eastern]" in str(s) # export diff --git a/pandas/tseries/frequencies.py b/pandas/tseries/frequencies.py index 2e960fdcb7611..6f1f58eaf2c39 100644 --- a/pandas/tseries/frequencies.py +++ b/pandas/tseries/frequencies.py @@ -32,10 +32,12 @@ from pandas.core.dtypes.common import ( is_datetime64_dtype, - is_datetime64tz_dtype, is_timedelta64_dtype, ) -from pandas.core.dtypes.dtypes import PeriodDtype +from pandas.core.dtypes.dtypes import ( + DatetimeTZDtype, + PeriodDtype, +) from pandas.core.dtypes.generic import ( ABCIndex, ABCSeries, @@ -149,7 +151,7 @@ def infer_freq( values = index._values if not ( is_datetime64_dtype(values) - or is_datetime64tz_dtype(values) + or isinstance(values.dtype, DatetimeTZDtype) or is_timedelta64_dtype(values) or values.dtype == object ):