pandas.Series.to_period #
- 系列。to_period ( freq = None , copy = None ) [来源] #
将系列从日期时间索引转换为周期索引。
- 参数:
- freq str,默认无
与PeriodIndex 相关的频率。
- 复制布尔值,默认 True
是否返还副本。
笔记
copy关键字将改变 pandas 3.0 中的行为 。默认情况下会启用Copy-on-Write ,这意味着所有带有 copy关键字的方法都将使用惰性复制机制来推迟复制并忽略copy关键字。 copy关键字将在 pandas 的未来版本中删除。
您已经可以通过启用写入时复制来获得未来的行为和改进
pd.options.mode.copy_on_write = True
- 返回:
- 系列
索引转换为PeriodIndex 的系列。
例子
>>> 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]')