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

Closed
nbonnotte opened this Issue Nov 8, 2015 · 3 comments

Comments

Projects
None yet
2 participants
Contributor

nbonnotte commented Nov 8, 2015

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 added this to the Next Major Release milestone Nov 8, 2015

Contributor

nbonnotte commented Nov 19, 2015

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

Contributor

nbonnotte commented Nov 23, 2015

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.

@jreback jreback modified the milestone: 0.18.0, Next Major Release Nov 29, 2015

@nbonnotte nbonnotte pushed a commit to nbonnotte/pandas that referenced this issue Dec 27, 2015

Nicolas Bonnotte API: DataFrame.to_csv formatting parameters for float indexes
Fix issue #11553
9302811
Contributor

nbonnotte commented Dec 28, 2015

Fixed by #11681

nbonnotte closed this Dec 28, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment