Skip to content

Commit f20e1cd

Browse files
check how == 'S'
1 parent bc24e84 commit f20e1cd

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

pandas/core/arrays/period.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,14 @@ def to_timestamp(self, freq=None, how: str = "start") -> DatetimeArray:
822822

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

825-
new_data = libperiod.periodarr_to_dt64arr(new_parr.asi8, base)
825+
is_start = how == "S"
826+
if is_start:
827+
new_data = np.asarray(
828+
[getattr(period, "start_time", period) for period in new_parr]
829+
)
830+
else:
831+
new_data = libperiod.periodarr_to_dt64arr(new_parr.asi8, base)
832+
826833
dta = DatetimeArray._from_sequence(new_data, dtype=np.dtype("M8[ns]"))
827834

828835
if self.freq.name == "B":

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,18 @@ def test_to_timestamp_1703(self):
141141
result = index.to_timestamp()
142142
assert result[0] == Timestamp("1/1/2012")
143143

144+
def test_cast_to_timestamps_at_beginning_of_period(self):
145+
# GH 59371
146+
index = period_range("2000", periods=3, freq="M")
147+
result = index.to_timestamp("M")
148+
149+
expected = DatetimeIndex(
150+
["2000-01-01", "2000-02-01", "2000-03-01"],
151+
dtype="datetime64[ns]",
152+
freq="MS",
153+
)
154+
tm.assert_equal(result, expected)
155+
144156

145157
def test_ms_to_timestamp_error_message():
146158
# https://github.com/pandas-dev/pandas/issues/58974#issuecomment-2164265446

0 commit comments

Comments
 (0)