Skip to content

pandas/date-part-key

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

Grouping on a year and a month uses two keys for one month and leaves out months with no rows. Group on pd.Grouper(freq="MS"), which gives one timestamp per month and was 6 times faster.

monthly = df.groupby([df["time"].dt.year, df["time"].dt.month])["load"].sum()
monthly = df.groupby(pd.Grouper(key="time", freq="MS"))["load"].sum()

The rule reports df.groupby([df["d"].dt.year, df["d"].dt.month]), and the same with index.year and index.month. On 2,000,000 quarter-hourly rows in Europe/Copenhagen the two keys took 89 ms and one monthly key took 14 ms.

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

The result has one timestamp per month in place of a year and a month, and it also holds the months without rows.

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

A year and a month are two keys for one month. Group on pd.Grouper(key=..., freq="MS"), or on pd.Grouper(freq="MS") for the index. The result has one timestamp per month and also holds the months without rows.

Browse the other rules of the pandas pack.