
上QQ阅读APP看书,第一时间看更新
005 使用月初日期设置DataFrame的行标签
此案例主要通过在date_range()函数中设置freq参数值为pd.offsets.MonthBegin,实现使用月初日期设置DataFrame的行标签。当在Jupyter Notebook中运行此案例代码之后,将在DataFrame中把行标签调整为每月的月初日期,效果分别如图005-1和图005-2所示。

图005-1

图005-2
主要代码如下。

在上面这段代码中,df.index=pd.date_range('20210111',periods=5, freq=pd.offsets.MonthBegin(1))表示在df中将行标签调整为每月的月初日期。如果改为df.index=pd.date_range('20210111',periods=5,freq=pd.offsets.BusinessMonthBegin(1)),则表示在df中将行标签调整为每月的工作日月初,即每月的第一天上班日期。
此案例的主要源文件是MyCode\H787\H787.ipynb。