DougKruger DougKruger - 18 days ago 6
Python Question

Prevent pandas.read_csv from inferring dtypes

How to prevent

pandas.read_csv()
from inferring the data types. For example, its converting strings
true
and
false
to Bool:
True
and
False
. The columns are many for many files, therefore not feasible to do:

df['field_name'] = df['field_name'].astype(np.float64)
for each of the columns in each file. I prefer pandas to just read file as it is, no type inferring.

Answer

Use the parameter dtype=object for Pandas to keep the data as such at load time.