Skip to content

Commit e97004b

Browse files
jbrockmendelphofl
authored andcommitted
DEPR: PeriodIndex.astype how keyword (#49234)
* DEPR: PeriodIndex.astype how keyword * typo fixup
1 parent 711fd8f commit e97004b

File tree

3 files changed

+2
-57
lines changed

3 files changed

+2
-57
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ Removal of prior version deprecations/changes
166166
- Remove arguments ``names`` and ``dtype`` from :meth:`Index.copy` and ``levels`` and ``codes`` from :meth:`MultiIndex.copy` (:issue:`35853`, :issue:`36685`)
167167
- Remove argument ``inplace`` from :meth:`MultiIndex.set_levels` and :meth:`MultiIndex.set_codes` (:issue:`35626`)
168168
- Disallow passing positional arguments to :meth:`MultiIndex.set_levels` and :meth:`MultiIndex.set_codes` (:issue:`41485`)
169+
- Removed argument ``how`` from :meth:`PeriodIndex.astype`, use :meth:`PeriodIndex.to_timestamp` instead (:issue:`37982`)
169170
- Removed argument ``try_cast`` from :meth:`DataFrame.mask`, :meth:`DataFrame.where`, :meth:`Series.mask` and :meth:`Series.where` (:issue:`38836`)
170171
- Disallow passing non-round floats to :class:`Timestamp` with ``unit="M"`` or ``unit="Y"`` (:issue:`47266`)
171172
- Removed :meth:`.Rolling.validate`, :meth:`.Expanding.validate`, and :meth:`.ExponentialMovingWindow.validate` (:issue:`43665`)

pandas/core/indexes/period.py

+1-33
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
timedelta,
66
)
77
from typing import Hashable
8-
import warnings
98

109
import numpy as np
1110

@@ -29,13 +28,8 @@
2928
cache_readonly,
3029
doc,
3130
)
32-
from pandas.util._exceptions import find_stack_level
3331

34-
from pandas.core.dtypes.common import (
35-
is_datetime64_any_dtype,
36-
is_integer,
37-
pandas_dtype,
38-
)
32+
from pandas.core.dtypes.common import is_integer
3933
from pandas.core.dtypes.dtypes import PeriodDtype
4034
from pandas.core.dtypes.missing import is_valid_na_for_dtype
4135

@@ -349,32 +343,6 @@ def asof_locs(self, where: Index, mask: npt.NDArray[np.bool_]) -> np.ndarray:
349343

350344
return super().asof_locs(where, mask)
351345

352-
@doc(Index.astype)
353-
def astype(self, dtype, copy: bool = True, how=lib.no_default):
354-
dtype = pandas_dtype(dtype)
355-
356-
if how is not lib.no_default:
357-
# GH#37982
358-
warnings.warn(
359-
"The 'how' keyword in PeriodIndex.astype is deprecated and "
360-
"will be removed in a future version. "
361-
"Use index.to_timestamp(how=how) instead.",
362-
FutureWarning,
363-
stacklevel=find_stack_level(),
364-
)
365-
else:
366-
how = "start"
367-
368-
if is_datetime64_any_dtype(dtype):
369-
# 'how' is index-specific, isn't part of the EA interface.
370-
# GH#45038 implement this for PeriodArray (but without "how")
371-
# once the "how" deprecation is enforced we can just dispatch
372-
# directly to PeriodArray.
373-
tz = getattr(dtype, "tz", None)
374-
return self.to_timestamp(how=how).tz_localize(tz)
375-
376-
return super().astype(dtype, copy=copy)
377-
378346
@property
379347
def is_full(self) -> bool:
380348
"""

pandas/tests/indexes/period/methods/test_astype.py

-24
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
NaT,
99
Period,
1010
PeriodIndex,
11-
Timedelta,
1211
period_range,
1312
)
1413
import pandas._testing as tm
@@ -149,30 +148,7 @@ def test_astype_array_fallback(self):
149148
def test_period_astype_to_timestamp(self):
150149
pi = PeriodIndex(["2011-01", "2011-02", "2011-03"], freq="M")
151150

152-
exp = DatetimeIndex(["2011-01-01", "2011-02-01", "2011-03-01"], freq="MS")
153-
with tm.assert_produces_warning(FutureWarning):
154-
# how keyword deprecated GH#37982
155-
res = pi.astype("datetime64[ns]", how="start")
156-
tm.assert_index_equal(res, exp)
157-
assert res.freq == exp.freq
158-
159-
exp = DatetimeIndex(["2011-01-31", "2011-02-28", "2011-03-31"])
160-
exp = exp + Timedelta(1, "D") - Timedelta(1, "ns")
161-
with tm.assert_produces_warning(FutureWarning):
162-
# how keyword deprecated GH#37982
163-
res = pi.astype("datetime64[ns]", how="end")
164-
tm.assert_index_equal(res, exp)
165-
assert res.freq == exp.freq
166-
167151
exp = DatetimeIndex(["2011-01-01", "2011-02-01", "2011-03-01"], tz="US/Eastern")
168152
res = pi.astype("datetime64[ns, US/Eastern]")
169153
tm.assert_index_equal(res, exp)
170154
assert res.freq == exp.freq
171-
172-
exp = DatetimeIndex(["2011-01-31", "2011-02-28", "2011-03-31"], tz="US/Eastern")
173-
exp = exp + Timedelta(1, "D") - Timedelta(1, "ns")
174-
with tm.assert_produces_warning(FutureWarning):
175-
# how keyword deprecated GH#37982
176-
res = pi.astype("datetime64[ns, US/Eastern]", how="end")
177-
tm.assert_index_equal(res, exp)
178-
assert res.freq == exp.freq

0 commit comments

Comments
 (0)