Skip to content

BUG: resample and Grouper raise NotImplementedError with a generic DateOffset - #71973

Open
hzmsh wants to merge 1 commit into
pandas-dev:mainfrom
hzmsh:fix-31697-resample-dateoffset
Open

hzmsh wants to merge 1 commit into
pandas-dev:mainfrom
hzmsh:fix-31697-resample-dateoffset

Conversation

@hzmsh

@hzmsh hzmsh commented Nov 9, 2026

Copy link
Copy Markdown

When users pass a DateOffset such as DateOffset(months=1) to resample, or to Grouper(freq=...), they get NotImplementedError: Prefix not defined, even though the resample docs say rule can be a DateOffset.

import pandas as pd

df = pd.DataFrame(
    {
        "Timestamp": pd.date_range("2020-01-01", "2020-03-01", periods=5),
        "Amount": [5, 10, 15, 20, 25],
    }
)
df.resample(pd.DateOffset(months=1), on="Timestamp").sum()
df.groupby(pd.Grouper(key="Timestamp", freq=pd.DateOffset(months=1))).sum()
# main:    both raise NotImplementedError: Prefix not defined
# this PR: both return
#             Amount
# Timestamp
# 2020-01-01      30
# 2020-02-01      20
# 2020-03-01      25

Cause:

  • Most pandas frequencies have a rule_code, a short name such as "ME" (month end) or "W" (weekly).
  • resample and Grouper(freq=...) both do their time binning in the TimeGrouper class. It reads freq.rule_code in two places (__init__ and _adjust_bin_edges), only to check whether the frequency is anchored to the end of a period, like month end or week.
  • DateOffset(months=1) adds one month but has no rule_code, so when TimeGrouper reads it, it raises Prefix not defined, and both resample and Grouper fail.

Fix:

  • This PR adds a small helper, _rule_code(freq), that returns "" when the frequency has no rule_code, following the existing handling in DatetimeIndexOpsMixin._parse_with_reso.
  • With "", both checks in TimeGrouper go to their else branch:
    1. __init__: closed and label default to "left", the same as for ordinary frequencies like "D".
    2. _adjust_bin_edges: the bin edges are used as they are, without the end-of-period adjustment.

Behavior change:

AI use: I worked with Claude Code (claude opus 5.5 (high)). Together we identified the root cause, wrote the fix and tests, and ran the new tests. It also helped turn my drafts and thoughts into this description. I also used gemini argon (high) for an additional review and audit.

…teOffset

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DataFrame.resample() breaks when rule parameter is of type DateOffset

1 participant