Python 来自熊猫数据帧的seaborn时间序列

Python 来自熊猫数据帧的seaborn时间序列,python,pandas,seaborn,Python,Pandas,Seaborn,我正在努力解决一个似乎非常简单的问题:如何让seaborn从熊猫数据框绘制时间序列折线图。我做错了什么 import seaborn as sns import pandas as pd df=pd.DataFrame({"Date":["2015-03-03","2015-03-02","2015-03-01"],"Close":[1,3,2]}) df["Date"]=pd.to_datetime(df["Date"])#Not sure if seaborn can parse strin

我正在努力解决一个似乎非常简单的问题:如何让seaborn从熊猫数据框绘制时间序列折线图。我做错了什么

import seaborn as sns
import pandas as pd
df=pd.DataFrame({"Date":["2015-03-03","2015-03-02","2015-03-01"],"Close":[1,3,2]})
df["Date"]=pd.to_datetime(df["Date"])#Not sure if seaborn can parse strings as dates
sns.tsplot(data=df,unit=None, time="Date", value="Close")
我得到了这个回溯:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-306-20e252f661c2> in <module>()
      1 df=pd.DataFrame({"Date":["2015-03-03","2015-03-02","2015-03-01"],"Close":[1,3,2]})
      2 df["Date"]=pd.to_datetime(df["Date"])
----> 3 sns.tsplot(data=df,unit=None, time="Date", value="Close")

C:\Anaconda\lib\site-packages\seaborn\timeseries.pyc in tsplot(data, time, unit, condition, value, err_style, ci, interpolate, color, estimator, n_boot, err_palette, err_kws, legend, ax, **kwargs)
    275     for c, (cond, df_c) in enumerate(data.groupby(condition, sort=False)):
    276 
--> 277         df_c = df_c.pivot(unit, time, value)
    278         x = df_c.columns.values.astype(np.float)
    279 

C:\Anaconda\lib\site-packages\pandas\core\frame.pyc in pivot(self, index, columns, values)
   3507         """
   3508         from pandas.core.reshape import pivot
-> 3509         return pivot(self, index=index, columns=columns, values=values)
   3510 
   3511     def stack(self, level=-1, dropna=True):

C:\Anaconda\lib\site-packages\pandas\core\reshape.pyc in pivot(self, index, columns, values)
    324     else:
    325         indexed = Series(self[values].values,
--> 326                          index=MultiIndex.from_arrays([self[index],
    327                                                        self[columns]]))
    328         return indexed.unstack(columns)

C:\Anaconda\lib\site-packages\pandas\core\frame.pyc in __getitem__(self, key)
   1795             return self._getitem_multilevel(key)
   1796         else:
-> 1797             return self._getitem_column(key)
   1798 
   1799     def _getitem_column(self, key):

C:\Anaconda\lib\site-packages\pandas\core\frame.pyc in _getitem_column(self, key)
   1802         # get column
   1803         if self.columns.is_unique:
-> 1804             return self._get_item_cache(key)
   1805 
   1806         # duplicate columns & possible reduce dimensionaility

C:\Anaconda\lib\site-packages\pandas\core\generic.pyc in _get_item_cache(self, item)
   1082         res = cache.get(item)
   1083         if res is None:
-> 1084             values = self._data.get(item)
   1085             res = self._box_item_values(item, values)
   1086             cache[item] = res

C:\Anaconda\lib\site-packages\pandas\core\internals.pyc in get(self, item, fastpath)
   2858                         loc = indexer.item()
   2859                     else:
-> 2860                         raise ValueError("cannot label index with a null key")
   2861 
   2862             return self.iget(loc, fastpath=fastpath)

ValueError: cannot label index with a null key
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在()
1 df=pd.数据帧({“日期”:[“2015-03-03”、“2015-03-02”、“2015-03-01”],“结束”:[1,3,2]})
2 df[“日期”]=pd.to_datetime(df[“日期”])
---->3 sns.tsplot(数据=df,单位=None,时间=Date,值=Close)
tsplot中的C:\Anaconda\lib\site packages\seaborn\timeseries.pyc(数据、时间、单位、条件、值、错误样式、ci、插值、颜色、估计器、n_引导、错误调色板、错误kws、图例、ax、**kwargs)
275对于枚举(data.groupby(condition,sort=False))中的c,(cond,df_c):
276
-->277 df_c=df_c.枢轴(单位、时间、值)
278 x=df_c.columns.values.astype(np.float)
279
pivot中的C:\Anaconda\lib\site packages\pandas\core\frame.pyc(self、index、columns、value)
3507         """
3508从pandas.core.reforme导入轴
->3509返回轴(self,index=index,columns=columns,values=values)
3510
3511 def堆栈(自身,级别=-1,dropna=真):
pivot中的C:\Anaconda\lib\site packages\pandas\core\reformate.pyc(self、index、columns、values)
324其他:
325索引=系列(自[值])。值,
-->326 index=多索引。从_数组([self[index],
327自我[列]])
328返回索引。取消堆栈(列)
C:\Anaconda\lib\site packages\pandas\core\frame.pyc in\uuuuu getitem\uuuuuuuu(self,key)
1795返回自我。\u获取项目\u多级(键)
1796其他:
->1797返回自我。\u获取项目\u列(键)
1798
1799 def_getitem_列(自身,键):
C:\Anaconda\lib\site packages\pandas\core\frame.pyc在_getitem_列中(self,key)
1802#获取列
1803如果self.columns.u是唯一的:
->1804返回自我。获取项目缓存(密钥)
1805
1806#重复列和可能的降维
C:\Anaconda\lib\site packages\pandas\core\generic.pyc在\u get\u item\u缓存中(self,item)
1082 res=cache.get(项)
1083如果res为无:
->1084值=自身数据获取(项目)
1085 res=自身。_框_项_值(项,值)
1086缓存[项目]=分辨率
get中的C:\Anaconda\lib\site packages\pandas\core\internals.pyc(self、item、fastpath)
2858 loc=索引器项()
2859其他:
->2860 raise VALUERROR(“无法使用空键标记索引”)
2861
2862返回self.iget(loc,fastpath=fastpath)
ValueError:无法使用空键标记索引

不幸的是,我在文档中没有找到任何答案。

seaborn的
tsplot
不是用来绘制简单的时间序列线图,而是用来绘制不确定性,请参见:

对于线图,您可以简单地执行以下操作

df.set_index('Date').plot()

我仍然认为seaborn应该涵盖这个简单的案例。我同意这一点。