在执行具有错误语法的 SQL 或引发错误的 SQL 时会引发错误。#

exception pandas.errors.DtypeWarning[源代码]#

pandas.errors.DtypeWarning

从文件中读取列中不同的 dtype 时引发的警告。

参见

read_csv

read_csvread_table 在给定 CSV 文件的一列或多列中遇到非统一的 dtype 时会引发此警告。

read_table

将 CSV(逗号分隔)文件读取到 DataFrame。

Notes

将通用分隔文件读取到 DataFrame。

尽管有警告,包含混合数据类型的 CSV 文件中的单个列仍将被读取为对象类型。请参阅下面的示例以更深入地了解此问题。

Examples

此示例创建并读取一个包含 intstr 列的大型 CSV 文件。

>>> df = pd.DataFrame({'a': (['1'] * 100000 + ['X'] * 100000 +
...                          ['1'] * 100000),
...                    'b': ['b'] * 300000})  
>>> df.to_csv('test.csv', index=False)  
>>> df2 = pd.read_csv('test.csv')  
... # DtypeWarning: Columns (0) have mixed types

值得注意的是,df2 对于相同的输入 ‘1’ 将同时包含 strint

>>> df2.iloc[262140, 0]  
'1'
>>> type(df2.iloc[262140, 0])  
<class 'str'>
>>> df2.iloc[262150, 0]  
1
>>> type(df2.iloc[262150, 0])  
<class 'int'>

解决此问题的一种方法是在 read_csvread_table 函数中使用 dtype 参数来显式指定转换:

>>> df2 = pd.read_csv('test.csv', sep=',', dtype={'a': str})  

没有发出警告。