Pandas reading csv as string type Pandas reading csv as string type python python

Pandas reading csv as string type


Update: this has been fixed: from 0.11.1 you passing str/np.str will be equivalent to using object.

Use the object dtype:

In [11]: pd.read_csv('a', dtype=object, index_col=0)Out[11]:                      A                     B1A  0.35633069074776547     0.7455853988037511B  0.20037376323337375  0.013921830784260236

or better yet, just don't specify a dtype:

In [12]: pd.read_csv('a', index_col=0)Out[12]:           A         B1A  0.356331  0.7455851B  0.200374  0.013922

but bypassing the type sniffer and truly returning only strings requires a hacky use of converters:

In [13]: pd.read_csv('a', converters={i: str for i in range(100)})Out[13]:                      A                     B1A  0.35633069074776547     0.7455853988037511B  0.20037376323337375  0.013921830784260236

where 100 is some number equal or greater than your total number of columns.

It's best to avoid the str dtype, see for example here.


Like Anton T said in his comment, pandas will randomly turn object types into float types using its type sniffer, even you pass dtype=object, dtype=str, or dtype=np.str.

Since you can pass a dictionary of functions where the key is a column index and the value is a converter function, you can do something like this (e.g. for 100 columns).

pd.read_csv('some_file.csv', converters={i: str for i in range(0, 100)})

You can even pass range(0, N) for N much larger than the number of columns if you don't know how many columns you will read.


Use a converter that applies to any column if you don't know the columns before hand:

import pandas as pdclass StringConverter(dict):    def __contains__(self, item):        return True    def __getitem__(self, item):        return str    def get(self, default=None):        return strpd.read_csv(file_or_buffer, converters=StringConverter())