Skip to content

Commit 795357d

Browse files
authored
BUG: astype, asfreq with non-nano (#55958)
* BUG: period.astype to non-nano dt64 * BUG: .asfreq with non-nano * GH ref
1 parent a7e4af4 commit 795357d

File tree

5 files changed

+21
-7
lines changed

5 files changed

+21
-7
lines changed

doc/source/whatsnew/v2.2.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ I/O
421421
Period
422422
^^^^^^
423423
- Bug in :class:`Period` addition silently wrapping around instead of raising ``OverflowError`` (:issue:`55503`)
424+
- Bug in casting from :class:`PeriodDtype` with ``astype`` to ``datetime64`` or :class:`DatetimeTZDtype` with non-nanosecond unit incorrectly returning with nanosecond unit (:issue:`55958`)
424425
-
425426

426427
Plotting
@@ -433,6 +434,7 @@ Groupby/resample/rolling
433434
- Bug in :class:`.Rolling` where duplicate datetimelike indexes are treated as consecutive rather than equal with ``closed='left'`` and ``closed='neither'`` (:issue:`20712`)
434435
- Bug in :meth:`.DataFrameGroupBy.idxmin`, :meth:`.DataFrameGroupBy.idxmax`, :meth:`.SeriesGroupBy.idxmin`, and :meth:`.SeriesGroupBy.idxmax` would not retain :class:`.Categorical` dtype when the index was a :class:`.CategoricalIndex` that contained NA values (:issue:`54234`)
435436
- Bug in :meth:`.DataFrameGroupBy.transform` and :meth:`.SeriesGroupBy.transform` when ``observed=False`` and ``f="idxmin"`` or ``f="idxmax"`` would incorrectly raise on unobserved categories (:issue:`54234`)
437+
- Bug in :meth:`DataFrame.asfreq` and :meth:`Series.asfreq` with a :class:`DatetimeIndex` with non-nanosecond resolution incorrectly converting to nanosecond resolution (:issue:`55958`)
436438
- Bug in :meth:`DataFrame.resample` not respecting ``closed`` and ``label`` arguments for :class:`~pandas.tseries.offsets.BusinessDay` (:issue:`55282`)
437439
- Bug in :meth:`DataFrame.resample` where bin edges were not correct for :class:`~pandas.tseries.offsets.BusinessDay` (:issue:`55281`)
438440
- Bug in :meth:`DataFrame.resample` where bin edges were not correct for :class:`~pandas.tseries.offsets.MonthBegin` (:issue:`55271`)

pandas/core/arrays/period.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,8 @@ def astype(self, dtype, copy: bool = True):
784784
if lib.is_np_dtype(dtype, "M") or isinstance(dtype, DatetimeTZDtype):
785785
# GH#45038 match PeriodIndex behavior.
786786
tz = getattr(dtype, "tz", None)
787-
return self.to_timestamp().tz_localize(tz)
787+
unit = dtl.dtype_to_unit(dtype)
788+
return self.to_timestamp().tz_localize(tz).as_unit(unit)
788789

789790
return super().astype(dtype, copy=copy)
790791

pandas/core/resample.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -2782,7 +2782,11 @@ def asfreq(
27822782

27832783
new_obj.index = _asfreq_compat(obj.index, freq)
27842784
else:
2785-
dti = date_range(obj.index.min(), obj.index.max(), freq=freq)
2785+
unit = None
2786+
if isinstance(obj.index, DatetimeIndex):
2787+
# TODO: should we disallow non-DatetimeIndex?
2788+
unit = obj.index.unit
2789+
dti = date_range(obj.index.min(), obj.index.max(), freq=freq, unit=unit)
27862790
dti.name = obj.index.name
27872791
new_obj = obj.reindex(dti, method=method, fill_value=fill_value)
27882792
if normalize:

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,13 @@ def test_astype_array_fallback(self):
139139
expected = np.array([True, True])
140140
tm.assert_numpy_array_equal(result, expected)
141141

142-
def test_period_astype_to_timestamp(self):
142+
def test_period_astype_to_timestamp(self, unit):
143+
# GH#55958
143144
pi = PeriodIndex(["2011-01", "2011-02", "2011-03"], freq="M")
144145

145-
exp = DatetimeIndex(["2011-01-01", "2011-02-01", "2011-03-01"], tz="US/Eastern")
146-
res = pi.astype("datetime64[ns, US/Eastern]")
146+
exp = DatetimeIndex(
147+
["2011-01-01", "2011-02-01", "2011-03-01"], tz="US/Eastern"
148+
).as_unit(unit)
149+
res = pi.astype(f"datetime64[{unit}, US/Eastern]")
147150
tm.assert_index_equal(res, exp)
148151
assert res.freq == exp.freq

pandas/tests/resample/test_datetime_index.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -1267,10 +1267,14 @@ def test_resample_not_monotonic(unit):
12671267
),
12681268
],
12691269
)
1270-
def test_resample_median_bug_1688(dtype):
1270+
def test_resample_median_bug_1688(dtype, unit):
1271+
# GH#55958
1272+
dti = DatetimeIndex(
1273+
[datetime(2012, 1, 1, 0, 0, 0), datetime(2012, 1, 1, 0, 5, 0)]
1274+
).as_unit(unit)
12711275
df = DataFrame(
12721276
[1, 2],
1273-
index=[datetime(2012, 1, 1, 0, 0, 0), datetime(2012, 1, 1, 0, 5, 0)],
1277+
index=dti,
12741278
dtype=dtype,
12751279
)
12761280

0 commit comments

Comments
 (0)