Skip to content

REGR: to_csv(na_rep) truncates values #31447

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
mdruiter opened this issue Jan 30, 2020 · 4 comments · Fixed by #31513
Closed

REGR: to_csv(na_rep) truncates values #31447

mdruiter opened this issue Jan 30, 2020 · 4 comments · Fixed by #31513
Labels
IO CSV read_csv, to_csv Output-Formatting __repr__ of pandas objects, to_string Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@mdruiter
Copy link
Contributor

Code Sample

>>> import pandas as pd
>>> pd.__version__
'1.0.0rc0'
>>> pd.Series(range(8, 12)).to_csv(na_rep='-')
',0\r\n0,8\r\n1,9\r\n2,1\r\n3,1\r\n'

Problem description

Despite (or due to?) #29975 and #30146, using na_rep still makes to_csv() truncate values. In this case integers.

Expected Output

',0\r\n0,8\r\n1,9\r\n2,10\r\n3,11\r\n'

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit : None python : 3.8.1.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 : Dutch_Netherlands.1252

pandas : 1.0.0rc0
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 41.2.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.10.3
IPython : 7.11.1
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : 3.1.2
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
s3fs : None
scipy : 1.4.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

@mdruiter mdruiter changed the title Still truncating with to_csv(na_rep) Still truncating with to_csv(na_rep) Jan 30, 2020
@jorisvandenbossche jorisvandenbossche added the Regression Functionality that used to work in a prior pandas version label Jan 30, 2020
@jorisvandenbossche jorisvandenbossche added this to the 1.0.1 milestone Jan 30, 2020
@jorisvandenbossche
Copy link
Member

Thanks for the report!

Yes, the itemsize = writers.word_len(na_rep) in the PR you linked seems like a wrong fix. Further investigations always welcome! I tagged it with the 1.0.1 milestone, as this is also a regression compared to 0.25 (#29975 was a new issue)

@jorisvandenbossche jorisvandenbossche added IO CSV read_csv, to_csv Output-Formatting __repr__ of pandas objects, to_string labels Jan 30, 2020
@jorisvandenbossche jorisvandenbossche changed the title Still truncating with to_csv(na_rep) REGR: to_csv(na_rep) truncates values Jan 30, 2020
@fwitte
Copy link

fwitte commented Jan 31, 2020

Hi,
this is happening similarly to me, but in my case it affects pure boolean columns:

import pandas as pd
pd.__version__
'1.0.0'
pd.Series([True, False, 3.1113, 150, -10]).to_csv('outfile.csv', na_rep='nan')

correctly yields

,0
0,True
1,False
2,3.1113
3,150
4,-10

while

pd.Series([True, False, False, True]).to_csv('outfile.csv', na_rep='nan')

results in this:

,0
0,Tru
1,Fal
2,Fal
3,Tru

Output of pd.show_versions()

commit : None
python : 3.6.7.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-76-generic
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : de_DE.UTF-8
LOCALE : de_DE.UTF-8

pandas : 1.0.0
numpy : 1.16.2
pytz : 2019.1
dateutil : 2.8.0
pip : 20.0.2
setuptools : 41.0.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.8.4 (dt dec pq3 ext lo64)
jinja2 : 2.10.3
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : 3.0.3
numexpr : 2.7.1
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : 1.3.11
tables : 3.6.1
tabulate : None
xarray : 0.14.1
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

Have a nice weekend!

@TomAugspurger
Copy link
Contributor

Taking a look at this now.

@TomAugspurger
Copy link
Contributor

TomAugspurger commented Jan 31, 2020

I think https://github.com/pandas-dev/pandas/pull/30146/files#diff-8f51ce8697bdff680b3f71b196c582ee is the bug. That will truncate the values to the length of na_rep, regardless of the length of the string repr values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IO CSV read_csv, to_csv Output-Formatting __repr__ of pandas objects, to_string Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants