DataFrame.to_records
当 type(index) 为 DatetimeIndex 时给出错误。
python版本是2.7.3,pandas是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