FutureWarning:正则表达式的默认值将在未来版本中从True变为False
我正在运行下面的代码来清理文本
import pandas as pd
def not_regex(pattern):
return r"((?!{}).)".format(pattern)
tmp = pd.DataFrame(['No one has a European accent either @',
'That the kid reminds me of Kevin'])
tmp[0].str.replace(not_regex('(b[-/]b|[a-zA-Z0-9])'), ' ')
然后它返回一个警告
<ipython-input-8-ef8a43f91dbd>:9: FutureWarning: The default value of regex will change from True to False in a future version.
tmp[0].str.replace(not_regex('(b[-/]b|[a-zA-Z0-9])'), ' ')
你能详细说明这个警告的原因吗?
回答
请参阅Pandas 1.2.0 发行说明:
在未来版本中,regex for的默认值
Series.str.replace()将从True更改为False。此外,当设置regex=True时,单字符正则表达式不会被视为文字字符串(GH24804)
即,现在明确使用正则表达式:
dframe['colname'] = dframe['colname'].str.replace(r'D+', regex=True)
- @Paradigm I think you replace fixed strings, use `regex=False` as the third argument in all the `replace` calls.
THE END
二维码