pandas.Series.dt.to_period#
- Series.dt.to_period(*args, **kwargs)[源代码]#
以特定频率强制转换为周期数组/索引。
将Datetime数组/索引转换为周期数组/索引。
- 参数
- freq字符串或偏移量,可选
其中一只Pandas offset strings 或偏移量对象。将在默认情况下被推断。
- 退货
- 周期数组/索引
- 加薪
- ValueError
转换具有非正规值的Datetime数组/索引时,因此无法推断频率。
参见
PeriodIndex
保持序数值的不变ndarray。
DatetimeIndex.to_pydatetime
将DatetimeIndex作为对象返回。
示例
>>> df = pd.DataFrame({"y": [1, 2, 3]}, ... index=pd.to_datetime(["2000-03-31 00:00:00", ... "2000-05-31 00:00:00", ... "2000-08-31 00:00:00"])) >>> df.index.to_period("M") PeriodIndex(['2000-03', '2000-05', '2000-08'], dtype='period[M]')
推断每天的频率
>>> idx = pd.date_range("2017-01-01", periods=2) >>> idx.to_period() PeriodIndex(['2017-01-01', '2017-01-02'], dtype='period[D]')