代码之家  ›  专栏  ›  技术社区  ›  aerin

类型错误:类型别名不能与IsInstance()一起使用

  •  7
  • aerin  · 技术社区  · 8 年前

    自从我将pandas升级到0.23.0之后,运行一行以删除空白区,就会出现此错误。 df.any_column = df.any_column.str.replace(' ','')

    我收到的错误消息如下:

    /usr/local/lib/python3.5/dist-packages/pandas/core/strings.py in replace(self, pat, repl, n, case, flags, regex)
       2427     def replace(self, pat, repl, n=-1, case=None, flags=0, regex=True):
       2428         result = str_replace(self._data, pat, repl, n=n, case=case,
    -> 2429                              flags=flags, regex=regex)
       2430         return self._wrap_result(result)
       2431
    
    /usr/local/lib/python3.5/dist-packages/pandas/core/strings.py in str_replace(arr, pat, repl, n, case, flags, regex)
        637         raise TypeError("repl must be a string or callable")
        638
    --> 639     is_compiled_re = is_re(pat)
        640     if regex:
        641         if is_compiled_re:
    
    /usr/local/lib/python3.5/dist-packages/pandas/core/dtypes/inference.py in is_re(obj)
        217     """
        218
    --> 219     return isinstance(obj, re_type)
        220
        221
    
    /usr/lib/python3.5/typing.py in __instancecheck__(self, obj)
        258
        259     def __instancecheck__(self, obj):
    --> 260         raise TypeError("Type aliases cannot be used with isinstance().")
        261
        262     def __subclasscheck__(self, cls):
    
    TypeError: Type aliases cannot be used with isinstance().
    

    我该怎么解决这个问题?

    1 回复  |  直到 8 年前
        1
  •  13
  •   TomAugspurger    8 年前

    这是Python3.5.2和更低版本中的一个bug。升级您的python(类似于3.5.4或更高版本应该可以工作),或者等到下一个pandas版本发布,其中包括修复: https://github.com/pandas-dev/pandas/pull/21098