Skip to content

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

Closed
@michaelaye

Description

@michaelaye

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Output-Formatting__repr__ of pandas objects, to_string

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions