- API reference
- Series
- pandas.Serie...
pandas.Series.to_period#
- Series.to_period(freq=None,copy=None)[source]#
Convert Series from DatetimeIndex to PeriodIndex.
- Parameters:
- freqstr, default None
Frequency associated with the PeriodIndex.
- copybool, default True
Whether or not to return a copy.
Note
Thecopy keyword will change behavior in pandas 3.0.Copy-on-Writewill be enabled by default, which means that all methods with acopy keyword will use a lazy copy mechanism to defer the copy andignore thecopy keyword. Thecopy keyword will be removed in afuture version of pandas.
You can already get the future behavior and improvements throughenabling copy on write
pd.options.mode.copy_on_write=True
- Returns:
- Series
Series with index converted to PeriodIndex.
Examples
>>>idx=pd.DatetimeIndex(['2023','2024','2025'])>>>s=pd.Series([1,2,3],index=idx)>>>s=s.to_period()>>>s2023 12024 22025 3Freq: Y-DEC, dtype: int64
Viewing the index
>>>s.indexPeriodIndex(['2023', '2024', '2025'], dtype='period[Y-DEC]')
On this page