【问题标题】:Error during replacement of categorical values in data with numerical values using Pandas replace module使用 Pandas 替换模块将数据中的分类值替换为数值时出错
【发布时间】:2018-04-10 22:31:16
【问题描述】:

AttributeError: 模块 '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'

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    这是 pandas 替换方法中的 bug,它是从 been fixed 开始的(修复将在 0.23.0 中)。

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

    【讨论】:

    • 所以,呃,解决办法就是继续使用python 3.6,直到0.23.0发布!
    猜你喜欢
    • 1970-01-01
    • 2017-03-23
    • 1970-01-01
    • 2021-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-17
    • 2020-01-12
    相关资源
    最近更新 更多