Skip to content

Commit e5a0df9

Browse files
authored
DOC: add an example to PeriodIndex.to_timestamp to clarify the behavior in case len(index)<3 (#57384)
* add an example to PeriodIndex.to_timestamp * correct to_timestamp docs, add an example * correct the wording in to_timestamp docstring
1 parent d3597ed commit e5a0df9

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

pandas/core/arrays/period.py

+14
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,20 @@ def to_timestamp(self, freq=None, how: str = "start") -> DatetimeArray:
638638
>>> idx.to_timestamp()
639639
DatetimeIndex(['2023-01-01', '2023-02-01', '2023-03-01'],
640640
dtype='datetime64[ns]', freq='MS')
641+
642+
The frequency will not be inferred if the index contains less than
643+
three elements, or if the values of index are not strictly monotonic:
644+
645+
>>> idx = pd.PeriodIndex(["2023-01", "2023-02"], freq="M")
646+
>>> idx.to_timestamp()
647+
DatetimeIndex(['2023-01-01', '2023-02-01'], dtype='datetime64[ns]', freq=None)
648+
649+
>>> idx = pd.PeriodIndex(
650+
... ["2023-01", "2023-02", "2023-02", "2023-03"], freq="2M"
651+
... )
652+
>>> idx.to_timestamp()
653+
DatetimeIndex(['2023-01-01', '2023-02-01', '2023-02-01', '2023-03-01'],
654+
dtype='datetime64[ns]', freq=None)
641655
"""
642656
from pandas.core.arrays import DatetimeArray
643657

0 commit comments

Comments
 (0)