Skip to content

BUG: read_csv() wrong reading if comment-character included in na_value #34002

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
2 of 3 tasks
Waljakov opened this issue May 5, 2020 · 2 comments
Open
2 of 3 tasks
Labels
Bug IO CSV read_csv, to_csv

Comments

@Waljakov
Copy link

Waljakov commented May 5, 2020

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

import pandas as pd
from io import StringIO

dataframe_file = (
    "# this is a comment\n"
    "1,2,3,4\n"
    "1,2,3,4#inline comment\n"
    "1,2#,3,4\n"
    "1,2,#N/A,4\n"
)

dataframe = pd.read_csv(StringIO(dataframe_file), comment="#", na_values="#N/A")

print(dataframe)

Output:

   1  2    3    4
0  1  2  3.0  4.0
1  1  2  NaN  NaN
2  1  2  NaN  NaN

Problem description

On the last row, it should read the 3rd value (#N/A) as NA-Value (NaN) and not as a comment.
But because the value #N/A starts with the comment-character # the rest of the line is interpreted as comment and the last value is not read.

Expected Output

   1  2    3    4
0  1  2  3.0  4.0
1  1  2  NaN  NaN
2  1  2  NaN  4.0

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.7.7.final.0
python-bits : 64
OS : Linux
OS-release : 5.6.8-arch1-1
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.0.3
numpy : 1.18.1
pytz : 2020.1
dateutil : 2.8.1
pip : 20.0.2
setuptools : 46.1.3.post20200330
Cython : 0.29.17
pytest : 5.4.1
hypothesis : 5.8.3
sphinx : 3.0.3
blosc : None
feather : None
xlsxwriter : 1.2.8
lxml.etree : 4.5.0
html5lib : 1.0.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.13.0
pandas_datareader: None
bs4 : 4.9.0
bottleneck : 1.3.2
fastparquet : None
gcsfs : None
lxml.etree : 4.5.0
matplotlib : 3.1.3
numexpr : 2.7.1
odfpy : None
openpyxl : 3.0.3
pandas_gbq : None
pyarrow : None
pytables : None
pytest : 5.4.1
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : 1.3.16
tables : 3.6.1
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : 1.3.0
xlsxwriter : 1.2.8
numba : 0.49.0

@Waljakov Waljakov added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 5, 2020
@a9ush
Copy link

a9ush commented May 7, 2020

Add pr template

@Waljakov
Copy link
Author

Waljakov commented May 7, 2020

Hey,
sorry, I am a bit confused. Why do you post "add pr template"?
There is a pr template from pandas available here.
Also I don't understand what does this have to do with my issue? Is there something missing that I should add?

@jbrockmendel jbrockmendel added IO CSV read_csv, to_csv and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO CSV read_csv, to_csv
Projects
None yet
Development

No branches or pull requests

3 participants