【问题标题】:Python: UserWarning: This pattern has match groups. To actually get the groups, use str.extractPython:用户警告:此模式具有匹配组。要实际获取组,请使用 str.extract
【发布时间】:2017-02-15 12:38:33
【问题描述】:

我有一个数据框,我尝试获取字符串,其中的列包含一些字符串 df看起来像

member_id,event_path,event_time,event_duration
30595,"2016-03-30 12:27:33",yandex.ru/,1
30595,"2016-03-30 12:31:42",yandex.ru/,0
30595,"2016-03-30 12:31:43",yandex.ru/search/?lr=10738&msid=22901.25826.1459330364.89548&text=%D1%84%D0%B8%D0%BB%D1%8C%D0%BC%D1%8B+%D0%BE%D0%BD%D0%BB%D0%B0%D0%B9%D0%BD&suggest_reqid=168542624144922467267026838391360&csg=3381%2C3938%2C2%2C3%2C1%2C0%2C0,0
30595,"2016-03-30 12:31:44",yandex.ru/search/?lr=10738&msid=22901.25826.1459330364.89548&text=%D1%84%D0%B8%D0%BB%D1%8C%D0%BC%D1%8B+%D0%BE%D0%BD%D0%BB%D0%B0%D0%B9%D0%BD&suggest_reqid=168542624144922467267026838391360&csg=3381%2C3938%2C2%2C3%2C1%2C0%2C0,0
30595,"2016-03-30 12:31:45",yandex.ru/search/?lr=10738&msid=22901.25826.1459330364.89548&text=%D1%84%D0%B8%D0%BB%D1%8C%D0%BC%D1%8B+%D0%BE%D0%BD%D0%BB%D0%B0%D0%B9%D0%BD&suggest_reqid=168542624144922467267026838391360&csg=3381%2C3938%2C2%2C3%2C1%2C0%2C0,0
30595,"2016-03-30 12:31:46",yandex.ru/search/?lr=10738&msid=22901.25826.1459330364.89548&text=%D1%84%D0%B8%D0%BB%D1%8C%D0%BC%D1%8B+%D0%BE%D0%BD%D0%BB%D0%B0%D0%B9%D0%BD&suggest_reqid=168542624144922467267026838391360&csg=3381%2C3938%2C2%2C3%2C1%2C0%2C0,0
30595,"2016-03-30 12:31:49",kinogo.co/,1
30595,"2016-03-30 12:32:11",kinogo.co/melodramy/,0

还有另一个带有 url 的 df

url
003\.ru\/[a-zA-Z0-9-_%$#?.:+=|()]+\/mobilnyj_telefon_bq_phoenix
003\.ru\/[a-zA-Z0-9-_%$#?.:+=|()]+\/mobilnyj_telefon_fly_
003\.ru\/sonyxperia
003\.ru\/[a-zA-Z0-9-_%$#?.:+=|()]+\/mobilnye_telefony_smartfony
003\.ru\/[a-zA-Z0-9-_%$#?.:+=|()]+\/mobilnye_telefony_smartfony\/brands5D5Bbr_23
1click\.ru\/sonyxperia
1click\.ru\/[a-zA-Z0-9-_%$#?.:+=|()]+\/chasy-motorola

我用

urls = pd.read_csv('relevant_url1.csv', error_bad_lines=False)
substr = urls.url.values.tolist()
data = pd.read_csv('data_nts2.csv', error_bad_lines=False, chunksize=50000)
result = pd.DataFrame()
for i, df in enumerate(data):
    res = df[df['event_time'].str.contains('|'.join(substr), regex=True)]

但它返回我

UserWarning: This pattern has match groups. To actually get the groups, use str.extract.

我该如何解决这个问题?

【问题讨论】:

    标签: python regex pandas


    【解决方案1】:

    您可以改用str.match。在您的代码中:

    res = df[df['event_time'].str.match('|'.join(substr), regex=True)]
    


    说明

    当正则表达式包含组时,str.contains 会触发警告,例如在正则表达式r'foo(bar)' 中,(bar) 部分被视为一个组,因为它位于括号中。因此,理论上您可以从正则表达式中提取它。

    然而,警告首先没有意义contains 应该只“测试模式或正则表达式是否包含在系列或索引”(pandas documentation)。没有提取组。

    在任何情况下,str.match 都不会抛出警告,并且目前与 str.contains 几乎相同,除了 (1) 字符串必须完全匹配和 (2) 不能从 str.match 停用正则表达式 (@ 987654332@ 有一个regex 参数来停用它们)

    【讨论】:

    • str.match('.*'+regex_string) 具有与str.contains(regex_string) 相同的预期行为,没有警告。只需要注意...regex_string 应该是一个字符串,而不是编译的正则表达式。
    【解决方案2】:

    您应该使用re.escape(yourString) 作为您传递给包含的字符串。

    【讨论】:

      【解决方案3】:

      消除警告的另一种方法是更改​​正则表达式,使其成为匹配组而不是捕获组。那是(?:) 符号。

      因此,如果匹配组为(url1|url2),则应将其替换为(?:url1|url2)

      【讨论】:

      • 最佳答案 imo.
      • 这绝对应该是公认的答案
      【解决方案4】:

      由于提供了regex=Truesublist 被视为正则表达式,在您的情况下,它包含捕获组(用括号括起来的字符串)。

      您会收到警告,因为如果您想捕获某些内容,则无法使用 str.contains返回布尔值,具体取决于提供的模式是否包含在字符串中)

      显然,您可以抑制警告,但修复它更好 他们。

      如果您真的想捕获某些内容,请转义括号块或使用str.extract

      【讨论】:

        【解决方案5】:

        urls 中的至少一个正则表达式模式必须使用捕获组。 str.contains 只为 df['event_time'] 中的每一行返回 True 或 False -- 它不使用捕获组。因此,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.*']})   
        

        避免用户警告。

        【讨论】:

          猜你喜欢
          • 2018-12-06
          • 1970-01-01
          • 1970-01-01
          • 2022-01-11
          • 2021-09-23
          • 2019-01-18
          • 1970-01-01
          • 2015-01-17
          相关资源
          最近更新 更多