From 4185f4bb7d4d1be132bc1d51ff0618ee8390e2ae Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 12 Apr 2023 10:44:19 -0700 Subject: [PATCH 1/2] DEPR: unused keywords in DTI/TDI construtors --- doc/source/whatsnew/v2.1.0.rst | 2 ++ pandas/core/indexes/datetimes.py | 26 +++++++++++++++++-- pandas/core/indexes/timedeltas.py | 15 ++++++++++- .../indexes/datetimes/test_constructors.py | 10 +++++++ .../indexes/timedeltas/test_constructors.py | 5 ++++ 5 files changed, 55 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index c7b3335a8c774..64e5dd47076e5 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -232,6 +232,8 @@ Deprecations - 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`) +- Deprecated unused "closed" and "normalize" keywords in the :class:`DatetimeIndex` constructor (:issue:`??`) +- Deprecated unused "closed" keyword in the :class:`TimedeltaIndex` constructor (:issue:`??`) - .. --------------------------------------------------------------------------- diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 3cf84804e0ae1..16aa6683010ea 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -29,6 +29,7 @@ cache_readonly, doc, ) +from pandas.util._exceptions import find_stack_level from pandas.core.dtypes.common import ( is_datetime64_dtype, @@ -152,9 +153,15 @@ class DatetimeIndex(DatetimeTimedeltaMixin): Set the Timezone of the data. normalize : bool, default False Normalize start/end dates to midnight before generating date range. + + .. deprecated:: 2.1.0 + closed : {'left', 'right'}, optional Set whether to include `start` and `end` that are on the boundary. The default includes boundary points on either end. + + .. deprecated:: 2.1.0 + ambiguous : 'infer', bool-ndarray, 'NaT', default 'raise' When clocks moved backward due to DST, ambiguous times may arise. For example in Central European Time (UTC+01), when going from 03:00 @@ -310,8 +317,8 @@ def __new__( data=None, freq: Frequency | lib.NoDefault = lib.no_default, tz=lib.no_default, - normalize: bool = False, - closed=None, + normalize: bool | lib.NoDefault = lib.no_default, + closed=lib.no_default, ambiguous: TimeAmbiguous = "raise", dayfirst: bool = False, yearfirst: bool = False, @@ -319,6 +326,21 @@ def __new__( copy: bool = False, name: Hashable = None, ) -> Self: + if closed is not lib.no_default: + warnings.warn( + f"The 'closed' keyword in {cls.__name__} construction is " + "deprecated and will be removed in a future version.", + FutureWarning, + stacklevel=find_stack_level(), + ) + if normalize is not lib.no_default: + warnings.warn( + f"The 'normalize' keyword in {cls.__name__} construction is " + "deprecated and will be removed in a future version.", + FutureWarning, + stacklevel=find_stack_level(), + ) + if is_scalar(data): cls._raise_scalar_data_error(data) diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index de5e5c61e96f1..afc90fb409709 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -2,6 +2,7 @@ from __future__ import annotations from typing import TYPE_CHECKING +import warnings from pandas._libs import ( index as libindex, @@ -12,6 +13,7 @@ Timedelta, to_offset, ) +from pandas.util._exceptions import find_stack_level from pandas.core.dtypes.common import ( is_dtype_equal, @@ -67,6 +69,9 @@ class TimedeltaIndex(DatetimeTimedeltaMixin): One of pandas date offset strings or corresponding objects. The string 'infer' can be passed in order to set the frequency of the index as the inferred frequency upon creation. + dtype : numpy.dtype or str, default None + Valid NumPy dtypes are timedelta64[ns]’, timedelta64[us]’, + timedelta64[ms]’, and timedelta64[s]’. copy : bool Make a copy of input ndarray. name : object @@ -132,11 +137,19 @@ def __new__( data=None, unit=None, freq=lib.no_default, - closed=None, + closed=lib.no_default, dtype=None, copy: bool = False, name=None, ): + if closed is not lib.no_default: + warnings.warn( + f"The 'closed' keyword in {cls.__name__} construction is " + "deprecated and will be removed in a future version.", + FutureWarning, + stacklevel=find_stack_level(), + ) + name = maybe_extract_name(name, data, cls) if is_scalar(data): diff --git a/pandas/tests/indexes/datetimes/test_constructors.py b/pandas/tests/indexes/datetimes/test_constructors.py index 82f75d9ff80e5..954bd8aeb2cde 100644 --- a/pandas/tests/indexes/datetimes/test_constructors.py +++ b/pandas/tests/indexes/datetimes/test_constructors.py @@ -33,6 +33,16 @@ class TestDatetimeIndex: + def test_closed_deprecated(self): + msg = "The 'closed' keyword" + with tm.assert_produces_warning(FutureWarning, match=msg): + DatetimeIndex([], closed=True) + + def test_normalize_deprecated(self): + msg = "The 'normalize' keyword" + with tm.assert_produces_warning(FutureWarning, match=msg): + DatetimeIndex([], normalize=True) + def test_from_dt64_unsupported_unit(self): # GH#49292 val = np.datetime64(1, "D") diff --git a/pandas/tests/indexes/timedeltas/test_constructors.py b/pandas/tests/indexes/timedeltas/test_constructors.py index 4e817ee708614..127074f7d3aae 100644 --- a/pandas/tests/indexes/timedeltas/test_constructors.py +++ b/pandas/tests/indexes/timedeltas/test_constructors.py @@ -18,6 +18,11 @@ class TestTimedeltaIndex: + def test_closed_deprecated(self): + msg = "The 'closed' keyword" + with tm.assert_produces_warning(FutureWarning, match=msg): + TimedeltaIndex([], closed=True) + def test_array_of_dt64_nat_raises(self): # GH#39462 nat = np.datetime64("NaT", "ns") From a3d150542c08c95b811708ed95caac4cca42f2a0 Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 12 Apr 2023 10:46:14 -0700 Subject: [PATCH 2/2] GH refs --- doc/source/whatsnew/v2.1.0.rst | 4 ++-- pandas/core/indexes/datetimes.py | 2 ++ pandas/core/indexes/timedeltas.py | 1 + pandas/tests/indexes/datetimes/test_constructors.py | 2 ++ pandas/tests/indexes/timedeltas/test_constructors.py | 1 + 5 files changed, 8 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 64e5dd47076e5..9b5cba1e1ee05 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -232,8 +232,8 @@ Deprecations - 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`) -- Deprecated unused "closed" and "normalize" keywords in the :class:`DatetimeIndex` constructor (:issue:`??`) -- Deprecated unused "closed" keyword in the :class:`TimedeltaIndex` constructor (:issue:`??`) +- Deprecated unused "closed" and "normalize" keywords in the :class:`DatetimeIndex` constructor (:issue:`52628`) +- Deprecated unused "closed" keyword in the :class:`TimedeltaIndex` constructor (:issue:`52628`) - .. --------------------------------------------------------------------------- diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 16aa6683010ea..7de1972de5e4a 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -327,6 +327,7 @@ def __new__( name: Hashable = None, ) -> Self: if closed is not lib.no_default: + # GH#52628 warnings.warn( f"The 'closed' keyword in {cls.__name__} construction is " "deprecated and will be removed in a future version.", @@ -334,6 +335,7 @@ def __new__( stacklevel=find_stack_level(), ) if normalize is not lib.no_default: + # GH#52628 warnings.warn( f"The 'normalize' keyword in {cls.__name__} construction is " "deprecated and will be removed in a future version.", diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index afc90fb409709..41409bb05a41a 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -143,6 +143,7 @@ def __new__( name=None, ): if closed is not lib.no_default: + # GH#52628 warnings.warn( f"The 'closed' keyword in {cls.__name__} construction is " "deprecated and will be removed in a future version.", diff --git a/pandas/tests/indexes/datetimes/test_constructors.py b/pandas/tests/indexes/datetimes/test_constructors.py index 954bd8aeb2cde..f657cc71e6346 100644 --- a/pandas/tests/indexes/datetimes/test_constructors.py +++ b/pandas/tests/indexes/datetimes/test_constructors.py @@ -34,11 +34,13 @@ class TestDatetimeIndex: def test_closed_deprecated(self): + # GH#52628 msg = "The 'closed' keyword" with tm.assert_produces_warning(FutureWarning, match=msg): DatetimeIndex([], closed=True) def test_normalize_deprecated(self): + # GH#52628 msg = "The 'normalize' keyword" with tm.assert_produces_warning(FutureWarning, match=msg): DatetimeIndex([], normalize=True) diff --git a/pandas/tests/indexes/timedeltas/test_constructors.py b/pandas/tests/indexes/timedeltas/test_constructors.py index 127074f7d3aae..a3de699a9f58d 100644 --- a/pandas/tests/indexes/timedeltas/test_constructors.py +++ b/pandas/tests/indexes/timedeltas/test_constructors.py @@ -19,6 +19,7 @@ class TestTimedeltaIndex: def test_closed_deprecated(self): + # GH#52628 msg = "The 'closed' keyword" with tm.assert_produces_warning(FutureWarning, match=msg): TimedeltaIndex([], closed=True)