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

前端 未结 5 2187
隐瞒了意图╮
隐瞒了意图╮ 2020-12-09 16:17

I have a dataframe and I try to get string, where on of column contain some string Df looks like

member_id,event_path,event_time,event_duration
30595,\"2016-         


        
5条回答
  •  时光说笑
    2020-12-09 16:55

    You can use str.match instead. In your code:

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


    Explanation

    The warning is triggered by str.contains when the regular expression includes groups, e.g. in the regex r'foo(bar)', the (bar) part is considered a group because it is in parenthesis. Therefore you could theoretically extract that from a regex.

    However, the warning doesn't make sense in the first place, contains is supposed to only "test if pattern or regex is contained within a string of a Series or Index" (pandas documentation). There is nothing about extracting groups.

    In any case, str.match does not throw the warning, and currently does almost the same as str.contains except that (1) the string must exactly match and (2) one cannot deactivate regex from str.match (str.contains has a regex parameter to deactivate them)

提交回复
热议问题