2012-08-01 53 views
0

DataFrame.to_records type(index)是DatetimeIndex時會報錯。當type(index)是DatetimeIndex時,DataFrame.to_records會報錯

python版本是2.7.3,熊貓是0.8.1。

以下是在IPython shell中發生錯誤的簡單示例代碼。

如何從to_records方法獲得正確的答案,一組記錄。

Python 2.7.3 (default, Apr 20 2012, 22:39:59) 
Type "copyright", "credits" or "license" for more information. 

IPython 0.12.1 -- An enhanced Interactive Python. 
?   -> Introduction and overview of IPython's features. 
%quickref -> Quick reference. 
help  -> Python's own help system. 
object? -> Details about 'object', use 'object??' for extra details. 

In [1]: import os 

In [2]: os.system('pip search pandas') 
pandas     - Powerful data structures for data analysis, time 
          series,and statistics 
    INSTALLED: 0.8.1 (latest) 
Out[2]: 0 

In [3]: from pandas import * 

In [4]: from numpy.random import randn 

In [5]: data = randn(5,3) 

In [6]: index = date_range('2012-08-01', periods=5) 

In [7]: columns = ['a', 'b', 'c'] 

In [8]: df = DataFrame(data, index=index, columns=columns) 

In [9]: df 
Out[9]: 
        a   b   c 
2012-08-01 2.355928 -2.465061 0.240094 
2012-08-02 -0.952323 0.746623 -0.384021 
2012-08-03 1.460156 0.292560 -0.494793 
2012-08-04 -0.989584 -1.630384 1.373587 
2012-08-05 0.014760 -0.789603 -0.622780 

In [10]: df.to_records() 
--------------------------------------------------------------------------- 
ValueError        Traceback (most recent call last) 
/home/ideabong/<ipython-input-10-6d3142e97d2d> in <module>() 
----> 1 df.to_records() 

/usr/local/lib/python2.7/dist-packages/pandas/core/frame.pyc in to_records(self, index) 
    889    names = list(map(str, self.columns)) 
    890 
--> 891   return np.rec.fromarrays(arrays, names=names) 
    892 
    893  @classmethod 

/usr/local/lib/python2.7/dist-packages/numpy/core/records.pyc in fromarrays(arrayList, dtype, shape, formats, names, titles, aligned, byteorder) 
    546  # Determine shape from data-type. 

    547  if len(descr) != len(arrayList): 
--> 548   raise ValueError, "mismatch between the number of fields "\ 
    549    "and the number of arrays" 
    550 

ValueError: mismatch between the number of fields and the number of arrays 

回答

相關問題