您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python:UserWarning:此模式具有匹配组。要实际获得组,请使用str.extract

Python:UserWarning:此模式具有匹配组。要实际获得组,请使用str.extract

中的至少一个正则表达式模式urls必须使用捕获组。 str.contains仅针对其中的每一行返回True或Falsedf['event_time']-不使用捕获组。因此,UserWarning警告您正则表达式使用捕获组,但未使用匹配项。

如果要删除,则UserWarning可以从正则表达式模式中找到并删除捕获组。它们没有显示在您发布的正则表达式模式中,但是它们必须在您的实际文件中。在字符类之外查找括号。

或者,您可以通过以下方式禁止此特定的UserWarning

import warnings
warnings.filterwarnings("ignore", 'This pattern has match groups')

在致电之前str.contains

这是一个简单的示例,演示了问题(和解决方案):

# import warnings
# warnings.filterwarnings("ignore", 'This pattern has match groups') # uncomment to suppress the UserWarning

import pandas as pd

df = pd.DataFrame({ 'event_time': ['gouda', 'stilton', 'gruyere']})

urls = pd.DataFrame({'url': ['g(.*)']})   # With a capturing group, there is a UserWarning
# urls = pd.DataFrame({'url': ['g.*']})   # Without a capturing group, there is no UserWarning. Uncommenting this line avoids the UserWarning.

substr = urls.url.values.tolist()
df[df['event_time'].str.contains('|'.join(substr), regex=True)]

版画

  script.py:10: UserWarning: This pattern has match groups. To actually get the groups, use str.extract.
  df[df['event_time'].str.contains('|'.join(substr), regex=True)]

从正则表达式模式中删除捕获组:

urls = pd.DataFrame({'url': ['g.*']})

避免了UserWarning。

python 2022/1/1 18:25:45 有350人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶