Skip to content

Commit 08c4a82

Browse files
authored
DEPR: PeriodIndex.astype(dt64) (#44398)
1 parent f744931 commit 08c4a82

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

doc/source/whatsnew/v1.4.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,8 @@ Other Deprecations
458458
- 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`)
459459
- 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`)
460460
- 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`)
461+
- Deprecated :meth:`PeriodIndex.astype` to ``datetime64[ns]`` or ``DatetimeTZDtype``, use ``obj.to_timestamp(how).tz_localize(dtype.tz)`` instead (:issue:`44398`)
462+
-
461463

462464
.. ---------------------------------------------------------------------------
463465

pandas/core/indexes/period.py

+8
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,14 @@ def astype(self, dtype, copy: bool = True, how=lib.no_default):
354354

355355
if is_datetime64_any_dtype(dtype):
356356
# 'how' is index-specific, isn't part of the EA interface.
357+
# GH#44398 deprecate astype(dt64), matching Series behavior
358+
warnings.warn(
359+
f"Converting {type(self).__name__} to DatetimeIndex with "
360+
"'astype' is deprecated and will raise in a future version. "
361+
"Use `obj.to_timestamp(how).tz_localize(dtype.tz)` instead.",
362+
FutureWarning,
363+
stacklevel=find_stack_level(),
364+
)
357365
tz = getattr(dtype, "tz", None)
358366
return self.to_timestamp(how=how).tz_localize(tz)
359367

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ def test_period_astype_to_timestamp(self):
164164
assert res.freq == exp.freq
165165

166166
exp = DatetimeIndex(["2011-01-01", "2011-02-01", "2011-03-01"], tz="US/Eastern")
167-
res = pi.astype("datetime64[ns, US/Eastern]")
167+
msg = "Use `obj.to_timestamp"
168+
with tm.assert_produces_warning(FutureWarning, match=msg):
169+
# GH#44398
170+
res = pi.astype("datetime64[ns, US/Eastern]")
168171
tm.assert_index_equal(res, exp)
169172
assert res.freq == exp.freq
170173

pandas/tests/indexes/test_common.py

+3
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,9 @@ def test_astype_preserves_name(self, index, dtype):
392392
):
393393
# This astype is deprecated in favor of tz_localize
394394
warn = FutureWarning
395+
elif isinstance(index, PeriodIndex) and dtype == "datetime64[ns]":
396+
# Deprecated in favor of to_timestamp GH#44398
397+
warn = FutureWarning
395398
try:
396399
# Some of these conversions cannot succeed so we use a try / except
397400
with tm.assert_produces_warning(warn):

0 commit comments

Comments
 (0)