[英]Seaborn regplot using datetime64 as the x axis
我有一个数据框看起来像这样:
date score
2017-06-04 90
2017-06-03 80
2017-06-02 70
当我尝试这个:
sns.regplot(x=date, y=score, data=df)
我收到一个错误:
TypeError: reduction operation 'mean' not allowed for this dtype
date的dtype是datetime64[ns]
,而score列的int64
。
如何隐藏date
列,以便regplot
起作用?
Seaborn不支持regplot
日期时间,但这是一个丑陋的regplot
:
df = df.sort_values('date')
df['date_f'] = pd.factorize(df['date'])[0] + 1
mapping = dict(zip(df['date_f'], df['date'].dt.date))
ax = sns.regplot('date_f', 'score', data=df)
labels = pd.Series(ax.get_xticks()).map(mapping).fillna('')
ax.set_xticklabels(labels)
产生
这是时间序列回归中使用的主要方法。 如果有每日数据,则将第1天编码为1,并随着日期的增加而增加数字。 假设您有一个规则间隔的时间序列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.