Skip to content

BUG: astype, asfreq with non-nano #55958

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ I/O
Period
^^^^^^
- Bug in :class:`Period` addition silently wrapping around instead of raising ``OverflowError`` (:issue:`55503`)
- Bug in casting from :class:`PeriodDtype` with ``astype`` to ``datetime64`` or :class:`DatetimeTZDtype` with non-nanosecond unit incorrectly returning with nanosecond unit (:issue:`55958`)
-

Plotting
Expand All @@ -433,6 +434,7 @@ Groupby/resample/rolling
- Bug in :class:`.Rolling` where duplicate datetimelike indexes are treated as consecutive rather than equal with ``closed='left'`` and ``closed='neither'`` (:issue:`20712`)
- 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`)
- 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`)
- Bug in :meth:`DataFrame.asfreq` and :meth:`Series.asfreq` with a :class:`DatetimeIndex` with non-nanosecond resolution incorrectly converting to nanosecond resolution (:issue:`55958`)
- Bug in :meth:`DataFrame.resample` not respecting ``closed`` and ``label`` arguments for :class:`~pandas.tseries.offsets.BusinessDay` (:issue:`55282`)
- Bug in :meth:`DataFrame.resample` where bin edges were not correct for :class:`~pandas.tseries.offsets.BusinessDay` (:issue:`55281`)
- Bug in :meth:`DataFrame.resample` where bin edges were not correct for :class:`~pandas.tseries.offsets.MonthBegin` (:issue:`55271`)
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,8 @@ def astype(self, dtype, copy: bool = True):
if lib.is_np_dtype(dtype, "M") or isinstance(dtype, DatetimeTZDtype):
# GH#45038 match PeriodIndex behavior.
tz = getattr(dtype, "tz", None)
return self.to_timestamp().tz_localize(tz)
unit = dtl.dtype_to_unit(dtype)
return self.to_timestamp().tz_localize(tz).as_unit(unit)

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

Expand Down
6 changes: 5 additions & 1 deletion pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -2782,7 +2782,11 @@ def asfreq(

new_obj.index = _asfreq_compat(obj.index, freq)
else:
dti = date_range(obj.index.min(), obj.index.max(), freq=freq)
unit = None
if isinstance(obj.index, DatetimeIndex):
# TODO: should we disallow non-DatetimeIndex?
unit = obj.index.unit
dti = date_range(obj.index.min(), obj.index.max(), freq=freq, unit=unit)
dti.name = obj.index.name
new_obj = obj.reindex(dti, method=method, fill_value=fill_value)
if normalize:
Expand Down
9 changes: 6 additions & 3 deletions pandas/tests/indexes/period/methods/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,13 @@ def test_astype_array_fallback(self):
expected = np.array([True, True])
tm.assert_numpy_array_equal(result, expected)

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

exp = DatetimeIndex(["2011-01-01", "2011-02-01", "2011-03-01"], tz="US/Eastern")
res = pi.astype("datetime64[ns, US/Eastern]")
exp = DatetimeIndex(
["2011-01-01", "2011-02-01", "2011-03-01"], tz="US/Eastern"
).as_unit(unit)
res = pi.astype(f"datetime64[{unit}, US/Eastern]")
tm.assert_index_equal(res, exp)
assert res.freq == exp.freq
8 changes: 6 additions & 2 deletions pandas/tests/resample/test_datetime_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1263,10 +1263,14 @@ def test_resample_not_monotonic(unit):
),
],
)
def test_resample_median_bug_1688(dtype):
def test_resample_median_bug_1688(dtype, unit):
# GH#55958
dti = DatetimeIndex(
[datetime(2012, 1, 1, 0, 0, 0), datetime(2012, 1, 1, 0, 5, 0)]
).as_unit(unit)
df = DataFrame(
[1, 2],
index=[datetime(2012, 1, 1, 0, 0, 0), datetime(2012, 1, 1, 0, 5, 0)],
index=dti,
dtype=dtype,
)

Expand Down