Skip to content
Closed
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
4 changes: 3 additions & 1 deletion pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,8 +824,10 @@ def to_timestamp(self, freq=None, how: str = "start") -> DatetimeArray:

new_parr = self.asfreq(freq, how=how)

dt64_dtype = np.dtype("datetime64[ns]")

new_data = libperiod.periodarr_to_dt64arr(new_parr.asi8, base)
dta = DatetimeArray._from_sequence(new_data, dtype=np.dtype("M8[ns]"))
dta = DatetimeArray._from_sequence(new_data, dtype=dt64_dtype)

if self.freq.name == "B":
# See if we can retain BDay instead of Day in cases where
Expand Down
26 changes: 26 additions & 0 deletions pandas/tests/indexes/period/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,29 @@ def test_dunder_array(array):
np.array(obj, dtype=dtype)
with pytest.raises(TypeError, match=msg):
np.array(obj, dtype=getattr(np, dtype))


def test_to_timestamp_monthly_resolution():
idx = PeriodIndex(["2011-01", "NaT", "2011-02"], freq="2M")
ts = idx.to_timestamp()
assert ts.dtype == np.dtype("datetime64[ns]")


def test_to_timestamp_yearly_resolution():
idx = PeriodIndex(["2011", "2012"], freq="Y")
ts = idx.to_timestamp()
assert ts.dtype == np.dtype("datetime64[ns]")


def test_to_timestamp_daily_resolution():
idx = PeriodIndex(["2011-01-01", "2011-01-02"], freq="D")
ts = idx.to_timestamp()
assert ts.dtype == np.dtype("datetime64[ns]")


def test_to_timestamp_nanosecond_resolution():
idx = PeriodIndex(
["2011-01-01 00:00:00.000000001", "2011-01-01 00:00:00.000000002"], freq="ns"
)
ts = idx.to_timestamp()
assert ts.dtype == np.dtype("datetime64[ns]")
Loading