data_group_by_data['issue_month'] = data_group_by_data['issue_d'].apply(lambda x: x.to_period('M'))
运行提示:'str' object has no attribute 'to_period'
补充 csv表中的issue_d的时间格式为:6/18/2021
报错信息是字符串对象没有to_period属性,使用pandas.Series.dt.to_period,见: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.dt.to_period.html。 对于要转换的对象,应该先转换成日期数组后,再用to_period函数转换。例如:df['issue_d']=pd.to_datetime(["6/18/2021", "6/19/2021", "7/19/2021"]),df['issue_d'] = df['issue_d'].dt.to_period("M")