Skip to content

Commit 191d633

Browse files
authored
DEPR: how keyword in PeriodIndex.astype (#37982)
1 parent f2cbf4e commit 191d633

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ Deprecations
476476
- :meth:`Categorical.is_dtype_equal` and :meth:`CategoricalIndex.is_dtype_equal` are deprecated, will be removed in a future version (:issue:`37545`)
477477
- :meth:`Series.slice_shift` and :meth:`DataFrame.slice_shift` are deprecated, use :meth:`Series.shift` or :meth:`DataFrame.shift` instead (:issue:`37601`)
478478
- Partial slicing on unordered :class:`DatetimeIndex` with keys, which are not in Index is deprecated and will be removed in a future version (:issue:`18531`)
479+
- The ``how`` keyword in :meth:`PeriodIndex.astype` is deprecated and will be removed in a future version, use ``index.to_timestamp(how=how)`` instead (:issue:`37982`)
479480
- Deprecated :meth:`Index.asi8` for :class:`Index` subclasses other than :class:`DatetimeIndex`, :class:`TimedeltaIndex`, and :class:`PeriodIndex` (:issue:`37877`)
480481
- The ``inplace`` parameter of :meth:`Categorical.remove_unused_categories` is deprecated and will be removed in a future version (:issue:`37643`)
481482

pandas/core/indexes/period.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from datetime import datetime, timedelta
22
from typing import Any, cast
3+
import warnings
34

45
import numpy as np
56

6-
from pandas._libs import index as libindex
7+
from pandas._libs import index as libindex, lib
78
from pandas._libs.tslibs import BaseOffset, Period, Resolution, Tick
89
from pandas._libs.tslibs.parsing import DateParseError, parse_time_string
910
from pandas._typing import DtypeObj
@@ -376,15 +377,26 @@ def asof_locs(self, where: Index, mask: np.ndarray) -> np.ndarray:
376377
return super().asof_locs(where, mask)
377378

378379
@doc(Index.astype)
379-
def astype(self, dtype, copy: bool = True, how="start"):
380+
def astype(self, dtype, copy: bool = True, how=lib.no_default):
380381
dtype = pandas_dtype(dtype)
381382

383+
if how is not lib.no_default:
384+
# GH#37982
385+
warnings.warn(
386+
"The 'how' keyword in PeriodIndex.astype is deprecated and "
387+
"will be removed in a future version. "
388+
"Use index.to_timestamp(how=how) instead",
389+
FutureWarning,
390+
stacklevel=2,
391+
)
392+
else:
393+
how = "start"
394+
382395
if is_datetime64_any_dtype(dtype):
383396
# 'how' is index-specific, isn't part of the EA interface.
384397
tz = getattr(dtype, "tz", None)
385398
return self.to_timestamp(how=how).tz_localize(tz)
386399

387-
# TODO: should probably raise on `how` here, so we don't ignore it.
388400
return super().astype(dtype, copy=copy)
389401

390402
@property

pandas/tests/indexes/period/test_astype.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,17 @@ def test_period_astype_to_timestamp(self):
144144
pi = PeriodIndex(["2011-01", "2011-02", "2011-03"], freq="M")
145145

146146
exp = DatetimeIndex(["2011-01-01", "2011-02-01", "2011-03-01"], freq="MS")
147-
res = pi.astype("datetime64[ns]")
147+
with tm.assert_produces_warning(FutureWarning):
148+
# how keyword deprecated GH#37982
149+
res = pi.astype("datetime64[ns]", how="start")
148150
tm.assert_index_equal(res, exp)
149151
assert res.freq == exp.freq
150152

151153
exp = DatetimeIndex(["2011-01-31", "2011-02-28", "2011-03-31"])
152154
exp = exp + Timedelta(1, "D") - Timedelta(1, "ns")
153-
res = pi.astype("datetime64[ns]", how="end")
155+
with tm.assert_produces_warning(FutureWarning):
156+
# how keyword deprecated GH#37982
157+
res = pi.astype("datetime64[ns]", how="end")
154158
tm.assert_index_equal(res, exp)
155159
assert res.freq == exp.freq
156160

@@ -161,6 +165,8 @@ def test_period_astype_to_timestamp(self):
161165

162166
exp = DatetimeIndex(["2011-01-31", "2011-02-28", "2011-03-31"], tz="US/Eastern")
163167
exp = exp + Timedelta(1, "D") - Timedelta(1, "ns")
164-
res = pi.astype("datetime64[ns, US/Eastern]", how="end")
168+
with tm.assert_produces_warning(FutureWarning):
169+
# how keyword deprecated GH#37982
170+
res = pi.astype("datetime64[ns, US/Eastern]", how="end")
165171
tm.assert_index_equal(res, exp)
166172
assert res.freq == exp.freq

0 commit comments

Comments
 (0)