Skip to content
Next Next commit
check how == 'S'
  • Loading branch information
aram-cedarwood committed Oct 3, 2024
commit 861d8cf9f7d428b09eafc794727b69b926f95ddc
9 changes: 8 additions & 1 deletion pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,14 @@ def to_timestamp(self, freq=None, how: str = "start") -> DatetimeArray:

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

new_data = libperiod.periodarr_to_dt64arr(new_parr.asi8, base)
is_start = how == "S"
if is_start:
new_data = np.asarray(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
new_data = np.asarray(
new_data = new_parr.dt.start_time

the list comprehension is going to be prohibitively slow (might be the cause of the current ASV failures). If you can use the vectorized datetimeindex (DTI) methods we have, I think that might fix things

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey thanks @WillAyd, I'll have more time later to look into this, but for now...
new_parr.dt.start_time gives AttributeError: 'PeriodArray' object has no attribute 'dt'
new_parr.start_time gives RecursionError: maximum recursion depth exceeded while calling a Python object

[getattr(period, "start_time", period) for period in new_parr]
)
else:
new_data = libperiod.periodarr_to_dt64arr(new_parr.asi8, base)

dta = DatetimeArray._from_sequence(new_data, dtype=np.dtype("M8[ns]"))

if self.freq.name == "B":
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/indexes/period/methods/test_to_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ def test_to_timestamp_1703(self):
result = index.to_timestamp()
assert result[0] == Timestamp("1/1/2012")

def test_cast_to_timestamps_at_beginning_of_period(self):
# GH 59371
index = period_range("2000", periods=3, freq="M")
result = index.to_timestamp("M")

expected = DatetimeIndex(
["2000-01-01", "2000-02-01", "2000-03-01"],
dtype="datetime64[ns]",
freq="MS",
)
tm.assert_equal(result, expected)


def test_ms_to_timestamp_error_message():
# https://github.com/pandas-dev/pandas/issues/58974#issuecomment-2164265446
Expand Down
Loading