Skip to content

DataFrame.to_csv ignores some formatting parameters for float indexes #11553

Closed
@nbonnotte

Description

@nbonnotte

xref #11551

Parameter float_format and decimal options are ignored in an Index, but work in the data itself.

In [2]: pd.DataFrame({'a': [0.1,1.1], 'b': [2, 3]}).to_csv(float_format='%.2f', index=False)
Out[2]: 'a,b\n0.10,2\n1.10,3\n

In [3]: pd.DataFrame({'a': [0.1,1.1], 'b': [2, 3]}).set_index('a').to_csv(float_format='%.2f')
Out[3]: 'a,b\n0.1,2\n1.1,3\n'

and

In [4]: pd.DataFrame({'a': [0.1,1.1], 'b': [2, 3]}).to_csv(decimal='^', index=False)
Out[4]: 'a,b\n0^1,2\n1^1,3\n'

In [4]: pd.DataFrame({'a': [0.1,1.1], 'b': [2, 3]}).set_index('a').to_csv(decimal='^')
Out[4]: 'a,b\n0.1,2\n1.1,3\n'

I'll do a PR, soon I hope :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    API DesignIndexingRelated to indexing on series/frames, not to indexes themselvesOutput-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