Skip to content

BUG: to_string with index=False incorrectly shifts truncation (ellipsis) column #40904

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
3 tasks done
mikeronayne opened this issue Apr 12, 2021 · 3 comments · Fixed by #40907 or #41223
Closed
3 tasks done

BUG: to_string with index=False incorrectly shifts truncation (ellipsis) column #40904

mikeronayne opened this issue Apr 12, 2021 · 3 comments · Fixed by #40907 or #41223
Labels
Bug Output-Formatting __repr__ of pandas objects, to_string
Milestone

Comments

@mikeronayne
Copy link
Contributor

mikeronayne commented Apr 12, 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

import pandas as pd
df = pd.DataFrame([[0]*100] * 10)
print(df.to_string(index=False, max_cols=20))

Output

 0   1   2   3   4   5   6   7   8   9   90  ...  91  92  93  94  95  96  97  98  99
  0   0   0   0   0   0   0   0   0   0   0  ...   0   0   0   0   0   0   0   0   0
  0   0   0   0   0   0   0   0   0   0   0  ...   0   0   0   0   0   0   0   0   0
  0   0   0   0   0   0   0   0   0   0   0  ...   0   0   0   0   0   0   0   0   0
  0   0   0   0   0   0   0   0   0   0   0  ...   0   0   0   0   0   0   0   0   0
  0   0   0   0   0   0   0   0   0   0   0  ...   0   0   0   0   0   0   0   0   0
  0   0   0   0   0   0   0   0   0   0   0  ...   0   0   0   0   0   0   0   0   0
  0   0   0   0   0   0   0   0   0   0   0  ...   0   0   0   0   0   0   0   0   0
  0   0   0   0   0   0   0   0   0   0   0  ...   0   0   0   0   0   0   0   0   0
  0   0   0   0   0   0   0   0   0   0   0  ...   0   0   0   0   0   0   0   0   0
  0   0   0   0   0   0   0   0   0   0   0  ...   0   0   0   0   0   0   0   0   0

Problem description

to_string is placing the truncation column (i.e. the column with the ellipses) one position too far to the right. This should not happen.

Expected Output

 0   1   2   3   4   5   6   7   8   9   ...  90  91  92  93  94  95  96  97  98  99
  0   0   0   0   0   0   0   0   0   0  ...   0   0   0   0   0   0   0   0   0   0
  0   0   0   0   0   0   0   0   0   0  ...   0   0   0   0   0   0   0   0   0   0
  0   0   0   0   0   0   0   0   0   0  ...   0   0   0   0   0   0   0   0   0   0
  0   0   0   0   0   0   0   0   0   0  ...   0   0   0   0   0   0   0   0   0   0
  0   0   0   0   0   0   0   0   0   0  ...   0   0   0   0   0   0   0   0   0   0
  0   0   0   0   0   0   0   0   0   0  ...   0   0   0   0   0   0   0   0   0   0
  0   0   0   0   0   0   0   0   0   0  ...   0   0   0   0   0   0   0   0   0   0
  0   0   0   0   0   0   0   0   0   0  ...   0   0   0   0   0   0   0   0   0   0
  0   0   0   0   0   0   0   0   0   0  ...   0   0   0   0   0   0   0   0   0   0
  0   0   0   0   0   0   0   0   0   0  ...   0   0   0   0   0   0   0   0   0   0

Output of pd.show_versions()

INSTALLED VERSIONS

commit : f2c8480
python : 3.9.4.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19041
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 9, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 1.2.3
numpy : 1.20.2
pytz : 2021.1
dateutil : 2.8.1
pip : 21.0.1
setuptools : 54.1.2
Cython : None
pytest : 6.2.3
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.3
IPython : 7.22.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@mikeronayne mikeronayne added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 12, 2021
@mikeronayne
Copy link
Contributor Author

mikeronayne commented Apr 12, 2021

I think DataFrameFormatter.tr_col_num needs to account for self.index being False. It could do this in DataFrameFormatter._truncate_horizontally. Then we should see the expected behavior from _insert_dot_separator_horizontal in pandas\io\formats\string.py. I will try to submit a PR.

@jreback jreback added this to the 1.3 milestone Apr 13, 2021
@jreback jreback added Output-Formatting __repr__ of pandas objects, to_string and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 13, 2021
@mikeronayne
Copy link
Contributor Author

mikeronayne commented Apr 29, 2021

Turns out there's an issue with the vertical truncation logic as well. The first column's truncation row is incorrectly left-justified when it should be right-justified. (See column 0 in the example)

The fix in the PR above then added another issue. The truncation row of the next column after the truncation column will always be ... when it should sometimes be .. (See column 6 in the example)

pd.DataFrame([[0]*11]*4).to_string(max_rows=2, max_cols=10, index=False)

Output

 0   1   2   3   4   ...  6    7   8   9   10
  0   0   0   0   0  ...   0    0   0   0   0
..   ..  ..  ..  ..  ...  ...  ..  ..  ..  ..
  0   0   0   0   0  ...   0    0   0   0   0

I'll submit another PR.

@mikeronayne
Copy link
Contributor Author

@jreback Should I open a new issue for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Output-Formatting __repr__ of pandas objects, to_string
Projects
None yet
2 participants