nilkn
nilkn

Reputation: 985

Pandas read_csv: AttributeError: 'NoneType' object has no attribute 'dtype'

Suppose I have a file test.csv which looks like this:

A,B,C
Hello,Hi,1

I'm attempting to read this into a Pandas dataframe:

cols = ['A','B','C']
col_types = {'A': str, 'B': str, 'C': int}
test = pd.read_csv('test.csv', names=cols, dtype=col_types)

This produces the error

AttributeError: 'NoneType' object has no attribute 'dtype'

Any ideas?

Upvotes: 0

Views: 8662

Answers (1)

chrisaycock
chrisaycock

Reputation: 37938

Your file already had the header row, so no need to specify any names.

In [6]: test = pd.read_csv('test.csv', dtype=col_types)

In [7]: test
Out[7]:
       A   B  C
0  Hello  Hi  1

In [8]: test.dtypes
Out[8]:
A    object
B    object
C     int64
dtype: object

Upvotes: 3

Related Questions