pandas.Series.to_period#

Series.to_period(freq=None, copy=None)[源代码]#

将 DatetimeIndex 转换为 PeriodIndex。

Parameters:
freqstr,默认 None

与 PeriodIndex 相关的频率。

copybool, default True

是否返回副本。

备注

copy 关键字在 pandas 3.0 中将更改行为。Copy-on-Write 将默认启用,这意味着所有带有 copy 关键字的方法都将使用惰性复制机制来延迟复制并忽略 copy 关键字。copy 关键字将在 pandas 的未来版本中移除。

通过启用 copy on write pd.options.mode.copy_on_write = True,您可以获得未来的行为和改进。

Returns:
Series

索引转换为 PeriodIndex 的 Series。

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

查看索引

>>> s.index
PeriodIndex(['2023', '2024', '2025'], dtype='period[Y-DEC]')