Python “float”类型的对象在使用to_stata时没有len()

Python “float”类型的对象在使用to_stata时没有len(),python,pandas,Python,Pandas,我的数据集中有三列,我正试图将它们保存为STATA dta文件。这是我清理数据后运行的最后三行 macro1=macro1.rename(columns={'index':'year', 'Price Index, PCE':'pce','Unemployment Rate':'urate'}) macro1.convert_objects(convert_numeric=True).dtypes macro1[['year','pce', 'urate]].to_stata('file pat

我的数据集中有三列,我正试图将它们保存为STATA dta文件。这是我清理数据后运行的最后三行

macro1=macro1.rename(columns={'index':'year', 'Price Index, PCE':'pce','Unemployment Rate':'urate'})
macro1.convert_objects(convert_numeric=True).dtypes
macro1[['year','pce', 'urate]].to_stata('file path\file name.dta', write_index=False)
这些是这些变量的数据类型

year     float64
pce      float64
urate    float64
dtype: object
问题是,当我尝试将这些列转换为.dta时,会收到一条错误消息

--------------------------------------------------------------------------- TypeError                                 Traceback (most recent call last) <ipython-input-69-a2069ee823e7> in <module>()
     36 macro1=macro1.rename(columns={'index':'year', 'Price Index, PCE':'pce','Unemployment Rate':'urate'})
     37 macro1.convert_objects(convert_numeric=True).dtypes
---> 38 macro1[['pce']].to_stata('file path\file name.dta', write_index=False)
     39 #macro1

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()
问题在于尿酸盐和四氯乙烯,因为当我尝试每年只储蓄一次时,效果很好

我不确定问题出在哪里。任何帮助都将不胜感激。

convert\u对象不会转换现有的数据类型,因此您需要分配操作:

macro1 = macro1.convert_objects(convert_numeric=True)

请参见

您必须分配convert\u对象的结果:macro1=macro1。convert\u对象convert\u numeric=Trueoops。谢谢就这样。