Search code examples
pythonpython-3.xpandasnumpyinfinity

how to stop pandas from inferring string value "Infinity" as inf and changing datatype to float64


I have a dataset with a column "Vendor" and it has multiple values as "Infinity", when I use read_csv, pandas automatically converted the Vendor column values to inf and changed its datatype to float64.

This is the test dataset I am using

Vendor, Value
Infinity,1
Infinity,2
Infinity,3
Infinity,4
Infinity,5

This is the result I am getting with inf value and datatype as float64

Vendor  Value
inf     1
inf     2
inf     3
inf     4
inf     5
Name: Vendor, dtype: float64

Is there a way to avoid default infering of Infinity to inf?


Solution

  • You can pass the dtype argument to set explicit column types for particular column names, like so:

    pd.read_csv(file_name, dtype={'Vendor': str})