diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 4ac737bb6b29a..74a50f435de5d 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -156,6 +156,7 @@ Removal of prior version deprecations/changes - Remove arguments ``names`` and ``dtype`` from :meth:`Index.copy` and ``levels`` and ``codes`` from :meth:`MultiIndex.copy` (:issue:`35853`, :issue:`36685`) - Remove argument ``inplace`` from :meth:`MultiIndex.set_levels` and :meth:`MultiIndex.set_codes` (:issue:`35626`) - Disallow passing positional arguments to :meth:`MultiIndex.set_levels` and :meth:`MultiIndex.set_codes` (:issue:`41485`) +- Removed argument ``how`` from :meth:`PeriodIndex.astype`, use :meth:`PeriodIndex.to_timestamp` instead (:issue:`37982`) - Removed argument ``try_cast`` from :meth:`DataFrame.mask`, :meth:`DataFrame.where`, :meth:`Series.mask` and :meth:`Series.where` (:issue:`38836`) - Disallow passing non-round floats to :class:`Timestamp` with ``unit="M"`` or ``unit="Y"`` (:issue:`47266`) - Removed deprecated :meth:`Timedelta.delta`, :meth:`Timedelta.is_populated`, and :attr:`Timedelta.freq` (:issue:`46430`, :issue:`46476`) diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py index a5408f19456dd..889d8f33cdfae 100644 --- a/pandas/core/indexes/period.py +++ b/pandas/core/indexes/period.py @@ -5,7 +5,6 @@ timedelta, ) from typing import Hashable -import warnings import numpy as np @@ -29,13 +28,8 @@ cache_readonly, doc, ) -from pandas.util._exceptions import find_stack_level -from pandas.core.dtypes.common import ( - is_datetime64_any_dtype, - is_integer, - pandas_dtype, -) +from pandas.core.dtypes.common import is_integer from pandas.core.dtypes.dtypes import PeriodDtype from pandas.core.dtypes.missing import is_valid_na_for_dtype @@ -349,32 +343,6 @@ def asof_locs(self, where: Index, mask: npt.NDArray[np.bool_]) -> np.ndarray: return super().asof_locs(where, mask) - @doc(Index.astype) - def astype(self, dtype, copy: bool = True, how=lib.no_default): - dtype = pandas_dtype(dtype) - - if how is not lib.no_default: - # GH#37982 - warnings.warn( - "The 'how' keyword in PeriodIndex.astype is deprecated and " - "will be removed in a future version. " - "Use index.to_timestamp(how=how) instead.", - FutureWarning, - stacklevel=find_stack_level(), - ) - else: - how = "start" - - if is_datetime64_any_dtype(dtype): - # 'how' is index-specific, isn't part of the EA interface. - # GH#45038 implement this for PeriodArray (but without "how") - # once the "how" deprecation is enforced we can just dispatch - # directly to PeriodArray. - tz = getattr(dtype, "tz", None) - return self.to_timestamp(how=how).tz_localize(tz) - - return super().astype(dtype, copy=copy) - @property def is_full(self) -> bool: """ diff --git a/pandas/tests/indexes/period/methods/test_astype.py b/pandas/tests/indexes/period/methods/test_astype.py index fbc1d3702115e..9720b751b87ce 100644 --- a/pandas/tests/indexes/period/methods/test_astype.py +++ b/pandas/tests/indexes/period/methods/test_astype.py @@ -8,7 +8,6 @@ NaT, Period, PeriodIndex, - Timedelta, period_range, ) import pandas._testing as tm @@ -149,30 +148,7 @@ def test_astype_array_fallback(self): def test_period_astype_to_timestamp(self): pi = PeriodIndex(["2011-01", "2011-02", "2011-03"], freq="M") - exp = DatetimeIndex(["2011-01-01", "2011-02-01", "2011-03-01"], freq="MS") - with tm.assert_produces_warning(FutureWarning): - # how keyword deprecated GH#37982 - res = pi.astype("datetime64[ns]", how="start") - tm.assert_index_equal(res, exp) - assert res.freq == exp.freq - - exp = DatetimeIndex(["2011-01-31", "2011-02-28", "2011-03-31"]) - exp = exp + Timedelta(1, "D") - Timedelta(1, "ns") - with tm.assert_produces_warning(FutureWarning): - # how keyword deprecated GH#37982 - res = pi.astype("datetime64[ns]", how="end") - tm.assert_index_equal(res, exp) - 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]") tm.assert_index_equal(res, exp) assert res.freq == exp.freq - - exp = DatetimeIndex(["2011-01-31", "2011-02-28", "2011-03-31"], tz="US/Eastern") - exp = exp + Timedelta(1, "D") - Timedelta(1, "ns") - with tm.assert_produces_warning(FutureWarning): - # how keyword deprecated GH#37982 - res = pi.astype("datetime64[ns, US/Eastern]", how="end") - tm.assert_index_equal(res, exp) - assert res.freq == exp.freq