From d511565f210e58cea50b4c27e22a46f86b9adf72 Mon Sep 17 00:00:00 2001 From: Brock Date: Thu, 11 Nov 2021 13:06:44 -0800 Subject: [PATCH 1/2] DEPR: PeriodIndex.astype(dt64) --- doc/source/whatsnew/v1.4.0.rst | 2 ++ pandas/core/indexes/period.py | 8 ++++++++ pandas/tests/indexes/period/methods/test_astype.py | 4 +++- pandas/tests/indexes/test_common.py | 3 +++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 99a66c7e5454b..23838b1959c01 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -400,6 +400,8 @@ Other Deprecations - Deprecated casting behavior when setting timezone-aware value(s) into a timezone-aware :class:`Series` or :class:`DataFrame` column when the timezones do not match. Previously this cast to object dtype. In a future version, the values being inserted will be converted to the series or column's existing timezone (:issue:`37605`) - Deprecated casting behavior when passing an item with mismatched-timezone to :meth:`DatetimeIndex.insert`, :meth:`DatetimeIndex.putmask`, :meth:`DatetimeIndex.where` :meth:`DatetimeIndex.fillna`, :meth:`Series.mask`, :meth:`Series.where`, :meth:`Series.fillna`, :meth:`Series.shift`, :meth:`Series.replace`, :meth:`Series.reindex` (and :class:`DataFrame` column analogues). In the past this has cast to object dtype. In a future version, these will cast the passed item to the index or series's timezone (:issue:`37605`) - Deprecated the 'errors' keyword argument in :meth:`Series.where`, :meth:`DataFrame.where`, :meth:`Series.mask`, and meth:`DataFrame.mask`; in a future version the argument will be removed (:issue:`44294`) +- Deprecated :meth:`PeriodIndex.astype` to ``datetime64[ns]`` or ``DatetimeTZDtype``, use ``obj.to_timestamp(how).tz_localize(dtype.tz)`` instead (:issue:`??`) +- .. --------------------------------------------------------------------------- diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py index fd5b5bb7396af..3c980a6885779 100644 --- a/pandas/core/indexes/period.py +++ b/pandas/core/indexes/period.py @@ -25,6 +25,7 @@ DtypeObj, ) from pandas.util._decorators import doc +from pandas.util._exceptions import find_stack_level from pandas.core.dtypes.common import ( is_datetime64_any_dtype, @@ -353,6 +354,13 @@ def astype(self, dtype, copy: bool = True, how=lib.no_default): if is_datetime64_any_dtype(dtype): # 'how' is index-specific, isn't part of the EA interface. + warnings.warn( + f"Converting {type(self).__name__} to DatetimeIndex with " + "'astype' is deprecated and will raise in a future version. " + "Use `obj.to_timestamp(how).tz_localize(dtype.tz)` instead.", + FutureWarning, + stacklevel=find_stack_level(), + ) tz = getattr(dtype, "tz", None) return self.to_timestamp(how=how).tz_localize(tz) diff --git a/pandas/tests/indexes/period/methods/test_astype.py b/pandas/tests/indexes/period/methods/test_astype.py index e2340a2db02f7..f3bd7572e7d10 100644 --- a/pandas/tests/indexes/period/methods/test_astype.py +++ b/pandas/tests/indexes/period/methods/test_astype.py @@ -164,7 +164,9 @@ def test_period_astype_to_timestamp(self): assert res.freq == exp.freq exp = DatetimeIndex(["2011-01-01", "2011-02-01", "2011-03-01"], tz="US/Eastern") - res = pi.astype("datetime64[ns, US/Eastern]") + msg = "Use `obj.to_timestamp" + with tm.assert_produces_warning(FutureWarning, match=msg): + res = pi.astype("datetime64[ns, US/Eastern]") tm.assert_index_equal(res, exp) assert res.freq == exp.freq diff --git a/pandas/tests/indexes/test_common.py b/pandas/tests/indexes/test_common.py index ed9243a5ba8d0..5f2fa64a6f6b4 100644 --- a/pandas/tests/indexes/test_common.py +++ b/pandas/tests/indexes/test_common.py @@ -332,6 +332,9 @@ def test_astype_preserves_name(self, index, dtype): ): # This astype is deprecated in favor of tz_localize warn = FutureWarning + elif isinstance(index, PeriodIndex) and dtype == "datetime64[ns]": + # Deprecated in favor of to_timestamp + warn = FutureWarning try: # Some of these conversions cannot succeed so we use a try / except with tm.assert_produces_warning(warn): From dde14b2cf6d714bbc7d951ce22336840b5fdeddd Mon Sep 17 00:00:00 2001 From: Brock Date: Thu, 11 Nov 2021 13:08:20 -0800 Subject: [PATCH 2/2] gh refs --- doc/source/whatsnew/v1.4.0.rst | 2 +- pandas/core/indexes/period.py | 1 + pandas/tests/indexes/period/methods/test_astype.py | 1 + pandas/tests/indexes/test_common.py | 2 +- 4 files changed, 4 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 23838b1959c01..8a8c1208b7b89 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -400,7 +400,7 @@ Other Deprecations - Deprecated casting behavior when setting timezone-aware value(s) into a timezone-aware :class:`Series` or :class:`DataFrame` column when the timezones do not match. Previously this cast to object dtype. In a future version, the values being inserted will be converted to the series or column's existing timezone (:issue:`37605`) - Deprecated casting behavior when passing an item with mismatched-timezone to :meth:`DatetimeIndex.insert`, :meth:`DatetimeIndex.putmask`, :meth:`DatetimeIndex.where` :meth:`DatetimeIndex.fillna`, :meth:`Series.mask`, :meth:`Series.where`, :meth:`Series.fillna`, :meth:`Series.shift`, :meth:`Series.replace`, :meth:`Series.reindex` (and :class:`DataFrame` column analogues). In the past this has cast to object dtype. In a future version, these will cast the passed item to the index or series's timezone (:issue:`37605`) - Deprecated the 'errors' keyword argument in :meth:`Series.where`, :meth:`DataFrame.where`, :meth:`Series.mask`, and meth:`DataFrame.mask`; in a future version the argument will be removed (:issue:`44294`) -- Deprecated :meth:`PeriodIndex.astype` to ``datetime64[ns]`` or ``DatetimeTZDtype``, use ``obj.to_timestamp(how).tz_localize(dtype.tz)`` instead (:issue:`??`) +- Deprecated :meth:`PeriodIndex.astype` to ``datetime64[ns]`` or ``DatetimeTZDtype``, use ``obj.to_timestamp(how).tz_localize(dtype.tz)`` instead (:issue:`44398`) - .. --------------------------------------------------------------------------- diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py index 3c980a6885779..1db476065a5c8 100644 --- a/pandas/core/indexes/period.py +++ b/pandas/core/indexes/period.py @@ -354,6 +354,7 @@ def astype(self, dtype, copy: bool = True, how=lib.no_default): if is_datetime64_any_dtype(dtype): # 'how' is index-specific, isn't part of the EA interface. + # GH#44398 deprecate astype(dt64), matching Series behavior warnings.warn( f"Converting {type(self).__name__} to DatetimeIndex with " "'astype' is deprecated and will raise in a future version. " diff --git a/pandas/tests/indexes/period/methods/test_astype.py b/pandas/tests/indexes/period/methods/test_astype.py index f3bd7572e7d10..c44f2efed1fcc 100644 --- a/pandas/tests/indexes/period/methods/test_astype.py +++ b/pandas/tests/indexes/period/methods/test_astype.py @@ -166,6 +166,7 @@ def test_period_astype_to_timestamp(self): exp = DatetimeIndex(["2011-01-01", "2011-02-01", "2011-03-01"], tz="US/Eastern") msg = "Use `obj.to_timestamp" with tm.assert_produces_warning(FutureWarning, match=msg): + # GH#44398 res = pi.astype("datetime64[ns, US/Eastern]") tm.assert_index_equal(res, exp) assert res.freq == exp.freq diff --git a/pandas/tests/indexes/test_common.py b/pandas/tests/indexes/test_common.py index 5f2fa64a6f6b4..28be474b28de1 100644 --- a/pandas/tests/indexes/test_common.py +++ b/pandas/tests/indexes/test_common.py @@ -333,7 +333,7 @@ def test_astype_preserves_name(self, index, dtype): # This astype is deprecated in favor of tz_localize warn = FutureWarning elif isinstance(index, PeriodIndex) and dtype == "datetime64[ns]": - # Deprecated in favor of to_timestamp + # Deprecated in favor of to_timestamp GH#44398 warn = FutureWarning try: # Some of these conversions cannot succeed so we use a try / except