Skip to content

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

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
nbonnotte opened this issue Nov 8, 2015 · 3 comments
Closed
Labels
API Design Indexing Related to indexing on series/frames, not to indexes themselves Output-Formatting __repr__ of pandas objects, to_string
Milestone

Comments

@nbonnotte
Copy link
Contributor

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

@jreback jreback added Indexing Related to indexing on series/frames, not to indexes themselves Output-Formatting __repr__ of pandas objects, to_string API Design labels Nov 8, 2015
@jreback jreback added this to the Next Major Release milestone Nov 8, 2015
@nbonnotte
Copy link
Contributor Author

And there's na_rep, which is also ignored (even if it's an edge case)

@nbonnotte
Copy link
Contributor Author

decimal is also ignored for 0.0 outside indexes:

In [28]: df = pd.DataFrame({'a': [0, 1.1], 'b': [2.2, 3.3]})

In [29]: df
Out[29]: 
     a    b
0  0.0  2.2
1  1.1  3.3

In [31]: print df.to_csv()
,a,b
0,0.0,2.2
1,1.1,3.3


In [32]: print df.to_csv(decimal="^")
,a,b
0,0,2^2
1,1^1,3^3

This is insignificant, but since I've found the place where it happens, I'll correct that as well.

@nbonnotte
Copy link
Contributor Author

Fixed by #11681

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design Indexing Related to indexing on series/frames, not to indexes themselves Output-Formatting __repr__ of pandas objects, to_string
Projects
None yet
Development

No branches or pull requests

2 participants