2013-01-25 93 views
1

第一,如果我用DataReader的数据读取,然后情节,一切都很好日期显示不正常时的情节大熊猫据帧

In [55]: t = DataReader('SPY','yahoo', start=datetime.datetime(1990,1,1)) 

In [56]: t 
Out[56]: 
<class 'pandas.core.frame.DataFrame'> 
Index: 5033 entries, 1993-01-29 00:00:00 to 2013-01-23 00:00:00 
Data columns: 
Open   5033 non-null values 
High   5033 non-null values 
Low   5033 non-null values 
Close  5033 non-null values 
Volume  5033 non-null values 
Adj Close 5033 non-null values 
dtypes: float64(5), int64(1) 

In [58]: t.plot() 
Out[58]: <matplotlib.axes.AxesSubplot at 0x8cca790> 

但是,如果我把它保存为一个CSV文件,并重新加载再次,我得到错误信息,情节是不完全正确要么,

In [62]: t.to_csv('spy.csv') 

In [63]: s = pd.read_csv('spy.csv', na_values=[" "]) 

In [64]: s.set_index('Date') 
Out[64]: 
<class 'pandas.core.frame.DataFrame'> 
Index: 5033 entries, 1993-01-29 00:00:00 to 2013-01-23 00:00:00 
Data columns: 
Open   5033 non-null values 
High   5033 non-null values 
Low   5033 non-null values 
Close  5033 non-null values 
Volume  5033 non-null values 
Adj Close 5033 non-null values 
dtypes: float64(5), int64(1) 

In [66]: s.plot()                
--------------------------------------------------------------------------- 
AttributeError       Traceback (most recent call last) 
/home/dli/pythonTest/pandas/<ipython-input-66-d3eb09d34df4> in <module>() 
----> 1 s.plot()                

/usr/lib/pymodules/python2.7/pandas/core/frame.pyc in plot(self, subplots, sharex, sharey, use_index, figsize, grid, legend, rot, ax, kind, **kwds) 
3748      ax.legend(loc='best')        
3749     else:             
-> 3750      ax.plot(x, y, label=str(col), **kwds)   
3751                   
3752     ax.grid(grid)           

/usr/lib/pymodules/python2.7/matplotlib/axes.pyc in plot(self, *args, **kwargs) 
3891   lines = []              
3892                   
-> 3893   for line in self._get_lines(*args, **kwargs):    
3894    self.add_line(line)           
3895    lines.append(line)           

/usr/lib/pymodules/python2.7/matplotlib/axes.pyc in _grab_next_args(self, *args, **kwargs) 
    320     return            
    321    if len(remaining) <= 3:         
--> 322     for seg in self._plot_args(remaining, kwargs):  
    323      yield seg          
    324     return            

/usr/lib/pymodules/python2.7/matplotlib/axes.pyc in _plot_args(self, tup, kwargs) 
    279   ret = []              
    280   if len(tup) > 1 and is_string_like(tup[-1]):     
--> 281    linestyle, marker, color = _process_plot_format(tup[-1]) 
    282    tup = tup[:-1]           
    283   elif len(tup) == 3:           

/usr/lib/pymodules/python2.7/matplotlib/axes.pyc in _process_plot_format(fmt) 
    93  # handle the multi char special cases and strip them from the  

    94  # string               

---> 95  if fmt.find('--')>=0:           
    96   linestyle = '--'            
    97   fmt = fmt.replace('--', '')         

AttributeError: 'numpy.ndarray' object has no attribute 'find'    

不知道如何解决它?

谢谢。 丹

回答

1

set_index方法默认返回一个新的数据帧,而不是将这种就地(其实大部分大熊猫功能是相似的)。它有一个inplace说法:

s.set_index('Date', inplace=True) 
s.plot() 

按预期其中工程!

注:该指标转换为DatetimeIndex你可以使用to_datetime

s.index = s.index.to_datetime() 

这就是说,s保持不变的你.set_index('Date')

In [63]: s = pd.read_csv('spy.csv', na_values=[" "]) 

In [64]: s.set_index('Date') 
Out[64]: 
<class 'pandas.core.frame.DataFrame'> 
Index: 5033 entries, 1993-01-29 00:00:00 to 2013-01-23 00:00:00 
Data columns: 
Open   5033 non-null values 
High   5033 non-null values 
Low   5033 non-null values 
Close  5033 non-null values 
Volume  5033 non-null values 
Adj Close 5033 non-null values 
dtypes: float64(5), int64(1) 

In [65]: s 
Out[65]: 
<class 'pandas.core.frame.DataFrame'> 
Int64Index: 5033 entries, 0 to 5032 
Data columns: 
Date   5033 non-null values 
Open   5033 non-null values 
High   5033 non-null values 
Low   5033 non-null values 
Close  5033 non-null values 
Volume  5033 non-null values 
Adj Close 5033 non-null values 
dtypes: float64(5), int64(1), object(1) 
+0

感谢安迪,然而,当我使用就地=真,我得到了以下错误“ValueError异常:无效的文字浮法():2013 -01-23 00:00:00“ – user1591487

+0

@ user1591487你使用的是什么版本的熊猫? (它在我的机器上正常工作...) –

+0

我使用熊猫0.7.0 – user1591487