Skip to content

pd.read_csv automatically casts strings into int/floatΒ #31821

@rusiano

Description

@rusiano

Code Sample, a copy-pastable example if possible

import pandas as pd
import csv

d = pd.DataFrame([
    ['1', '2', 3], 
    ['3', 4, 5]])

d.to_csv('test_d', quoting=csv.QUOTE_NONNUMERIC, index=False)

dr = pd.read_csv('test_d')
dr.dtypes

0 int64
1 int64
2 int64
dtype: object

Problem description

I have string columns that I write as strings, but when I read them I find out that they have been converted to numeric even though the csv has all the necessary quoting characters. I do not understand why they are converted to numeric if they are strings, the fact that the characters reperesent numbers should make no difference.

The option of using the parameter dtype=... is not an option for me because I do not necessarily know the names of all the columns.

Expected Output

The expected output is finding string where there are strings and numbers where there are numbers.

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line]

INSTALLED VERSIONS

commit : None
python : 3.7.4.final.0
python-bits : 64
OS : Windows
OS-release : 10
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 12, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.None

pandas : 0.25.1
numpy : 1.16.5
pytz : 2019.3
dateutil : 2.8.0
pip : 19.2.3
setuptools : 41.4.0
Cython : 0.29.13
pytest : 5.2.1
hypothesis : None
sphinx : 2.2.0
blosc : None
feather : None
xlsxwriter : 1.2.1
lxml.etree : 4.4.1
html5lib : 1.0.1
pymysql : None
psycopg2 : None
jinja2 : 2.10.3
IPython : 7.8.0
pandas_datareader: None
bs4 : 4.8.0
bottleneck : 1.2.1
fastparquet : None
gcsfs : None
lxml.etree : 4.4.1
matplotlib : 3.1.1
numexpr : 2.7.0
odfpy : None
openpyxl : 3.0.0
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : 0.2.2
scipy : 1.3.1
sqlalchemy : 1.3.9
tables : 3.5.2
xarray : None
xlrd : 1.2.0
xlwt : 1.3.0
xlsxwriter : 1.2.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDtype ConversionsUnexpected or buggy dtype conversionsIO CSVread_csv, to_csv

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions