Skip to content

pandas/apply-date-expression

Rule apply-date-expression
Pack pandas
Severity warning
Fix none, edit by hand
Library versions every version

Reading a date part inside apply and comparing it runs Python once for every value, and was 22 to 94 times slower. Compare the whole column through .dt, as in df["d"].dt.weekday >= 5.

weekend = df["time"].apply(lambda time: time.weekday() >= 5)
weekend = df["time"].dt.weekday >= 5

The rule reports df["d"].apply(lambda t: t.weekday() >= 5) and lambda t: t.hour * 60 + t.minute, a lambda that compares a date part or computes with parts, which was 22 to 94 times slower than the column form. The rule has no fix, because the rewrite depends on the operator.

xpd has no automatic fix, so make the change by hand.

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

This reads a date part and compares it once per value in Python. Compare the whole column through .dt, as in $S.dt.weekday >= 5.

Browse the other rules of the pandas pack.