pandas.DataFrame.to_period#
- DataFrame.to_period(freq=None, axis=0, copy=True)[源代码]#
将DataFrame从DatetimeIndex转换为PerodIndex。
将DataFrame从DatetimeIndex转换为所需的频率(如果未传递,则从索引推断)。
- 参数
- freq字符串,默认
周期索引的频率。
- axis{0或‘index’,1或‘Columns’},默认为0
要转换的轴(默认情况下为索引)。
- copy布尔值,默认为True
如果为False,则不复制基础输入数据。
- 退货
- 带周期索引的DataFrame
示例
>>> idx = pd.to_datetime( ... [ ... "2001-03-31 00:00:00", ... "2002-05-31 00:00:00", ... "2003-08-31 00:00:00", ... ] ... )
>>> idx DatetimeIndex(['2001-03-31', '2002-05-31', '2003-08-31'], dtype='datetime64[ns]', freq=None)
>>> idx.to_period("M") PeriodIndex(['2001-03', '2002-05', '2003-08'], dtype='period[M]')
对于每年的频率
>>> idx.to_period("Y") PeriodIndex(['2001', '2002', '2003'], dtype='period[A-DEC]')