0

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?

4
  • 1
    Are you sure that is the error? Commented May 21, 2015 at 20:17
  • what version of pandas are you using? Commented May 21, 2015 at 20:34
  • Yep, it's definitely the error I'm getting. I'm using '0.12.0', according to pd.__version__. Commented May 21, 2015 at 20:35
  • latest is 0.16.1, might be no harm to update Commented May 21, 2015 at 20:36

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

4 Comments

The OP's code should not give the error suggested though no?
@PadraicCunningham The error I get in pandas 0.15.2 is cannot safely convert passed user dtype of <i8 for object dtyped data in column 2. I just figured the OP was using a different version.
@chriscock, yes I tried there and got the same, your approach is obviously the correct one, the error just seems a bit strange.
Thanks, this seems to fix that error. I'd vote up your answer but I can't as I have < 15 reputation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.