Skip to content

additional leading space when using formatters on object column #26002

Open
@simonjayhawkins

Description

@simonjayhawkins

Code Sample, a copy-pastable example if possible

code sample adapted from test_to_string_with_formatters in pandas\tests\io\formats\test_format.py

from pandas import DataFrame

df = DataFrame({'int': [1, 2, 3],
                'float': [1.0, 2.0, 3.0],
                'object': [(1, 2), True, False]},
               columns=['int', 'float', 'object'])

formatters = [('int', lambda x: '0x{x:x}'.format(x=x)),
              ('float', lambda x: '[{x: 4.1f}]'.format(x=x)),
              ('object', lambda x: '-{x!s}-'.format(x=x))]
print(df.to_string(formatters=dict(formatters)))
  int  float    object
0 0x1 [ 1.0]  -(1, 2)-
1 0x2 [ 2.0]    -True-
2 0x3 [ 3.0]   -False-

Problem description

2 spaces between float and object columns, only 1 between int and float

Expected Output

even spacing

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.7.2.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 58 Stepping 9, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.25.0.dev0+376.gfc5f29237
pytest: 4.3.0
pip: 19.0.3
setuptools: 40.6.3
Cython: 0.29.5
numpy: 1.16.2
scipy: 1.2.1
pyarrow: 0.11.1
xarray: 0.11.3
IPython: 7.3.0
sphinx: 1.8.4
patsy: 0.5.1
dateutil: 2.8.0
pytz: 2018.9
blosc: None
bottleneck: 1.2.1
tables: 3.4.4
numexpr: 2.6.9
feather: None
matplotlib: 3.0.2
openpyxl: 2.6.0
xlrd: 1.2.0
xlwt: 1.3.0
xlsxwriter: 1.1.5
lxml.etree: 4.3.1
bs4: 4.7.1
html5lib: 1.0.1
sqlalchemy: 1.2.18
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: 0.2.0
fastparquet: 0.2.1
pandas_gbq: None
pandas_datareader: None
gcsfs: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions