Skip to content

pandas/literal-contains

Rule literal-contains
Pack pandas
Severity warning
Fix unsafe, with xpd check --fix --unsafe-fixes
Library versions every version

str.contains treats its pattern as a regular expression by default, even when the pattern is plain text. Pass regex=False for plain text, which is faster.

matches = df["name"].str.contains("load_")
matches = df["name"].str.contains("load_", regex=False)

The rule reports df["s"].str.contains("ta_") with a pattern that has no regular expression character, where regex=False gives the same match faster.

xpd check --fix --unsafe-fixes applies the fixes of this rule. Check the result, because an unsafe fix can change what the code does.

The unsafe fix writes str.contains("ta_", regex=False). On a column of Python objects, a value that is not text gives NaN with a pattern and False with regex=False.

xpd prints this message with the issue. A name that starts with $ stands for code from your file.

This pattern is plain text, so the regex engine is not needed. Pass regex=False.

Browse the other rules of the pandas pack.