Skip to content

BUG: to_string(header=False) should not shift the index w.r.t. the data #49230

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
jondo opened this issue Oct 21, 2022 · 5 comments · Fixed by #49738
Closed
2 of 3 tasks

BUG: to_string(header=False) should not shift the index w.r.t. the data #49230

jondo opened this issue Oct 21, 2022 · 5 comments · Fixed by #49738
Assignees
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@jondo
Copy link

jondo commented Oct 21, 2022

Pandas version checks

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

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

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
df = pd.DataFrame([2,3])
df.index.name = 'a'
print(df.to_string(header=False))

Issue Description

The code results in

a  2
0  3
1 

Expected Behavior

a   
0  2
1  3

Installed Versions

pd.show_versions() yields:

INSTALLED VERSIONS

commit : 91111fd
python : 3.9.2.final.0
python-bits : 64
OS : Linux
OS-release : 5.10.0-17-amd64
Version : #1 SMP Debian 5.10.136-1 (2022-08-13)
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_DK.UTF-8
LOCALE : en_DK.UTF-8

pandas : 1.5.1
numpy : 1.23.3
pytz : 2022.4
dateutil : 2.8.2
setuptools : 44.1.1
pip : 22.2.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 7.34.0
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.6.1
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None

@jondo jondo added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 21, 2022
@jondo
Copy link
Author

jondo commented Oct 21, 2022

Workaround: reset df.index.name to None before calling df.to_string.

@vamsi-verma-s
Copy link
Contributor

use index_names=False

print(df.to_string(header=False, index_names=False))
0  2
1  3

@jondo
Copy link
Author

jondo commented Oct 21, 2022

@vamsi-verma-s, thank you for pointing out the index_names argument.

In my case df.to_string(header=False) evaluates to the string 'a 2\n0 3\n1 ', which nonsensically has the index name and the first value of the data column in the same row, and the rest of the index shifted w.r.t the data.

I now updated my report: I expect df.to_string(header=False) to give 'a \n0 2\n1 3', analogous to the ' 0\na \n0 2\n1 3' of df.to_string()`.

@jondo jondo changed the title BUG: to_string(header=False) should also remove the index name BUG: to_string(header=False) should not shift the index w.r.t. the data Oct 21, 2022
@andrewchen1216
Copy link
Contributor

take

@attack68
Copy link
Contributor

Note that Styler.to_string is also a workaround

>>> print(df.style.hide(axis="columns").to_string())
a 
0 2
1 3

>>> print(df.style.hide(axis="columns").hide(axis="index", names=True).to_string())
0 2
1 3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants