#coding:utf8 import pandas as pd import matplotlib.pyplot as plt import seaborn as sns import numpy as np sns.set_style('darkgrid') import matplotlib.font_manager as fm myfont=fm.FontProperties(fname=r'./data/simhei.ttf') #请完善下面的函数 def push_week(new_data): ############ Begin ############ new_data=new_data[new_data['type']==4].copy() #选取样本 new_data['weekdays'] = pd.to_datetime(new_data['time']).apply(lambda x: x.weekday()+1) #时间转化 week_days = new_data.groupby('weekdays')['user_id'].count() #统计购买次数 fig=plt.figure(figsize=(8,6)) #设置大小 bar_width = 0.33 # 设置宽度 plt.bar(week_days.index.values , week_days.values, bar_width, label='下单的次数') plt.xlabel('时间',fontproperties=myfont,fontsize=9) plt.ylabel('数量',fontproperties=myfont,fontsize=9) plt.title('一周内每天的下单情况',fontproperties=myfont,fontsize=12) plt.xticks(week_days.index.values, ('周一', '周二', '周三', '周四', '周五', '周六', '周日'),fontproperties=myfont,fontsize=9) plt.ylim(0,300) plt.legend(prop=myfont) ############ End ############ plt.savefig('./task2/task2_week.png') plt.close(fig) def push_date(new_data): new_data = new_data[(new_data['type'] == 4) & (pd.to_datetime(new_data['time']) < pd.to_datetime('2016-03-01'))].copy() #选出2016年数据 new_data['days'] = [x.day for x in pd.to_datetime(new_data['time'])] #选出天数 renew=new_data.groupby('days')['sku_id'].count() fig = plt.figure(figsize=(8, 6)) plt.plot(renew.index.values,renew.values,label='购买次数') plt.xlabel('天数',fontproperties=myfont,fontsize=9) plt.ylabel('次数',fontproperties=myfont,fontsize=9) plt.title('购买量和月内日期的关系',fontproperties=myfont,fontsize=12) plt.legend(prop=myfont) ############ End ############ plt.savefig('./task2/task2_date.png') plt.close(fig) 报错src/task2_test.py:22: FutureWarning: The pandas.datetime class is deprecated and will be removed from pandas in a future version. Import from datetime instead. data['weekdays'] = pd.to_datetime(data['time']).apply(pd.datetime.weekday) + 1 购买意愿与星期之间的关系图完成! 购买意愿与日期之间的关系图完成!
时间: 2024-03-15 21:47:55 浏览: 136
这个错误是因为pandas.datetime类已经被弃用,建议使用datetime库来代替。可以将代码中的pd.datetime.weekday()改为pd.datetime().weekday(),即可解决这个问题。
修改后的代码如下:
```python
#coding:utf8
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
sns.set_style('darkgrid')
import matplotlib.font_manager as fm
myfont=fm.FontProperties(fname=r'./data/simhei.ttf')
#请完善下面的函数
def push_week(new_data):
############ Begin ############
new_data=new_data[new_data['type']==4].copy() #选取样本
new_data['weekdays'] = pd.to_datetime(new_data['time']).apply(lambda x: x.weekday()+1) #时间转化
week_days = new_data.groupby('weekdays')['user_id'].count() #统计购买次数
fig=plt.figure(figsize=(8,6)) #设置大小
bar_width = 0.33 # 设置宽度
plt.bar(week_days.index.values , week_days.values, bar_width, label='下单的次数')
plt.xlabel('时间',fontproperties=myfont,fontsize=9)
plt.ylabel('数量',fontproperties=myfont,fontsize=9)
plt.title('一周内每天的下单情况',fontproperties=myfont,fontsize=12)
plt.xticks(week_days.index.values, ('周一', '周二', '周三', '周四', '周五', '周六', '周日'),fontproperties=myfont,fontsize=9)
plt.ylim(0,300)
plt.legend(prop=myfont)
############ End ############
plt.savefig('./task2/task2_week.png')
plt.close(fig)
def push_date(new_data):
new_data = new_data[(new_data['type'] == 4) & (pd.to_datetime(new_data['time']) < pd.to_datetime('2016-03-01'))].copy() #选出2016年数据
new_data['days'] = [x.day for x in pd.to_datetime(new_data['time'])] #选出天数
renew=new_data.groupby('days')['sku_id'].count()
fig = plt.figure(figsize=(8, 6))
plt.plot(renew.index.values,renew.values,label='购买次数')
plt.xlabel('天数',fontproperties=myfont,fontsize=9)
plt.ylabel('次数',fontproperties=myfont,fontsize=9)
plt.title('购买量和月内日期的关系',fontproperties=myfont,fontsize=12)
plt.legend(prop=myfont)
############ End ############
plt.savefig('./task2/task2_date.png')
plt.close(fig)
```
建议在代码中加入注释,使得代码更加易读易懂。
阅读全文