Skip to content

BUG: DataFrame.to_string() creates extra space for string dtypes in pandas >=2.0 #52690

Closed
@samuelchodur

Description

@samuelchodur

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

print(pd.__version__)

# Expected Output
col1 = 'TEST'
col2 = 'PANDAS'
col3 = 'to_string'
print(f'{col1:<6s} {col2:<7s} {col3:<10s}')

# to_string output
df = pd.DataFrame([{'col1': 'TEST', 'col2': 'PANDAS',
                    'col3': 'to_string'}])
d = {'col1': '{:<6s}'.format,
     'col2': '{:<7s}'.format,
     'col3': '{:<10s}'.format}
print(df.to_string(index=False, header=False,
                   formatters=d))

Issue Description

Extra spaces get added when using to_string() in pandas 2.0 and the main branch.

image

Possibly related:
#4158
#4668

Was advised in Slack to create a new issue, as those bug reports are quite old.

Expected Behavior

Issue not present in pandas 1.5.3.

image

Installed Versions

INSTALLED VERSIONS (non-working)

commit : b661313
python : 3.8.16.final.0
python-bits : 64
OS : Linux
OS-release : 6.1.23-1-MANJARO
Version : #1 SMP PREEMPT_DYNAMIC Thu Apr 6 19:47:04 UTC 2023
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_US.utf8
LOCALE : en_US.UTF-8

pandas : 2.1.0.dev0+527.gb661313cf5.dirty
numpy : 1.23.5
pytz : 2023.3
dateutil : 2.8.2
setuptools : 67.6.1
pip : 23.1
Cython : 0.29.33
pytest : 7.3.1
hypothesis : 6.71.0
sphinx : 4.5.0
blosc : None
feather : None
xlsxwriter : 3.0.9
lxml.etree : 4.9.2
html5lib : 1.1
pymysql : 1.0.3
psycopg2 : 2.9.3
jinja2 : 3.1.2
IPython : 8.12.0
pandas_datareader: None
bs4 : 4.12.2
bottleneck : 1.3.7
brotli :
fastparquet : 2023.2.0
fsspec : 2023.4.0
gcsfs : 2023.4.0
matplotlib : 3.6.3
numba : 0.56.4
numexpr : 2.8.4
odfpy : None
openpyxl : 3.1.0
pandas_gbq : None
pyarrow : 11.0.0
pyreadstat : 1.2.1
pyxlsb : 1.0.10
s3fs : 2023.4.0
scipy : 1.10.1
snappy :
sqlalchemy : 2.0.9
tables : 3.8.0
tabulate : 0.9.0
xarray : 2023.1.0
xlrd : 2.0.1
zstandard : 0.19.0
tzdata : 2023.3
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

Labels

BugOutput-Formatting__repr__ of pandas objects, to_stringRegressionFunctionality that used to work in a prior pandas version

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions