中的至少一个正则表达式模式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。