Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
761 views
in Technique[技术] by (71.8m points)

python - pandas read_csv column dtype is set to decimal but converts to string

I am using pandas (v0.18.1) to import the following data from a file called 'test.csv':

a,b,c,d
1,1,1,1.0

I have set the dtype to 'decimal.Decimal' for columns 'c' and 'd' but instead they return as type 'str'.

import pandas as pd
import decimal as D

df = pd.read_csv('test.csv', dtype={'a': int, 'b': float, 'c': D.Decimal, 'd': D.Decimal})

for i, v in df.iterrows():
    print(type(v.a), type(v.b), type(v.c), type(v.d))

Results:

`<class 'int'> <class 'float'> <class 'str'> <class 'str'>`

I have also tried converting to decimal explicitly after import with no luck (converting to float works but not decimal).

df.c = df.c.astype(float)
df.d = df.d.astype(D.Decimal)
for i, v in df.iterrows():
    print(type(v.a), type(v.b), type(v.c), type(v.d))

Results:

`<class 'int'> <class 'float'> <class 'float'> <class 'str'>`

The following code converts a 'str' to 'decimal.Decimal' so I don't understand why pandas doesn't behave the same way.

x = D.Decimal('1.0')
print(type(x))

Results:

`<class 'decimal.Decimal'>`
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you need converters:

import pandas as pd
import io
import decimal as D

temp = u"""a,b,c,d
           1,1,1,1.0"""

# after testing replace io.StringIO(temp) to filename
df = pd.read_csv(io.StringIO(temp), 
                 dtype={'a': int, 'b': float}, 
                 converters={'c': D.Decimal, 'd': D.Decimal})

print (df)
       a    b  c    d
    0  1  1.0  1  1.0

for i, v in df.iterrows():
    print(type(v.a), type(v.b), type(v.c), type(v.d))

    <class 'int'> <class 'float'> <class 'decimal.Decimal'> <class 'decimal.Decimal'>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...