addition of 'float_format' to DataFrame.to_csv?

958 views
Skip to first unread message

yarden

unread,
Oct 21, 2012, 11:23:30 AM10/21/12
to pystat...@googlegroups.com
Hi,

The "float_format" option that appears in the DataFrame.to_csv method is incredibly useful -- I was wondering when it got added to pandas? I see that "float_format" got added to the to string representation almost a year ago, way back in 0.6, but it looks like my pandas 0.8.1 does not have it for DataFrames:

$ python
Python 2.6.5 (r265:79063, Oct  1 2012, 22:04:36) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
>>> a = [{"a": 0.435, "b": 0.43423534634}]
>>> b = pandas.DataFrame(a)
>>> b.to_csv("test_float.txt", float_format="%.2f")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: to_csv() got an unexpected keyword argument 'float_format'
>>> pandas
<module 'pandas' from '/usr/local/lib/python2.6/dist-packages/pandas-0.8.1-py2.6-linux-x86_64.egg/pandas/__init__.pyc'>


Did this method only get added after 0.8.1? I'm asking because many users of the code I'm writing have pandas 0.8.* and will not want to upgrade, so if it's a very new addition, I was hoping someone could recommend a hackier way that is compatible with older pandas releases like 0.8.* to reformat the floats with 'to_csv'. Is there a simple way to do it, or is the answer just not to use 'to_csv' for this and instead serialize the df by hand?  Thanks.

Wes McKinney

unread,
Oct 21, 2012, 12:45:29 PM10/21/12
to pystat...@googlegroups.com
Yes-- it got added after 0.8.1 IIRC.

I strongly recommend that you upgrade to 0.9.0. More than 100 (mostly
very minor, but potentially annoying) bugs were fixed:

http://github.com/pydata/pandas/issues?milestone=15&state=closed

- Wes
Reply all
Reply to author
Forward
0 new messages