Skip to content

BUG: PeriodIndex.from_fields rejects non-December quarterly freqs (#55784) - #66553

Open
karandhaodiyal28-hash wants to merge 1 commit into
pandas-dev:mainfrom
karandhaodiyal28-hash:fix-55784-periodindex-quarterly-anchor
Open

BUG: PeriodIndex.from_fields rejects non-December quarterly freqs (#55784)#66553
karandhaodiyal28-hash wants to merge 1 commit into
pandas-dev:mainfrom
karandhaodiyal28-hash:fix-55784-periodindex-quarterly-anchor

Conversation

@karandhaodiyal28-hash

@karandhaodiyal28-hash karandhaodiyal28-hash commented Jul 30, 2026

Copy link
Copy Markdown

_range_from_fields validated a quarterly freq with an exact-equality check
base != FreqGroup.FR_QTR.value. Quarterly period dtype codes are FR_QTR
plus the anchor-month offset (Q-DEC=2000, Q-JAN=2001, ..., Q-FEB=2002), so
the check only accepted Q-DEC and raised AssertionError: base must equal FR_QTR for every other quarterly anchor -- even though the equivalent scalar
Period(year=2014, quarter=3, freq=QuarterEnd(startingMonth=2)) works fine.

>>> import pandas as pd
>>> pd.Period(year=2014, quarter=3, freq=pd.offsets.QuarterEnd(startingMonth=2))
Period('2014Q3', 'Q-FEB')
>>> pd.PeriodIndex.from_fields(year=[2014], quarter=[3],
...                            freq=pd.offsets.QuarterEnd(startingMonth=2))
AssertionError: base must equal FR_QTR

The fix uses a frequency-group membership test instead of exact equality:

if FreqGroup.from_period_dtype_code(base) != FreqGroup.FR_QTR:

base itself is unchanged (it is still passed to libperiod.period_ordinal),
so the anchored quarterly frequency is preserved; only the guard is corrected to
accept any code in the FR_QTR group. Added a regression test parametrized over
all twelve quarterly anchors, asserting the result matches the scalar Period
path. Non-quarterly frequencies are still rejected, and the existing Q-DEC
behavior is unchanged.

base = libperiod.freq_to_dtype_code(freq)
if base != FreqGroup.FR_QTR.value:
if FreqGroup.from_period_dtype_code(base) != FreqGroup.FR_QTR:
raise AssertionError("base must equal FR_QTR")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this reachable? if so, we should be raising something other than AssertionError

@jbrockmendel

Copy link
Copy Markdown
Member

I suspect the scalar case is similarly broken if you want to address it in a followup

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.

BUG: Unable to create PeriodIndex with freq of QuarterEnd with startingMonth != 12

2 participants