pandas.errors.DtypeWarning#
- exception pandas.errors.DtypeWarning[source]#
Warning raised when reading different dtypes in a column from a file.
Raised when read_csv or read_table cannot re-read a column whose chunks inferred different dtypes, and concatenating them mixes types.
See also
read_csvRead CSV (comma-separated) file into a DataFrame.
read_tableRead general delimited file into a DataFrame.
Notes
With
low_memory=True(the default) dtypes are inferred per chunk, and a column whose chunks disagree is re-read in one pass. This warning is issued where the column cannot be re-read, e.g. from a pipe or in a later chunk of an iterating read. The column is then returned with mixed types, such as an object column.Examples
This example creates and reads a large CSV file with a column that contains int and str.
>>> df = pd.DataFrame( ... { ... "a": (["1"] * 100000 + ["X"] * 100000 + ["1"] * 100000), ... "b": ["b"] * 300000, ... } ... ) >>> df.to_csv("test.csv", index=False)
A script run as
cat test.csv | python script.pyreads it from a pipe, which cannot be rewound:>>> import sys >>> df2 = pd.read_csv(sys.stdin) ... # DtypeWarning: Columns (0: a) have mixed types
Important to notice that
df2will contain both str and int for the same 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 the dtype parameter in the read_csv and read_table functions to explicit the conversion:
>>> df2 = pd.read_csv("test.csv", sep=",", dtype={"a": str})
No warning was issued.