pandas.Series.to_timestamp#
- Series.to_timestamp(freq=None, how='start', copy=None)[源代码]#
在周期*开始*时强制转换为 Timestamp 的 DatetimeIndex。
- Parameters:
- freqstr,默认值为 PeriodIndex 的频率
所需频率。
- how{‘s’, ‘e’, ‘start’, ‘end’}
将周期转换为时间戳的约定;周期的开始或结束。
- 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:
- 具有 DatetimeIndex 的 Series
Examples
>>> idx = pd.PeriodIndex(['2023', '2024', '2025'], freq='Y') >>> s1 = pd.Series([1, 2, 3], index=idx) >>> s1 2023 1 2024 2 2025 3 Freq: Y-DEC, dtype: int64
生成的 Timestamps 的频率为 YearBegin
>>> s1 = s1.to_timestamp() >>> s1 2023-01-01 1 2024-01-01 2 2025-01-01 3 Freq: YS-JAN, dtype: int64
使用 freq,这是 Timestamps 的偏移量
>>> s2 = pd.Series([1, 2, 3], index=idx) >>> s2 = s2.to_timestamp(freq='M') >>> s2 2023-01-31 1 2024-01-31 2 2025-01-31 3 Freq: YE-JAN, dtype: int64