Skip to content

Commit 861d8cf

Browse files
check how == 'S'
1 parent 139def2 commit 861d8cf

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
@@ -811,7 +811,14 @@ def to_timestamp(self, freq=None, how: str = "start") -> DatetimeArray:
811811

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

814-
new_data = libperiod.periodarr_to_dt64arr(new_parr.asi8, base)
814+
is_start = how == "S"
815+
if is_start:
816+
new_data = np.asarray(
817+
[getattr(period, "start_time", period) for period in new_parr]
818+
)
819+
else:
820+
new_data = libperiod.periodarr_to_dt64arr(new_parr.asi8, base)
821+
815822
dta = DatetimeArray._from_sequence(new_data, dtype=np.dtype("M8[ns]"))
816823

817824
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)