Skip to content

BUG: DataFrame.to_string() creates extra space for string dtypes #4158

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
michaelaye opened this issue Jul 8, 2013 · 4 comments
Closed

BUG: DataFrame.to_string() creates extra space for string dtypes #4158

michaelaye opened this issue Jul 8, 2013 · 4 comments
Labels
Output-Formatting __repr__ of pandas objects, to_string
Milestone

Comments

@michaelaye
Copy link
Contributor

pd.__version__
'0.12.0.dev-6a0b9ec'

exdf = pd.DataFrame([["aoe",12,3.14],["qjk",34,5.67]])
exdf.columns = list('abc')
#exdf = exdf.reindex(columns=reversed(list('abc')))
def a(inp):
    return '{:>3s},'.format(inp)
def b(inp):
    return '{:2d},'.format(inp)
def c(inp):
    return '{:3.2f},'.format(inp)
print repr(exdf.to_string(formatters={'a':a,'b':b,'c':c},
                          index=False,header=False))

' aoe, 12, 3.14,\n qjk, 34, 5.67,'
^^extra space!

Note, when you reindex the columns to be the other way around by removing the # in front of the line exdf = exdf.reindex ... then you get:

'3.14, 12, aoe,\n5.67, 34, qjk,'
^^no extra space!

where one can easily see, that usually, as it is correct, the first element in the line does NOT get a space. Therefore I consider this a formatting bug for (string) objects.

@michaelaye
Copy link
Contributor Author

FYI, I am using to_string() and not to_csv(), because I need spaces behind the comma that is currently not possible with to_csv(). Also, to_csv() does not accept a formatting dictionary.

@michaelaye
Copy link
Contributor Author

Expected outcome:

for line in exdf.values:
    print repr("{:>3s}, {:2d}, {:3.2f}".format(*line))

'aoe, 12, 3.14'
'qjk, 34, 5.67'

@jreback
Copy link
Contributor

jreback commented Sep 28, 2013

closing in favor of master issue #4668

@jreback jreback closed this as completed Sep 28, 2013
@samuelchodur
Copy link

samuelchodur commented Apr 14, 2023

This bug is not present in 1.5.3 but I am seeing it now in 2.0.0 and the current main branch (output shown below).

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))

results in
image

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

No branches or pull requests

3 participants