Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

string - TypeError: Type aliases cannot be used with isinstance()

Ever since I upgraded pandas to 0.23.0, I'm getting this error for running a line to remove whitespace df.any_column = df.any_column.str.replace(' ','')

The error message that I'm getting is below:

/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().

How should I fix this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

That's a bug in Python 3.5.2 and lower. Either upgrade your python (something like 3.5.4 or newer should work), or wait till the next pandas release, which includes the fix: https://github.com/pandas-dev/pandas/pull/21098


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...