Skip to content

BUG: pd.read_csv ignores columns with only zeros #39906

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

Closed
2 of 3 tasks
JustinGuese opened this issue Feb 19, 2021 · 2 comments
Closed
2 of 3 tasks

BUG: pd.read_csv ignores columns with only zeros #39906

JustinGuese opened this issue Feb 19, 2021 · 2 comments
Labels
Bug IO CSV read_csv, to_csv Needs Info Clarification about behavior needed to assess issue

Comments

@JustinGuese
Copy link

JustinGuese commented Feb 19, 2021

  • 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.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

columns = ["type", "symbol", "Date", "Time", "TSNumber", "Price", "Quantity", "SettlementDate", "Yield", "AccruedInterest", "ConditionCodes", "QuotationBasis", "ExchangeRate", "TradeAttributes"]
df = pd.read_csv("20200803.csv",dtype = str,names=columns)

Problem description

Reading in a csv file with the following line
T,FMG,20200803,000000,10071068,,,20200804,,,XT,,,,
results in pandas ignoring the 4th column, and matches the current column to the next value (10071068) in that case.

Previous reports suggested setting the dtype to string (see https://stackoverflow.com/questions/13250046/how-to-keep-leading-zeros-in-a-column-when-reading-csv-with-pandas), which should be fine if the column is read in as "0000000", but apparently it is not. Am I missing something?

Current output:

-- | -- | -- | -- | --
71068 | FMG | 20200803 | 10071068 | 20200803.0 ...

Expected Output

-- | -- | -- | -- | --
71068 | FMG | 20200803 | 000000 | 10071068 | 20200803.0 ...

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 67a3d42
python : 3.8.5.final.0
python-bits : 64
OS : Linux
OS-release : 5.8.0-7642-generic
Version : #47161228899020.04~b8113e7-Ubuntu SMP Wed Feb 3 02:25:36 UTC 2
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.1.4
numpy : 1.19.4
pytz : 2020.4
dateutil : 2.8.1
pip : 20.3.3
setuptools : 51.3.3.post20210118
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.2
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.19.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : None
fastparquet : 0.5.0
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : 3.0.6
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : 1.3.22
tables : None
tabulate : None
xarray : None
xlrd : 2.0.1
xlwt : None
numba : 0.52.0

@JustinGuese JustinGuese added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 19, 2021
@phofl
Copy link
Member

phofl commented Feb 19, 2021

I am getting

  type    symbol    Date  ... QuotationBasis ExchangeRate TradeAttributes
T  FMG  20200803  000000  ...            NaN          NaN             NaN

where T is moved to the index, cause your names are shorter than the data. This means that the 0 is moved to date.

Please provide something which can be executed and narrow it down to the relevant part. I don't think that most of your columns on the right side of the 0000 are necessary. For example the following is executable:

s = StringIO("""
T,FMG,20200803,000000,10071068,,,20200804,,,XT,,,,
""")
columns = ["type", "symbol", "Date", "Time", "TSNumber", "Price", "Quantity", "SettlementDate", "Yield", "AccruedInterest", "ConditionCodes", "QuotationBasis", "ExchangeRate", "TradeAttributes"]
df = pd.read_csv(s, names=columns, dtype=str)

@phofl phofl added IO CSV read_csv, to_csv Needs Info Clarification about behavior needed to assess issue and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 19, 2021
@MarcoGorelli
Copy link
Member

Hi @JustinGuese - closing for now, will reopen if you include a reproducible example

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 Needs Info Clarification about behavior needed to assess issue
Projects
None yet
Development

No branches or pull requests

3 participants