Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Ctrl+K

pandas.errors.DtypeWarning#

exceptionpandas.errors.DtypeWarning[source]#

Warning raised when reading different dtypes in a column from a file.

Raised for a dtype incompatibility. This can happen wheneverread_csvorread_table encounter non-uniform dtypes in a column(s) of a givenCSV file.

See also

read_csv

Read CSV (comma-separated) file into a DataFrame.

read_table

Read general delimited file into a DataFrame.

Notes

This warning is issued when dealing with larger files because the dtypechecking happens per chunk read.

Despite the warning, the CSV file is read with mixed types in a singlecolumn which will be an object type. See the examples below to betterunderstand this issue.

Examples

This example creates and reads a large CSV file with a column that containsint andstr.

>>>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

Important to notice thatdf2 will contain bothstr andint for thesame input, ‘1’.

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

One way to solve this issue is using thedtype parameter in theread_csv andread_table functions to explicit the conversion:

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

No warning was issued.


[8]ページ先頭

©2009-2025 Movatter.jp