Skip to content

pandas/apply-date-part

Pack Severity Fix Versions
pandas warning unsafe, with xpd check --fix --unsafe-fixes All versions

Reading a date part such as the year inside apply runs Python once for every value. Read it from the whole column through .dt.

years = df["time"].apply(lambda time: time.year)
years = df["time"].dt.year

The rule reports df["d"].apply(lambda t: t.year), which calls Python once per value where the dt accessor works on the whole column. The fix is unsafe, because a column method returns a missing value where the lambda raised. The dtype can also change, as when dt.year gives int32 where the lambda gave int64.

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 df["d"].dt.year.

Use $S.dt.$P instead of a Python function per value.

See all rules in the pandas pack.