Skip to content

Commit

Permalink
DEPR: PeriodIndex.astype how keyword (pandas-dev#49234)
Browse files Browse the repository at this point in the history
* DEPR: PeriodIndex.astype how keyword

* typo fixup
  • Loading branch information
jbrockmendel authored and noatamir committed Nov 9, 2022
1 parent 358b71d commit 3e0f1ee
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 57 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,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 :meth:`.Rolling.validate`, :meth:`.Expanding.validate`, and :meth:`.ExponentialMovingWindow.validate` (:issue:`43665`)
Expand Down
34 changes: 1 addition & 33 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
timedelta,
)
from typing import Hashable
import warnings

import numpy as np

Expand All @@ -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

Expand Down Expand Up @@ -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:
"""
Expand Down
24 changes: 0 additions & 24 deletions pandas/tests/indexes/period/methods/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
NaT,
Period,
PeriodIndex,
Timedelta,
period_range,
)
import pandas._testing as tm
Expand Down Expand Up @@ -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

0 comments on commit 3e0f1ee

Please sign in to comment.