1

I'm tying to write a pandas data frame to a stata .dta file. Following the advice given in Save .dta files in python, I wrote:

import pandas as pd
df.to_stata(workdir+' generosity.dta')

and I got an error message TypeError: object of type 'float' has no len() and I'm not sure what this means.

most columns in df are objects, but there are three columns that are float64.

I tried following another method(as described in this post Convert .CSV files to .DTA files in Python) via rpy2, but when i tried to install it, I received an error message "Error: tried to guess R's home but no r commnand in the path" so I've given up on it (I have R on my computer but have not used it once)

Thank you very much.

edit: here is the result:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-140-7a8f8bc8d446> in <module>()
      1 #write the dataframe as a Stata file
----> 2 df.to_stata(workdir+group+' generosity.dta')

C:\Users\chungk\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\core\frame.pyc in to_stata(self, fname, convert_dates, write_index, encoding, byteorder, time_stamp, data_label)
   1262                              time_stamp=time_stamp, data_label=data_label,
   1263                              write_index=write_index)
-> 1264         writer.write_file()
   1265 
   1266     @Appender(fmt.docstring_to_string, indents=1)

C:\Users\chungk\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\io\stata.pyc in write_file(self)
   1245         self._write(_pad_bytes("", 5))
   1246         if self._convert_dates is None:
-> 1247             self._write_data_nodates()
   1248         else:
   1249             self._write_data_dates()

C:\Users\chungk\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\io\stata.pyc in _write_data_nodates(self)
   1327                     if var is None or var == np.nan:
   1328                         var = _pad_bytes('', typ)
-> 1329                     if len(var) < typ:
   1330                         var = _pad_bytes(var, typ)
   1331                     if compat.PY3:

TypeError: object of type 'float' has no len()
Community
  • 1
  • 1
chungkim271
  • 927
  • 1
  • 10
  • 20
  • Could you add what the result of `workdir+' generosity.dta'` is just to rule out whether the path is incorrect – EdChum May 08 '15 at 15:01
  • I posted the result. – chungkim271 May 08 '15 at 15:20
  • Does something like `df.to_stata('temp.dta')` work? I wonder if the space in the name/path could be causing a problem. – JohnE May 08 '15 at 18:32
  • Unfortunately, it still doesn't work. I used the same expression to write to an excel file and that works, so I'm excluding that possibility at the moment. Thanks though! – chungkim271 May 08 '15 at 18:51
  • I can't tell from that error message if the problem was with a float or with an attempt to convert an object to float? Not sure how `pandas.to_stata()` deals with that. I guess for starters you should try to figure out exactly which column is the problem. Why do you have so many columns that are objects? Are they just strings? – JohnE May 08 '15 at 19:02
  • At your recommendation, I identified the columns that were causing problems and coverted them to string. After that, it worked! I'm not sure why only certain columns were causing errors except that they contained much longer strings than the others. Thank you! – chungkim271 May 08 '15 at 19:24

0 Answers0