使用Pandas替换模块

时间:2018-04-10 22:31:16

标签: python pandas

AttributeError:module' re'没有属性' _pattern_type'

我在使用Pandas替换模块尝试使用数值替换数据集中的分类数据时遇到上述错误。我的代码如下。 housng_dataframe是使用Pandas在外壳数据集上创建的数据框。

ordinal_mapping_dict = {"LotShape": {"Reg": 3,"IR1": 2,"IR2": 1,"IR3": 0}, 
                        "ExterQual":{"Ex": 4,"Gd": 3,"TA": 2,"Fa": 1,"Po": 0},
                        "HeatingQC":{"Ex": 4,"Gd": 3,"TA": 2,"Fa": 1,"Po": 0},
                        "KitchenQual":{"Ex": 4,"Gd": 3,"TA": 2,"Fa": 1,"Po": 0},
                        "Functional":{"Typ":7,"Min1":6,"Min2":5,"Mod":4,"Maj1":3,"Maj2":2,"Sev":1,"Sal":0},
                        "PavedDrive":{"Y": 2,"P": 1,"N": 0 }}

housing_dataframe = housing_dataframe.replace(ordinal_mapping_dict)

输出:

AttributeError                            Traceback (most recent call last)
<ipython-input-106-d0830be43fa5> in <module>()
      6                         "PavedDrive":{"Y": 2,"P": 1,"N": 0 }}
      7 
----> 8 housing_dataframe = housing_dataframe.replace(ordinal_mapping_dict)

c:\users\diro\appdata\local\programs\python\python37\lib\site-packages\pandas\core\generic.py in replace(self, to_replace, value, inplace, limit, regex, method, axis)
   4529 
   4530             return self.replace(to_replace, value, inplace=inplace,
-> 4531                                 limit=limit, regex=regex)
   4532         else:
   4533 

c:\users\diro\appdata\local\programs\python\python37\lib\site-packages\pandas\core\generic.py in replace(self, to_replace, value, inplace, limit, regex, method, axis)
   4548                                                     value=value[c],
   4549                                                     inplace=False,
-> 4550                                                     regex=regex)
   4551                     return None if inplace else res
   4552 

c:\users\diro\appdata\local\programs\python\python37\lib\site-packages\pandas\core\generic.py in replace(self, to_replace, value, inplace, limit, regex, method, axis)
   4578                                                        dest_list=value,
   4579                                                        inplace=inplace,
-> 4580                                                        regex=regex)
   4581 
   4582                 else:  # [NA, ''] -> 0

c:\users\diro\appdata\local\programs\python\python37\lib\site-packages\pandas\core\internals.py in replace_list(self, src_list, dest_list, inplace, regex, mgr)
   3500                         convert = i == src_len
   3501                         result = b.replace(s, d, inplace=inplace, regex=regex,
-> 3502                                            mgr=mgr, convert=convert)
   3503                         new_rb = _extend_blocks(result, new_rb)
   3504                     else:

c:\users\diro\appdata\local\programs\python\python37\lib\site-packages\pandas\core\internals.py in replace(self, to_replace, value, inplace, filter, regex, convert, mgr)
   2206         blocks = [self]
   2207 
-> 2208         if not either_list and is_re(to_replace):
   2209             return self._replace_single(to_replace, value, inplace=inplace,
   2210                                         filter=filter, regex=True,

c:\users\diro\appdata\local\programs\python\python37\lib\site-packages\pandas\core\dtypes\inference.py in is_re(obj)
    200     """
    201 
--> 202     return isinstance(obj, re._pattern_type)
    203 
    204 

AttributeError: module 're' has no attribute '_pattern_type'

1 个答案:

答案 0 :(得分:0)

这是pandas替换方法中的bug,自been fixed以来(修复将在0.23.0中)。

Python 3.7删除了私有函数_pattern_type
(参考:https://github.com/spulec/moto/issues/1546).