pandas.PeriodIndex.to_timestamp#
- PeriodIndex.to_timestamp(freq=None, how='start')[source]#
Cast to DatetimeIndex.
If possible, gives microsecond-unit DatetimeIndex. Otherwise gives nanosecond unit.
- Parameters:
- freqstr or DateOffset, optional
Target frequency. By default the result is exactly the start (or end) of each period. With
how="start", the result is the start ofasfreq(freq, how="start"), except that afreqcoarser than daily gives midnight on that period’s last day, soto_timestamp("M")gives month-end dates. Withhow="end",freqonly affects the resolution (‘ns’ gives nanosecond unit).- how{‘start’, ‘end’, ‘s’, ‘e’}, default ‘start’
Whether to use the start or end of the time period being converted. Case-insensitive.
- Returns:
- DatetimeIndex
Timestamp representation of given Period-like object.
See also
PeriodIndex.dayThe days of the period.
PeriodIndex.from_fieldsConstruct a PeriodIndex from fields (year, month, day, etc.).
PeriodIndex.from_ordinalsConstruct a PeriodIndex from ordinals.
PeriodIndex.hourThe hour of the period.
PeriodIndex.minuteThe minute of the period.
PeriodIndex.monthThe month as January=1, December=12.
PeriodIndex.secondThe second of the period.
PeriodIndex.yearThe year of the period.
Examples
>>> idx = pd.PeriodIndex(["2023-01", "2023-02", "2023-03"], freq="M") >>> idx.to_timestamp() DatetimeIndex(['2023-01-01', '2023-02-01', '2023-03-01'], dtype='datetime64[us]', freq='MS')
A
freqcoarser than daily gives the last day of each period:>>> idx.to_timestamp("M") DatetimeIndex(['2023-01-31', '2023-02-28', '2023-03-31'], dtype='datetime64[us]', freq='ME')
The frequency will not be inferred if the index contains less than three elements, or if the values of index are not strictly monotonic:
>>> idx = pd.PeriodIndex(["2023-01", "2023-02"], freq="M") >>> idx.to_timestamp() DatetimeIndex(['2023-01-01', '2023-02-01'], dtype='datetime64[us]', freq=None)
>>> idx = pd.PeriodIndex( ... ["2023-01", "2023-02", "2023-02", "2023-03"], freq="2M" ... ) >>> idx.to_timestamp() DatetimeIndex(['2023-01-01', '2023-02-01', '2023-02-01', '2023-03-01'], dtype='datetime64[us]', freq=None)