pandas.Series.to_period#
- Series.to_period(freq=None, copy=<no_default>)[source]#
Convert Series from DatetimeIndex to PeriodIndex.
- Parameters:
- freqstr, default None
Frequency associated with the PeriodIndex.
- copybool, default False
Whether or not to return a copy.
Note
The copy keyword will change behavior in pandas 3.0. Copy-on-Write will be enabled by default, which means that all methods with a copy keyword will use a lazy copy mechanism to defer the copy and ignore the copy keyword. The copy keyword will be removed in a future version of pandas.
You can already get the future behavior and improvements through enabling copy on write
pd.options.mode.copy_on_write = True
Deprecated since version 3.0.0.
- Returns:
- Series
Series with index converted to PeriodIndex.
See also
DataFrame.to_period
Equivalent method for DataFrame.
Series.dt.to_period
Convert DateTime column values.
Examples
>>> idx = pd.DatetimeIndex(["2023", "2024", "2025"]) >>> s = pd.Series([1, 2, 3], index=idx) >>> s = s.to_period() >>> s 2023 1 2024 2 2025 3 Freq: Y-DEC, dtype: int64
Viewing the index
>>> s.index PeriodIndex(['2023', '2024', '2025'], dtype='period[Y-DEC]')