从 pd.DataFrame 设置 dtypes 给出 TypeError: object of type 'type' has no len()

Roc*_*etq 5 python types runtime-error python-3.x pandas

假设我有一个数据框,并且想将数据类型设置为所有列,就像我调用read_csv方法一样。为了简单起见,同样的错误TypeError: object of type 'type' has no len() 给出了这段代码:

df = pd.DataFrame([1,2,2,3], columns = ['num'], dtype={'num':int})
Run Code Online (Sandbox Code Playgroud)

这里出了什么问题以及如何让它发挥作用?

完整的错误堆栈:

TypeError                                 Traceback (most recent call last)
<ipython-input-42-e8a84bf74364> in <module>()
----> 1 df = pd.DataFrame([1,2,2,3], columns = ['num'], dtype={'num':int})

C:\Anaconda3\lib\site-packages\pandas\core\frame.py in __init__(self, data, index, columns, dtype, copy)
    264             data = {}
    265         if dtype is not None:
--> 266             dtype = self._validate_dtype(dtype)
    267 
    268         if isinstance(data, DataFrame):

C:\Anaconda3\lib\site-packages\pandas\core\generic.py in _validate_dtype(self, dtype)
    145 
    146         if dtype is not None:
--> 147             dtype = pandas_dtype(dtype)
    148 
    149             # a compound dtype

C:\Anaconda3\lib\site-packages\pandas\core\dtypes\common.py in pandas_dtype(dtype)
   1895 
   1896     try:
-> 1897         npdtype = np.dtype(dtype)
   1898     except (TypeError, ValueError):
   1899         raise

C:\Anaconda3\lib\site-packages\numpy\core\_internal.py in _usefields(adict, align)
     60         names = None
     61     if names is None:
---> 62         names, formats, offsets, titles = _makenames_list(adict, align)
     63     else:
     64         formats = []

C:\Anaconda3\lib\site-packages\numpy\core\_internal.py in _makenames_list(adict, align)
     28     for fname in fnames:
     29         obj = adict[fname]
---> 30         n = len(obj)
     31         if not isinstance(obj, tuple) or n not in [2, 3]:
     32             raise ValueError("entry not a 2- or 3- tuple")

TypeError: object of type 'type' has no len()
Run Code Online (Sandbox Code Playgroud)

IMC*_*ins 7

文档中,我引用:

dtype : dtype,默认 None 强制的数据类型。只允许使用单一数据类型。如果没有,则推断

简而言之,您必须仅指定一种 dtype,并且不能传递字典。

文档中的示例...

df = pd.DataFrame(data=d, dtype=np.int8)
df.dtypes
#col1    int8
#col2    int8
#dtype: object
Run Code Online (Sandbox Code Playgroud)