Python 使用datetime64作为x轴的Seaborn regplot

Python 使用datetime64作为x轴的Seaborn regplot,python,pandas,numpy,seaborn,Python,Pandas,Numpy,Seaborn,我有一个如下所示的数据帧: 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 日期的数据类型为datetime64[ns],分数列的数据类型为int64 如何

我有一个如下所示的数据帧:

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
日期的数据类型为
datetime64[ns]
,分数列的数据类型为
int64


如何转换
date
列以使
regplot
工作?

Seaborn不支持
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,并随着天数的增加而增加。这假设您有一个规则间隔的时间序列

Seaborn不支持
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,并随着天数的增加而增加。这假设您有一个规则间隔的时间序列